Skip to content

Commit a6a4466

Browse files
committed
Optimize annotation inspection
The annotation attributes are not needed, so just read the names when inspecting the class files. Reduces startup time.
1 parent 1e09ff0 commit a6a4466

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

grails-core/src/main/groovy/org/grails/asm/AnnotationMetadataReader.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.grails.asm;
22

3+
import org.springframework.asm.AnnotationVisitor;
4+
import org.springframework.asm.SpringAsmInfo;
5+
import org.springframework.asm.Type;
36
import org.springframework.core.NestedIOException;
47
import org.springframework.core.io.Resource;
58
import org.springframework.core.type.AnnotationMetadata;
@@ -24,8 +27,24 @@ public class AnnotationMetadataReader implements MetadataReader {
2427

2528
private final AnnotationMetadata annotationMetadata;
2629

27-
30+
/**
31+
* Reads only the annotation names from a class resource
32+
* @param resource The resource
33+
* @param classLoader The classloader
34+
* @throws IOException
35+
*/
2836
public AnnotationMetadataReader(Resource resource, ClassLoader classLoader) throws IOException {
37+
this(resource, classLoader, false);
38+
}
39+
/**
40+
* Constructs a new annotation metadata reader
41+
*
42+
* @param resource The resource
43+
* @param classLoader The classloader
44+
* @param readAttributeValues Whether to read the attributes in addition or just the annotation class names
45+
* @throws IOException
46+
*/
47+
public AnnotationMetadataReader(Resource resource, ClassLoader classLoader, boolean readAttributeValues) throws IOException {
2948
InputStream is = new BufferedInputStream(resource.getInputStream());
3049
ClassReader classReader;
3150
try {
@@ -39,7 +58,22 @@ public AnnotationMetadataReader(Resource resource, ClassLoader classLoader) thro
3958
is.close();
4059
}
4160

42-
AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(classLoader);
61+
62+
AnnotationMetadataReadingVisitor visitor;
63+
64+
if(readAttributeValues) {
65+
visitor = new AnnotationMetadataReadingVisitor(classLoader);
66+
}
67+
else {
68+
visitor = new AnnotationMetadataReadingVisitor(classLoader) {
69+
@Override
70+
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
71+
String className = Type.getType(desc).getClassName();
72+
this.annotationSet.add(className);
73+
return new EmptyAnnotationVisitor();
74+
}
75+
};
76+
}
4377
classReader.accept(visitor, ClassReader.SKIP_DEBUG);
4478

4579
this.annotationMetadata = visitor;
@@ -63,4 +97,21 @@ public ClassMetadata getClassMetadata() {
6397
public AnnotationMetadata getAnnotationMetadata() {
6498
return this.annotationMetadata;
6599
}
100+
101+
private static class EmptyAnnotationVisitor extends AnnotationVisitor {
102+
103+
public EmptyAnnotationVisitor() {
104+
super(SpringAsmInfo.ASM_VERSION);
105+
}
106+
107+
@Override
108+
public AnnotationVisitor visitAnnotation(String name, String desc) {
109+
return this;
110+
}
111+
112+
@Override
113+
public AnnotationVisitor visitArray(String name) {
114+
return this;
115+
}
116+
}
66117
}

0 commit comments

Comments
 (0)