org.springframework.asm.ClassReader Java Examples

The following examples show how to use org.springframework.asm.ClassReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MethodMetadataReadingVisitorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected AnnotationMetadata get(Class<?> source) {
	try {
		ClassLoader classLoader = source.getClassLoader();
		String className = source.getName();
		String resourcePath = ResourceLoader.CLASSPATH_URL_PREFIX
				+ ClassUtils.convertClassNameToResourcePath(className)
				+ ClassUtils.CLASS_FILE_SUFFIX;
		Resource resource = new DefaultResourceLoader().getResource(resourcePath);
		try (InputStream inputStream = new BufferedInputStream(
				resource.getInputStream())) {
			ClassReader classReader = new ClassReader(inputStream);
			AnnotationMetadataReadingVisitor metadata = new AnnotationMetadataReadingVisitor(
					classLoader);
			classReader.accept(metadata, ClassReader.SKIP_DEBUG);
			return metadata;
		}
	}
	catch (Exception ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example #2
Source File: AnnotationMetadataReadingVisitorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
protected AnnotationMetadata get(Class<?> source) {
	try {
		ClassLoader classLoader = source.getClassLoader();
		String className = source.getName();
		String resourcePath = ResourceLoader.CLASSPATH_URL_PREFIX
				+ ClassUtils.convertClassNameToResourcePath(className)
				+ ClassUtils.CLASS_FILE_SUFFIX;
		Resource resource = new DefaultResourceLoader().getResource(resourcePath);
		try (InputStream inputStream = new BufferedInputStream(
				resource.getInputStream())) {
			ClassReader classReader = new ClassReader(inputStream);
			AnnotationMetadataReadingVisitor metadata = new AnnotationMetadataReadingVisitor(
					classLoader);
			classReader.accept(metadata, ClassReader.SKIP_DEBUG);
			return metadata;
		}
	}
	catch (Exception ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example #3
Source File: MergedAnnotationMetadataVisitorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void loadFrom(Class<?> type) {
	ClassVisitor visitor = new ClassVisitor(SpringAsmInfo.ASM_VERSION) {

		@Override
		public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) {
			return MergedAnnotationReadingVisitor.get(getClass().getClassLoader(),
					null, descriptor, visible,
					annotation -> MergedAnnotationMetadataVisitorTests.this.annotation = annotation);
		}

	};
	try {
		new ClassReader(type.getName()).accept(visitor, ClassReader.SKIP_DEBUG
				| ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES);
	}
	catch (IOException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example #4
Source File: SimpleMetadataReader.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private static ClassReader getClassReader(Resource resource) throws IOException {
	try (InputStream is = new BufferedInputStream(resource.getInputStream())) {
		try {
			return new ClassReader(is);
		}
		catch (IllegalArgumentException ex) {
			throw new NestedIOException("ASM ClassReader failed to parse class file - " +
					"probably due to a new Java class file version that isn't supported yet: " + resource, ex);
		}
	}
}
 
Example #5
Source File: SimpleMetadataReader.java    From thinking-in-spring-boot-samples with Apache License 2.0 5 votes vote down vote up
SimpleMetadataReader(Resource resource, ClassLoader classLoader) throws IOException {
    this.resource = resource;
    InputStream is = this.resource.getInputStream();
    try {
        this.classReader = new ClassReader(is);
    }
    finally {
        is.close();
    }
    this.classLoader = classLoader;
}
 
Example #6
Source File: AbstractByteCodeLoadingProxy.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void afterPropertiesSet() throws Exception {
	byte[] bytes = FileCopyUtils.copyToByteArray(this.resource.getInputStream());
	String className = new ClassReader(bytes).getClassName().replace("/", ".");
	Class<?> factoryClass = this.classLoader.defineClass(className, bytes);
	try {
		this.target = ((CompilationResultFactory<T>) factoryClass.newInstance())
				.getResult();
		this.method = findFactoryMethod(factoryClass);
	}
	catch (InstantiationException | IllegalAccessException e) {
		throw new IllegalArgumentException("failed to load Function byte code", e);
	}
}
 
Example #7
Source File: ClassPathTestScanner.java    From COLA with GNU Lesser General Public License v2.1 4 votes vote down vote up
private String getClassName(Resource resource) throws IOException {
    ClassReader classReader = new ClassReader(resource.getInputStream());
    String className = classReader.getClassName();
    className = className.replaceAll("/", ".");
    return className;
}