-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable preload of annotation metadata (#27)
- Loading branch information
Showing
8 changed files
with
331 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2024 Newport Robotics Group | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package com.nrg948; | ||
|
||
import com.nrg948.annotations.Annotations; | ||
import com.nrg948.preferences.RobotPreferences; | ||
|
||
/** A class to initialize the NRG Common Library. */ | ||
public final class Common { | ||
/* Disallow instantiation */ | ||
private Common() { | ||
} | ||
|
||
/** | ||
* Initializes the NRG Common library. | ||
* | ||
* This method must be called in the <code>Robot.initRobot()</code> method | ||
* before the <code>RobotContainer</code> is created. | ||
* | ||
* @param pkgs The packages to scan for annotations implemented by the NRG | ||
* Common Library. | ||
*/ | ||
public static void init(String... pkgs) { | ||
Annotations.init(pkgs); | ||
RobotPreferences.init(); | ||
} | ||
} |
146 changes: 146 additions & 0 deletions
146
nrgcommon/src/main/java/com/nrg948/annotations/Annotations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2024 Newport Robotics Group | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
package com.nrg948.annotations; | ||
|
||
import static org.reflections.scanners.Scanners.FieldsAnnotated; | ||
import static org.reflections.scanners.Scanners.MethodsAnnotated; | ||
import static org.reflections.scanners.Scanners.SubTypes; | ||
import static org.reflections.scanners.Scanners.TypesAnnotated; | ||
|
||
import java.net.JarURLConnection; | ||
import java.net.URL; | ||
import java.util.Arrays; | ||
import java.util.Enumeration; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
|
||
import org.reflections.Reflections; | ||
import org.reflections.Store; | ||
import org.reflections.serializers.Serializer; | ||
import org.reflections.serializers.XmlSerializer; | ||
import org.reflections.util.ConfigurationBuilder; | ||
import org.reflections.util.QueryFunction; | ||
|
||
/** | ||
* A class providing access to types annotated by the NRG Common Library | ||
* annotations. | ||
*/ | ||
public final class Annotations { | ||
private static Reflections reflections; | ||
|
||
/* Disallow instantiation */ | ||
private Annotations() { | ||
} | ||
|
||
/** | ||
* Initializes the annotation metadata for the NRG Common Library. | ||
* | ||
* @param pkgs The packages to scan for annotations implemented by the NRG | ||
* Common Library. | ||
*/ | ||
public static void init(String... pkgs) { | ||
reflections = loadFromMetadata().orElseGet(() -> scanPackages(pkgs)); | ||
} | ||
|
||
/** | ||
* Creates and initializes a {@link Reflections} instance from metadata, if | ||
* present. | ||
* | ||
* Reflections metadata is stored in the META-INF/reflections directory in the | ||
* program's JAR file. | ||
* | ||
* @return An optional {@link Reflections} instance initialized from metadata. | ||
* If no metadata is present, {@link Optional#empty()} is returned. | ||
*/ | ||
private static Optional<Reflections> loadFromMetadata() { | ||
Optional<Reflections> reflections = Optional.empty(); | ||
Serializer xmlSerializer = new XmlSerializer(); | ||
ClassLoader loader = ClassLoader.getSystemClassLoader(); | ||
|
||
try { | ||
Enumeration<URL> resources = loader.getResources("META-INF/reflections"); | ||
|
||
while (resources.hasMoreElements()) { | ||
URL url = resources.nextElement(); | ||
JarURLConnection connection = (JarURLConnection) url.openConnection(); | ||
|
||
try (JarFile jar = connection.getJarFile()) { | ||
Enumeration<JarEntry> entries = jar.entries(); | ||
|
||
while (entries.hasMoreElements()) { | ||
JarEntry entry = entries.nextElement(); | ||
|
||
if (entry.getName().endsWith("-reflections.xml")) { | ||
if (reflections.isEmpty()) { | ||
reflections = Optional.of(new Reflections()); | ||
} | ||
|
||
reflections.get().collect(jar.getInputStream(entry), xmlSerializer); | ||
} | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
System.err.println("WARNING: Failed to load Reflections metadata: " + e.getMessage()); | ||
reflections = null; | ||
} | ||
|
||
return reflections; | ||
} | ||
|
||
/** | ||
* Scans the specified packages for annotations implemented by the NRG Common | ||
* Library. | ||
* | ||
* @param pkgs The packages to scan for annotations implemented by the NRG | ||
* Common Library. | ||
* | ||
* @return The annotation metadata for the specified packages. | ||
*/ | ||
private static Reflections scanPackages(String... pkgs) { | ||
String[] allPkgs = Arrays.copyOf(pkgs, pkgs.length + 1); | ||
|
||
allPkgs[allPkgs.length - 1] = "com.nrg948"; | ||
|
||
return new Reflections( | ||
new ConfigurationBuilder() | ||
.forPackages(allPkgs) | ||
.setScanners(FieldsAnnotated, MethodsAnnotated, SubTypes, TypesAnnotated)); | ||
} | ||
|
||
/** | ||
* Returns the set of elements annotated with the specified annotation. | ||
* | ||
* @param <T> The type of element. | ||
* @param query The query function. | ||
* | ||
* @return The set of elements annotated with the specified annotation. | ||
*/ | ||
public static <T> Set<T> get(QueryFunction<Store, T> query) { | ||
return reflections.get(query); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2024 Newport Robotics Group | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* The NRG Common Library provides a set of classes, annotations and utilities | ||
* used by FIRST Robotics Competition Team 948 - Newport Robotics Group (NRG948). | ||
* | ||
* <p> | ||
* To initialize the libray, you must call the {@link Common#init(String...)} | ||
* method passing the name of the robot package. In the Command-based Robot, | ||
* this must be done in the <code>Robot.initRobot()</code> method before the | ||
* <code>RobotContainer</code> is created.<br> | ||
* | ||
* <pre> | ||
* <code> | ||
* {@literal @}Override | ||
* public void robotInit() { | ||
* // Initialize the NRG Common Library before creating the RobotContainer so that | ||
* // it is initialized and ready for use by the subsystems. | ||
* Common.init("frc.robot"); | ||
* | ||
* // Instantiate our RobotContainer. This will perform all our button bindings, | ||
* // and put our autonomous chooser on the dashboard. | ||
* m_robotContainer = new RobotContainer(); | ||
* } | ||
* </code> | ||
* </pre> | ||
*/ | ||
package com.nrg948; |
Oops, something went wrong.