rmuller / infomas-asl Goto Github PK
View Code? Open in Web Editor NEWOpen sourced code of the INFOMAS PIM Application Suite
License: Apache License 2.0
Open sourced code of the INFOMAS PIM Application Suite
License: Apache License 2.0
Hi,
Docs show 3.1 example, but its not released yet?
This applies to 3.0.4. Please ignore if this has been dealt with already - I haven't checked.
My code in reportTypeAnnotation()
knows that an annotated class was found, but doesn't know in which jar or directory it was found. It'd be nice if another parameter could be added to reportTypeAnnotation()
to indicate it.
If the class path contains any jars that do not exist then detect will stop scanning without processing the remaining jars.
The check in ClassFileIterator whether the file exists should perhaps continue rather than return null if the file does not exist.
The build failed on my 64bit Windows with 1.6.u29 because of:
Failed tests:
test(eu.infomas.annotation.ZipFileIteratorTest): expected:<17439> but was:<17062>
The unpacked RT.JAR has 17061 classes. I guess the problem here is is the different architecture + an internal class.
This library is quite amazing for scanning the classpath for classes of interest.
Is there a chance to perhaps extend it to allow searching based on super types, i.e. to find all classes implementing a specific interface or extending a specific class?
Hi,
My jar is uploaded to some third party software which is saving it to some temporary location. And while doing that it's changing my jar's name to something like myjarname.jar.2343252352344.tmp (or something). It is still the same file, but it's name doesn't end in ".jar" anymore.
And it's being ignored while scanning because in ClassFileIterator.java there's this line (105): endsWithIgnoreCase(name, ".jar")
If you'll find another way to determine is the file is a jar or not - that will be great.
how to you obtain the value on a Annotation method?
in the function
reportTypeAnnotation(Class<? extends Annotation> annotation, String className)
i am not able to invoke a method or cast annotation to my Annotation type. how to do you do this?
Hi - I ran into this, when trying to use the annotation scanner from within GlassFish 4.1.1.
java.lang.ClassCastException: org.apache.felix.framework.URLHandlersBundleURLConnection cannot be cast to java.net.JarURLConnection
at eu.infomas.annotation.AnnotationDetector.detect(AnnotationDetector.java:287)
I'm going to see if I can come up with a patch - unless you have already noticed this / have a patch pending release.
Hi,
I get an error when scanning classes inside WEB-INF/lib. It's easy to reproduce: just "mvn clean install" on this project and deploy the .war file in Wildfly.
Here's the stacktrace:
2014-11-19 14:12:20,756 ERROR [org.jboss.msc.service.fail](MSC service thread 1-8) MSC000001: Failed to start service jboss.undertow.deployment.default-server.default-host./wuic-sample-js-sprite: org.jboss.msc.service.StartException in service jboss.undertow.deployment.default-server.default-host./wuic-sample-js-sprite: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_11]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_11]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_11]
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: URI scheme is not "file"
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:219)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:87)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.start(UndertowDeploymentService.java:72)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
... 3 more
Caused by: java.lang.IllegalArgumentException: URI scheme is not "file"
at java.io.File.(File.java:421) [rt.jar:1.8.0_11]
at eu.infomas.annotation.AnnotationDetector.toFile(AnnotationDetector.java:335)
at eu.infomas.annotation.AnnotationDetector.detect(AnnotationDetector.java:276)
at com.github.wuic.util.AnnotationDetectorScanner.scan(AnnotationDetectorScanner.java:75)
at com.github.wuic.config.ObjectBuilderFactory.(ObjectBuilderFactory.java:163)
at com.github.wuic.ContextBuilder.(ContextBuilder.java:190)
at com.github.wuic.ContextBuilder.(ContextBuilder.java:215)
at com.github.wuic.WuicFacade.(WuicFacade.java:178)
at com.github.wuic.WuicFacadeBuilder.build(WuicFacadeBuilder.java:283)
at com.github.wuic.jee.WuicServletContextListener.contextInitialized(WuicServletContextListener.java:94)
at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:173)
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:190)
... 7 more
In private void detect(final ClassFileIterator iterator)
there's a catch (Throwable)
which can and will catch any exception that is thrown from users' Reporter classes. This makes it harder for users to write correct code, since problems will be hidden from them.
This applies to version 3.0.4. I didn't check version 3.1.
Call Hierarchy:
reportTypeAnnotation(Class<? extends Annotation>, String) : void readAnnotations(DataInput, char) : void - eu.infomas.annotation.AnnotationDetector readAttributes(DataInput, char, boolean) : void - eu.infomas.annotation.AnnotationDetector detect(DataInput) : void - eu.infomas.annotation.AnnotationDetector detect(ClassFileIterator) : void - eu.infomas.annotation.AnnotationDetector detect() : void - eu.infomas.annotation.AnnotationDetector detect(File...) : void - eu.infomas.annotation.AnnotationDetector
java.lang.NullPointerException: Attempt to get length of null array
at eu.infomas.annotation.FileIterator.addReverse(FileIterator.java:117)
at eu.infomas.annotation.FileIterator.next(FileIterator.java:105)
at eu.infomas.annotation.FileIterator.next(FileIterator.java:106)
at eu.infomas.annotation.ClassFileIterator.next(ClassFileIterator.java:84)
at eu.infomas.annotation.AnnotationDetector.detect(AnnotationDetector.java:344)
at eu.infomas.annotation.AnnotationDetector.detect(AnnotationDetector.java:248)
See discussion @ issue #5
If I use the detect(File[])
API (the one which doesn't look at the
classpath but instead at the files/directories specified in the parameter)
it ignores jar files that it discovers when walking
the directory tree unless the jar file was specified in the File[]
parameter (a 'root file') .
The code that makes this happen is
else if (fileIterator.isRootFile() && endsWithIgnoreCase(name, ".jar"))
in ClassFileIterator.next()
specifically the isRootFile()
check.
This behaviour is not what I would have expected. Assuming that it's
intended, a mention in the javadoc would be worthwhile.
If classpath contains "." (current directory) then all sub-directories are scanned. The scanning however takes into account JAR files in the directories and reports annotations in them.
This behaviour differs from the classpath notion in Java (the JAR files in sub-dirs are not in the classpath). Attempt to load the classes from JARs in sub-dirs that are not explicitly specified in the classpath results in class loading errors.
Hi!
You example don't work. Instead .collect(AnnotationDetector::getType);
and .collect(AnnotationDetector::getMethod);
need use .collect(Cursor::getType);
and .collect(Cursor::getMethod);
respectively.
Best regards
I'm using https://github.com/flowersinthesand/portal-java and the author recommends using your annotation detector. Currently I'm using 3.0.2
.
In my dev environment everything works perfectly, once I get out into my packaged .jar it picks up classes in a package that doesn't contain those classes.
Scanning the package 'com.github.flowersinthesand.portal.support'
Scanned @Bean("adminSocketHandler") on 'com.company.production.socket.AdminSocketHandler'
Scanned @Bean("approvalSocketHandler") on 'com.company.production.workflow.process.approval.ApprovalSocketHandler'
Scanned @Bean("impositionManagerSocketHandler") on 'com.company.production.workflow.process.imposition.manager.ImpositionManagerSocketHandler'
Here's a screenshot of me browsing the .jar showing these classes don't exist:
The portal-java
project's package scanning source can be found here: https://github.com/flowersinthesand/portal-java/blob/portal-parent-0.6/core/src/main/java/com/github/flowersinthesand/portal/App.java#L138-L180
Since this works on my local machine within IntelliJ, but not in my packaged .jar, something's up. I'm not entirely sure if it's a bug in one of the applications, or something about how maven is packaging things for me.
I'm using Maven::package
with the shade plugin....
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>shade</goal></goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.company.production.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
First of all, thank you for this nice lib ๐
It seems that readAnnotationElementValue
is not implemented fully. It doesn't actually read the annotaion element value and store it. I think It's good feature to support as mostly of time when we scan annotator we will need element value along the way.
Hello I wonder if this library would work in a Java EE Application Server. I am using your excellent library in a java SE environment and works great. But it doesn't detect annotations in Jboss AS 7.1
Hi @rmuller
How can I find the value/parameter from an annotation?
Example:
@webservice(name="MyBestServiceName", targetNamespace="http://some.where.com")
public class MyBestService {
.......
I found your excellent package and use it Jersey REST application to autodiscover annotated types. Disovering appropriate classes I made detectAnnotatedTypes helper function to simplify use-case.
What do you think could informas-asl package provide similar helper method out of the box, detect by given types and return collection? This would make many basic use-cases run less customized code and library integration looked nice and clean.
private List<Class<?>> detectAnnotatedTypes(final Class<? extends Annotation>[] annotationTypes,
String... packageNames) {
final List<Class<?>> list = new ArrayList<Class<?>>();
TypeReporter reporter = new TypeReporter() {
@Override
public Class<? extends Annotation>[] annotations() { return annotationTypes; }
@Override
public void reportTypeAnnotation(Class<? extends Annotation> annotation, String className) {
try { list.add(Class.forName(className)); } catch (ClassNotFoundException ex) { }
}
};
AnnotationDetector cf = new AnnotationDetector(reporter);
try {
cf.detect(packageNames);
} catch (Exception ex) {
if (ex instanceof IllegalArgumentException) throw (IllegalArgumentException)ex;
throw new IllegalArgumentException(ex);
}
return list;
}
This is how I use it in Jersey REST custom Application class. Detecting jaxb beans and resource classes at runtime is easy.
...
private Class[] types;
// list annotated Java beans
List> classes = detectAnnotatedTypes(
new Class[] { javax.xml.bind.annotation.XmlRootElement.class }
, "com.myapp.dao"
);
types = classes.toArray(new Class<?>[classes.size()]);
context = new JSONJAXBContext(JSONConfiguration.natural().build(), types);
...
// list annotated Rest Resource classes
classes.addAll( detectAnnotatedTypes( new Class[] { javax.ws.rs.Path.class }
, "com.myapp.rest"
));
When running in Weblogic I get the following:
weblogic.application.ModuleException: [HTTP:101216]Servlet: "com.baloise.rest.Application" failed to preload on startup in Web application: "
java.lang.ClassCastException: weblogic.utils.zip.ZipURLConnection cannot be cast to java.net.JarURLConnection
at eu.infomas.annotation.AnnotationDetector.detect(AnnotationDetector.java:295)
at com.baloise.rest.Application.addFromClassPath(Application.java:59)
at com.baloise.rest.Application.getClasses(Application.java:26)
final AnnotationDetector.MethodReporter reporter = new AnnotationDetector.MethodReporter() {
@OverRide
public Class<? extends Annotation>[] annotations() {
return new Class[]{Test.class};
}
@Override
public void reportMethodAnnotation(Class<? extends Annotation> annotation,
String className, String methodName) {
System.out.println(className +" " + methodName); // do something
}
};
final AnnotationDetector cf = new AnnotationDetector(reporter);
cf.detect();
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at eu.infomas.annotation.AnnotationDetector.resolveUtf8(AnnotationDetector.java:540)
at eu.infomas.annotation.AnnotationDetector.readThisClass(AnnotationDetector.java:399)
at eu.infomas.annotation.AnnotationDetector.detect(AnnotationDetector.java:328)
at eu.infomas.annotation.AnnotationDetector.detect(AnnotationDetector.java:310)
at eu.infomas.annotation.AnnotationDetector.detect(AnnotationDetector.java:240)
at org.sgtest.submitter.SubmitterImpl.main(SubmitterImpl.java:59)
Suppose a jar to be scanned is stored in "/home/user/Test (W + P + S)/classes/test.jar" and loaded in the application classpath.
annotation-detector-3.1.0-SNAPSHOT.jar fails in this case with:
Caused by: java.lang.AssertionError: Not a File: /home/user/Test (W P S)/classes/test.jar
at eu.infomas.annotation.AnnotationDetector.detect(AnnotationDetector.java:297)
Please note the "+" sign of the directory is not shown in the
stacktrace. This is because URLDecoder.decode replaces "+" with a
space.
Patch below fixes the issue,
diff --git a/annotation-detector/src/main/java/eu/infomas/annotation/AnnotationDetector.java b/annotation-detector/src/main/java/eu/infomas/annotation/AnnotationDetector.java
index f44221e..0404c37 100644
--- a/annotation-detector/src/main/java/eu/infomas/annotation/AnnotationDetector.java
+++ b/annotation-detector/src/main/java/eu/infomas/annotation/AnnotationDetector.java
@@ -327,7 +327,14 @@ public final class AnnotationDetector {
private File toFile(final URL url) throws UnsupportedEncodingException {
// do not use url.toString() or url.toExternalForm() because of the
// 'file:' prefix / protocol identifier
return new File(URLDecoder.decode(url.getFile(), "UTF-8"));
//
// URLDecoder.decode will replace any '+' sign with a
// space. If url.getFile () happens to contain '+'s in its
// path the returned file path will be incorrect and the jar
// to scan will not be found at a later stage. So we convert
// the '+'s to their HTML character code (%2B) first so the
// decoder returns the '+'s back unchanged.
return new File(URLDecoder.decode(url.getFile().replaceAll("\\+", "%2B"), "UTF-8"));
}
private void detect(final ClassFileIterator iterator) throws IOException {
Hi. I came from Stackoverflow https://stackoverflow.com/questions/259140/scanning-java-annotations-at-runtime
I would like to try your library, but it's still in SNAPSHOT for 7 years. Can you please maybe release a 3.1.x version to maven central? It wouldn't matter if there are still features/issues open, at least I could try it out.
Hello, thanks for making your code available. I've tried to make use of it, but it's not doing anything for me.
I've downloaded the source and modified the pom.xml to use maven2. I've tried a lot of configurations.
This doesn't do anything:
public class AnnotationDetectorTest {
..
@Test
public void testClassPathScannerRT() throws IOException {
for (int i = 0; i < 6; ++i) {
final long time = System.currentTimeMillis();
final CountingReporter counter = new CountingReporter(Override.class);
final AnnotationDetector cf = new AnnotationDetector(counter);
and nothing.
This isn't actually what I want to do. I'm trying to operate on a set of packages of my own on the Java classpath. I still haven't managed to list anything.
Any help appreciated.
I'm encountering classes with the CONSTANT_InvokeDynamic - and the current scanner throws a (silent) exception and skips the class - failing to find any annotations on the class.
http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4
Its type '18'.
It looks like the current code only has constant pool types defined up to '12'
I am receiving an IOException complaining of too many open files while scanning. I have looked through the code and can find no place where the input streams are being closed.
Of course scanning classpath to detect annotations is a common use case but developer may also scan it to detect resources.
A great enhancement would be to detect resources matching a regex for instance. Not sure this is the scope of the project but the library seems to be already designed to introduce this feature without a lot of work.
What do you think?
It would nice to also have the list of class constructors annotated.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.