use of org.objectweb.asm.AnnotationVisitor in project drill by apache.
the class CheckMethodVisitorFsm method visitLocalVariableAnnotation.
@Override
public AnnotationVisitor visitLocalVariableAnnotation(final int typeRef, final TypePath typePath, final Label[] start, final Label[] end, final int[] index, final String desc, final boolean visible) {
fsmCursor.transition("visitLocalVariableAnnotation");
final AnnotationVisitor annotationVisitor = super.visitLocalVariableAnnotation(typeRef, typePath, start, end, index, desc, visible);
// TODO: add CheckAnnotationVisitorFsm
return annotationVisitor;
}
use of org.objectweb.asm.AnnotationVisitor in project drill by apache.
the class CheckMethodVisitorFsm method visitParameterAnnotation.
@Override
public AnnotationVisitor visitParameterAnnotation(final int parameter, final String desc, final boolean visible) {
fsmCursor.transition("visitParameterAnnotation");
final AnnotationVisitor annotationVisitor = super.visitParameterAnnotation(parameter, desc, visible);
// TODO: add CheckAnnotationVisitorFsm
return annotationVisitor;
}
use of org.objectweb.asm.AnnotationVisitor in project cdap by caskdata.
the class ArtifactInspector method isPlugin.
/**
* Detects if a class is annotated with {@link Plugin} without loading the class.
*
* @param className name of the class
* @param classLoader ClassLoader for loading the class file of the given class
* @return true if the given class is annotated with {@link Plugin}
*/
private boolean isPlugin(String className, ClassLoader classLoader) {
try (InputStream is = classLoader.getResourceAsStream(className.replace('.', '/') + ".class")) {
if (is == null) {
return false;
}
// Use ASM to inspect the class bytecode to see if it is annotated with @Plugin
final boolean[] isPlugin = new boolean[1];
ClassReader cr = new ClassReader(is);
cr.accept(new ClassVisitor(Opcodes.ASM5) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if (Plugin.class.getName().equals(Type.getType(desc).getClassName()) && visible) {
isPlugin[0] = true;
}
return super.visitAnnotation(desc, visible);
}
}, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
return isPlugin[0];
} catch (IOException e) {
// If failed to open the class file, then it cannot be a plugin
LOG.warn("Failed to open class file for {}", className, e);
return false;
}
}
use of org.objectweb.asm.AnnotationVisitor in project cdap by caskdata.
the class HttpHandlerGenerator method inspectHandler.
/**
* Inspects the given type and copy/rewrite handler methods from it into the newly generated class.
*
* @param delegateType The user handler type
* @param inspectType The type that needs to be inspected. It's either the delegateType or one of its parents
*/
private void inspectHandler(final TypeToken<?> delegateType, final TypeToken<?> inspectType, final String pathPrefix, final Type classType, final ClassWriter classWriter, final List<Class<?>> preservedClasses) throws IOException {
Class<?> rawType = inspectType.getRawType();
// Visit the delegate class, copy and rewrite handler method, with method body just do delegation
try (InputStream sourceBytes = rawType.getClassLoader().getResourceAsStream(Type.getInternalName(rawType) + ".class")) {
ClassReader classReader = new ClassReader(sourceBytes);
classReader.accept(new ClassVisitor(Opcodes.ASM5) {
// Only need to visit @Path at the class level if we are inspecting the user handler class
private final boolean inspectDelegate = delegateType.equals(inspectType);
private boolean visitedPath = !inspectDelegate;
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
super.visit(version, access, name, signature, superName, interfaces);
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
// Copy the class annotation if it is @Path. Only do it for one time
Type type = Type.getType(desc);
if (inspectDelegate && type.equals(Type.getType(Path.class))) {
visitedPath = true;
AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(desc, visible);
return new AnnotationVisitor(Opcodes.ASM5, annotationVisitor) {
@Override
public void visit(String name, Object value) {
// "value" is the key for the Path annotation string.
if (name.equals("value")) {
super.visit(name, pathPrefix + value.toString());
} else {
super.visit(name, value);
}
}
};
} else {
return super.visitAnnotation(desc, visible);
}
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
// annotation.
if (!visitedPath) {
String pathDesc = Type.getType(Path.class).getDescriptor();
AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(pathDesc, true);
annotationVisitor.visit("value", pathPrefix);
annotationVisitor.visitEnd();
visitedPath = true;
}
// Copy the method if it is public and annotated with one of the HTTP request method
MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
if (!Modifier.isPublic(access)) {
return mv;
}
return new HandlerMethodVisitor(delegateType, mv, desc, signature, access, name, exceptions, classType, classWriter, preservedClasses);
}
}, ClassReader.SKIP_DEBUG);
}
}
Aggregations