@Override protected boolean removeEldestEntry(Map.Entry<Resource, MetadataReader> eldest) { return size() > this.cacheLimit; } }
/** * Create a new CachingMetadataReaderFactory for the default class loader, * using a local resource cache. */ public CachingMetadataReaderFactory() { super(); setCacheLimit(DEFAULT_CACHE_LIMIT); }
@Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { // no-op return new EmptyFieldVisitor(); }
@Test public void asmAnnotationMetadataForInterface() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(AnnotationMetadata.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); doTestMetadataForInterfaceClass(metadata); }
@Override public void setBeanClassLoader(ClassLoader beanClassLoader) { this.beanClassLoader = beanClassLoader; if (!this.setMetadataReaderFactoryCalled) { this.metadataReaderFactory = new CachingMetadataReaderFactory(beanClassLoader); } }
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { // Skip bridge methods - we're only interested in original annotation-defining user methods. // On JDK 8, we'd otherwise run into double detection of the same annotated method... if ((access & Opcodes.ACC_BRIDGE) != 0) { return super.visitMethod(access, name, desc, signature, exceptions); } return new MethodMetadataReadingVisitor(name, access, getClassName(), Type.getReturnType(desc).getClassName(), this.classLoader, this.methodMetadataSet); }
@Override @Nullable public AnnotationAttributes getAnnotationAttributes(String annotationName, boolean classValuesAsString) { AnnotationAttributes raw = AnnotationReadingVisitorUtils.getMergedAnnotationAttributes( this.attributesMap, this.metaAnnotationMap, annotationName); if (raw == null) { return null; } return AnnotationReadingVisitorUtils.convertClassValues( "class '" + getClassName() + "'", this.classLoader, raw, classValuesAsString); }
@Override @Nullable public AnnotationAttributes getAnnotationAttributes(String annotationName, boolean classValuesAsString) { AnnotationAttributes raw = AnnotationReadingVisitorUtils.getMergedAnnotationAttributes( this.attributesMap, this.metaAnnotationMap, annotationName); if (raw == null) { return null; } return AnnotationReadingVisitorUtils.convertClassValues( "method '" + getMethodName() + "'", this.classLoader, raw, classValuesAsString); }
@Override public void visitEnum(String attributeName, String asmTypeDescriptor, String attributeValue) { Object newValue = getEnumValue(asmTypeDescriptor, attributeValue); visit(attributeName, newValue); }
/** * Clear the local metadata cache, if any, removing all cached class metadata. */ public void clearCache() { if (this.metadataReaderFactory instanceof CachingMetadataReaderFactory) { // Clear cache in externally provided MetadataReaderFactory; this is a no-op // for a shared cache since it'll be cleared by the ApplicationContext. ((CachingMetadataReaderFactory) this.metadataReaderFactory).clearCache(); } }
@Override public AnnotationVisitor visitArray(String attributeName) { return new RecursiveAnnotationArrayVisitor(attributeName, this.attributes, this.classLoader); }
/** * Return the maximum number of entries for the MetadataReader cache. */ public int getCacheLimit() { if (this.metadataReaderCache instanceof LocalResourceCache) { return ((LocalResourceCache) this.metadataReaderCache).getCacheLimit(); } else { return (this.metadataReaderCache != null ? Integer.MAX_VALUE : 0); } }
@Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { // no-op return new EmptyMethodVisitor(); }
@Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { // no-op return new EmptyAnnotationVisitor(); }
/** * https://jira.spring.io/browse/SPR-11649 */ @Test public void multipleAnnotationsWithIdenticalAttributeNamesUsingAnnotationMetadataReadingVisitor() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(NamedAnnotationsClass.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); assertMultipleAnnotationsWithIdenticalAttributeNames(metadata); }
/** * Return the MetadataReaderFactory used by this component provider. */ public final MetadataReaderFactory getMetadataReaderFactory() { if (this.metadataReaderFactory == null) { this.metadataReaderFactory = new CachingMetadataReaderFactory(); } return this.metadataReaderFactory; }
@Test public void metaAnnotationOverridesUsingAnnotationMetadataReadingVisitor() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(ComposedConfigurationWithAttributeOverridesClass.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); assertMetaAnnotationOverrides(metadata); }
/** * https://jira.spring.io/browse/SPR-11649 */ @Test public void composedAnnotationWithMetaAnnotationsWithIdenticalAttributeNamesUsingAnnotationMetadataReadingVisitor() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(NamedComposedAnnotationClass.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); assertMultipleAnnotationsWithIdenticalAttributeNames(metadata); }
@Test public void asmAnnotationMetadataForAnnotation() throws Exception { MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(Component.class.getName()); AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); doTestMetadataForAnnotationClass(metadata); }