Skip to content

Commit bacaa1d

Browse files
committed
Add MetadataReaderLog interface
Add a MetadataReaderLog callback interface that can be used with SimpleMetadataReader and CachingMetadataReaderFactory to decide how meta-data read log messages should be handled.
1 parent 985ede9 commit bacaa1d

7 files changed

+103
-33
lines changed

spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationAttributesReadingVisitor.java

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,19 @@
4545
*/
4646
abstract class AbstractRecursiveAnnotationVisitor extends AnnotationVisitor {
4747

48-
protected final Log logger = LogFactory.getLog(this.getClass());
48+
protected final MetadataReaderLog logger;
4949

5050
protected final AnnotationAttributes attributes;
5151

5252
protected final ClassLoader classLoader;
5353

5454

55-
public AbstractRecursiveAnnotationVisitor(ClassLoader classLoader, AnnotationAttributes attributes) {
55+
public AbstractRecursiveAnnotationVisitor(ClassLoader classLoader,
56+
AnnotationAttributes attributes, MetadataReaderLog logger) {
5657
super(SpringAsmInfo.ASM_VERSION);
5758
this.classLoader = classLoader;
5859
this.attributes = attributes;
60+
this.logger = (logger == null ? new MetadataReaderLogAdapter() : logger);
5961
}
6062

6163

@@ -67,11 +69,13 @@ public AnnotationVisitor visitAnnotation(String attributeName, String asmTypeDes
6769
String annotationType = Type.getType(asmTypeDescriptor).getClassName();
6870
AnnotationAttributes nestedAttributes = new AnnotationAttributes();
6971
this.attributes.put(attributeName, nestedAttributes);
70-
return new RecursiveAnnotationAttributesVisitor(annotationType, nestedAttributes, this.classLoader);
72+
return new RecursiveAnnotationAttributesVisitor(annotationType, nestedAttributes,
73+
this.classLoader, this.logger);
7174
}
7275

7376
public AnnotationVisitor visitArray(String attributeName) {
74-
return new RecursiveAnnotationArrayVisitor(attributeName, this.attributes, this.classLoader);
77+
return new RecursiveAnnotationArrayVisitor(attributeName, this.attributes,
78+
this.classLoader, this.logger);
7579
}
7680

7781
public void visitEnum(String attributeName, String asmTypeDescriptor, String attributeValue) {
@@ -84,10 +88,10 @@ public void visitEnum(String attributeName, String asmTypeDescriptor, String att
8488
}
8589
}
8690
catch (ClassNotFoundException ex) {
87-
this.logger.debug("Failed to classload enum type while reading annotation metadata", ex);
91+
this.logger.log("Failed to classload enum type while reading annotation metadata", ex);
8892
}
8993
catch (IllegalAccessException ex) {
90-
this.logger.warn("Could not access enum value while reading annotation metadata", ex);
94+
this.logger.log("Could not access enum value while reading annotation metadata", ex);
9195
}
9296
this.attributes.put(attributeName, valueToUse);
9397
}
@@ -106,9 +110,10 @@ final class RecursiveAnnotationArrayVisitor extends AbstractRecursiveAnnotationV
106110
private final List<AnnotationAttributes> allNestedAttributes = new ArrayList<AnnotationAttributes>();
107111

108112

109-
public RecursiveAnnotationArrayVisitor(
110-
String attributeName, AnnotationAttributes attributes, ClassLoader classLoader) {
111-
super(classLoader, attributes);
113+
public RecursiveAnnotationArrayVisitor(String attributeName,
114+
AnnotationAttributes attributes, ClassLoader classLoader,
115+
MetadataReaderLog logger) {
116+
super(classLoader, attributes, logger);
112117
this.attributeName = attributeName;
113118
}
114119

@@ -132,7 +137,8 @@ public AnnotationVisitor visitAnnotation(String attributeName, String asmTypeDes
132137
String annotationType = Type.getType(asmTypeDescriptor).getClassName();
133138
AnnotationAttributes nestedAttributes = new AnnotationAttributes();
134139
this.allNestedAttributes.add(nestedAttributes);
135-
return new RecursiveAnnotationAttributesVisitor(annotationType, nestedAttributes, this.classLoader);
140+
return new RecursiveAnnotationAttributesVisitor(annotationType, nestedAttributes,
141+
this.classLoader, this.logger);
136142
}
137143

138144
public void visitEnd() {
@@ -154,9 +160,10 @@ class RecursiveAnnotationAttributesVisitor extends AbstractRecursiveAnnotationVi
154160
private final String annotationType;
155161

156162

157-
public RecursiveAnnotationAttributesVisitor(
158-
String annotationType, AnnotationAttributes attributes, ClassLoader classLoader) {
159-
super(classLoader, attributes);
163+
public RecursiveAnnotationAttributesVisitor(String annotationType,
164+
AnnotationAttributes attributes, ClassLoader classLoader,
165+
MetadataReaderLog logger) {
166+
super(classLoader, attributes, logger);
160167
this.annotationType = annotationType;
161168
}
162169

@@ -167,7 +174,7 @@ public final void visitEnd() {
167174
this.doVisitEnd(annotationClass);
168175
}
169176
catch (ClassNotFoundException ex) {
170-
this.logger.debug("Failed to classload type while reading annotation " +
177+
this.logger.log("Failed to classload type while reading annotation " +
171178
"metadata. This is a non-fatal error, but certain annotation " +
172179
"metadata may be unavailable.", ex);
173180
}
@@ -226,11 +233,11 @@ final class AnnotationAttributesReadingVisitor extends RecursiveAnnotationAttrib
226233
private final Map<String, Set<String>> metaAnnotationMap;
227234

228235

229-
public AnnotationAttributesReadingVisitor(
230-
String annotationType, MultiValueMap<String, AnnotationAttributes> attributesMap,
231-
Map<String, Set<String>> metaAnnotationMap, ClassLoader classLoader) {
232-
233-
super(annotationType, new AnnotationAttributes(), classLoader);
236+
public AnnotationAttributesReadingVisitor(String annotationType,
237+
MultiValueMap<String, AnnotationAttributes> attributesMap,
238+
Map<String, Set<String>> metaAnnotationMap, ClassLoader classLoader,
239+
MetadataReaderLog logger) {
240+
super(annotationType, new AnnotationAttributes(), classLoader, logger);
234241
this.annotationType = annotationType;
235242
this.attributesMap = attributesMap;
236243
this.metaAnnotationMap = metaAnnotationMap;
@@ -267,3 +274,14 @@ private void recusivelyCollectMetaAnnotations(Set<String> visited, Annotation an
267274
}
268275
}
269276
}
277+
278+
279+
class MetadataReaderLogAdapter implements MetadataReaderLog {
280+
281+
private Log logger = LogFactory.getLog(AnnotationAttributesReadingVisitor.class);
282+
283+
@Override
284+
public void log(String message, Throwable t) {
285+
logger.debug(message, t);
286+
}
287+
}

spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationMetadataReadingVisitor.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,27 @@ final class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisitor
5656

5757
private final MultiValueMap<String, MethodMetadata> methodMetadataMap = new LinkedMultiValueMap<String, MethodMetadata>();
5858

59+
private final MetadataReaderLog logger;
5960

60-
public AnnotationMetadataReadingVisitor(ClassLoader classLoader) {
61+
62+
public AnnotationMetadataReadingVisitor(ClassLoader classLoader, MetadataReaderLog logger) {
6163
this.classLoader = classLoader;
64+
this.logger = logger;
6265
}
6366

6467

6568
@Override
6669
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
67-
return new MethodMetadataReadingVisitor(name, access, this.getClassName(), this.classLoader, this.methodMetadataMap);
70+
return new MethodMetadataReadingVisitor(name, access, this.getClassName(),
71+
this.classLoader, this.methodMetadataMap, this.logger);
6872
}
6973

7074
@Override
7175
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
7276
String className = Type.getType(desc).getClassName();
7377
this.annotationSet.add(className);
74-
return new AnnotationAttributesReadingVisitor(className, this.attributeMap, this.metaAnnotationMap, this.classLoader);
78+
return new AnnotationAttributesReadingVisitor(className, this.attributeMap,
79+
this.metaAnnotationMap, this.classLoader, this.logger);
7580
}
7681

7782

spring-core/src/main/java/org/springframework/core/type/classreading/CachingMetadataReaderFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -92,14 +92,14 @@ public int getCacheLimit() {
9292

9393

9494
@Override
95-
public MetadataReader getMetadataReader(Resource resource) throws IOException {
95+
public MetadataReader getMetadataReader(Resource resource, MetadataReaderLog logger) throws IOException {
9696
if (getCacheLimit() <= 0) {
9797
return super.getMetadataReader(resource);
9898
}
9999
synchronized (this.metadataReaderCache) {
100100
MetadataReader metadataReader = this.metadataReaderCache.get(resource);
101101
if (metadataReader == null) {
102-
metadataReader = super.getMetadataReader(resource);
102+
metadataReader = super.getMetadataReader(resource, logger);
103103
this.metadataReaderCache.put(resource, metadataReader);
104104
}
105105
return metadataReader;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core.type.classreading;
18+
19+
/**
20+
* Callback interface that can be used to collect non-fatal log messages from a
21+
* {@link MetadataReaderFactory} implementation. Allows callers to decide how
22+
* and when non-fatal log messages should be displayed.
23+
*
24+
* @author Phillip Webb
25+
* @since 4.0
26+
*/
27+
public interface MetadataReaderLog {
28+
29+
/**
30+
* Called to log a non fatal error whilst reading meta-data.
31+
* @param message the log message
32+
* @param t the underlying cause (may be {@code null})
33+
*/
34+
void log(String message, Throwable t);
35+
36+
}

spring-core/src/main/java/org/springframework/core/type/classreading/MethodMetadataReadingVisitor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,25 @@ final class MethodMetadataReadingVisitor extends MethodVisitor implements Method
5555

5656
private final MultiValueMap<String, AnnotationAttributes> attributeMap = new LinkedMultiValueMap<String, AnnotationAttributes>(2);
5757

58+
private final MetadataReaderLog logger;
59+
5860
public MethodMetadataReadingVisitor(String name, int access, String declaringClassName, ClassLoader classLoader,
59-
MultiValueMap<String, MethodMetadata> methodMetadataMap) {
61+
MultiValueMap<String, MethodMetadata> methodMetadataMap, MetadataReaderLog logger) {
6062
super(SpringAsmInfo.ASM_VERSION);
6163
this.name = name;
6264
this.access = access;
6365
this.declaringClassName = declaringClassName;
6466
this.classLoader = classLoader;
6567
this.methodMetadataMap = methodMetadataMap;
68+
this.logger = logger;
6669
}
6770

6871
@Override
6972
public AnnotationVisitor visitAnnotation(final String desc, boolean visible) {
7073
String className = Type.getType(desc).getClassName();
7174
methodMetadataMap.add(className, this);
72-
return new AnnotationAttributesReadingVisitor(className, this.attributeMap, null, this.classLoader);
75+
return new AnnotationAttributesReadingVisitor(className, this.attributeMap, null,
76+
this.classLoader, this.logger);
7377
}
7478

7579
public String getMethodName() {

spring-core/src/main/java/org/springframework/core/type/classreading/SimpleMetadataReader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ final class SimpleMetadataReader implements MetadataReader {
4646
private final AnnotationMetadata annotationMetadata;
4747

4848

49-
SimpleMetadataReader(Resource resource, ClassLoader classLoader) throws IOException {
49+
SimpleMetadataReader(Resource resource, ClassLoader classLoader, MetadataReaderLog logger) throws IOException {
5050
InputStream is = new BufferedInputStream(resource.getInputStream());
5151
ClassReader classReader;
5252
try {
@@ -60,7 +60,7 @@ final class SimpleMetadataReader implements MetadataReader {
6060
is.close();
6161
}
6262

63-
AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(classLoader);
63+
AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(classLoader, logger);
6464
classReader.accept(visitor, ClassReader.SKIP_DEBUG);
6565

6666
this.annotationMetadata = visitor;

spring-core/src/main/java/org/springframework/core/type/classreading/SimpleMetadataReaderFactory.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -69,15 +69,22 @@ public final ResourceLoader getResourceLoader() {
6969
return this.resourceLoader;
7070
}
7171

72-
7372
public MetadataReader getMetadataReader(String className) throws IOException {
73+
return getMetadataReader(className, null);
74+
}
75+
76+
public MetadataReader getMetadataReader(String className, MetadataReaderLog logger) throws IOException {
7477
String resourcePath = ResourceLoader.CLASSPATH_URL_PREFIX +
7578
ClassUtils.convertClassNameToResourcePath(className) + ClassUtils.CLASS_FILE_SUFFIX;
76-
return getMetadataReader(this.resourceLoader.getResource(resourcePath));
79+
return getMetadataReader(this.resourceLoader.getResource(resourcePath), logger);
7780
}
7881

7982
public MetadataReader getMetadataReader(Resource resource) throws IOException {
80-
return new SimpleMetadataReader(resource, this.resourceLoader.getClassLoader());
83+
return getMetadataReader(resource, null);
84+
}
85+
86+
public MetadataReader getMetadataReader(Resource resource, MetadataReaderLog logger) throws IOException {
87+
return new SimpleMetadataReader(resource, this.resourceLoader.getClassLoader(), logger);
8188
}
8289

8390
}

0 commit comments

Comments
 (0)