List of usage examples for org.objectweb.asm.tree ClassNode ClassNode
public ClassNode()
From source file:org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.bind.MethodBindVisitorTestCase.java
License:Apache License
private static ClassNode type() { ClassNode node = new ClassNode(); node.name = "my/Component"; return node; }
From source file:org.apache.felix.ipojo.manipulator.metadata.annotation.visitor.ComponentVisitorTestCase.java
License:Apache License
private ClassNode clazz() { ClassNode node = new ClassNode(); node.name = "my/Component"; return node; }
From source file:org.apache.felix.scrplugin.helper.ClassModifier.java
License:Apache License
/** * Add bind/unbind methods//w w w. ja v a 2 s . c om * @param className The class name in which the methods are injected * @param referenceName Name of the reference * @param fieldName Name of the field * @param typeName Name of the type * @param createBind Name of the bind method or null * @param createUnbind Name of the unbind method or null * @param outputDirectory Output directory where the class file is stored * @throws SCRDescriptorException */ public static void addMethods(final String className, final String referenceName, final String fieldName, final String typeName, final boolean createBind, final boolean createUnbind, final ClassLoader classLoader, final String outputDirectory, final Log logger) throws SCRDescriptorException { // now do byte code manipulation final String fileName = outputDirectory + File.separatorChar + className.replace('.', File.separatorChar) + ".class"; final ClassNode cn = new ClassNode(); try { final ClassReader reader = new ClassReader(new FileInputStream(fileName)); reader.accept(cn, 0); // For target Java7 and above use: ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES final int mask = (cn.version > 50 ? ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES : 0); final ClassWriter writer = new ClassWriter(mask) { @Override protected String getCommonSuperClass(final String type1, final String type2) { Class<?> c, d; try { c = classLoader.loadClass(type1.replace('/', '.')); d = classLoader.loadClass(type2.replace('/', '.')); } catch (final Exception e) { throw new RuntimeException(e.toString(), e); } if (c.isAssignableFrom(d)) { return type1; } if (d.isAssignableFrom(c)) { return type2; } if (c.isInterface() || d.isInterface()) { return "java/lang/Object"; } do { c = c.getSuperclass(); } while (!c.isAssignableFrom(d)); return c.getName().replace('.', '/'); } }; cn.accept(writer); if (createBind) { logger.debug("Adding bind " + className + " " + fieldName); createMethod(writer, className, referenceName, fieldName, typeName, true); } if (createUnbind) { logger.debug("Adding unbind " + className + " " + fieldName); createMethod(writer, className, referenceName, fieldName, typeName, false); } final FileOutputStream fos = new FileOutputStream(fileName); fos.write(writer.toByteArray()); fos.close(); } catch (final Exception e) { throw new SCRDescriptorException("Unable to add methods to " + className, typeName, e); } }
From source file:org.apache.felix.scrplugin.helper.ClassScanner.java
License:Apache License
/** * Scan a single class.//from ww w .java2 s . c o m */ private ClassDescription processClass(final Class<?> annotatedClass, final String location) throws SCRDescriptorFailureException, SCRDescriptorException { log.debug("Processing " + annotatedClass.getName()); try { // get the class file for ASM final String pathToClassFile = annotatedClass.getName().replace('.', '/') + ".class"; final InputStream input = project.getClassLoader().getResourceAsStream(pathToClassFile); final ClassReader classReader; try { classReader = new ClassReader(input); } finally { input.close(); } final ClassNode classNode = new ClassNode(); classReader.accept(classNode, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES); // create descriptions final List<ScannedAnnotation> annotations = extractAnnotation(classNode, annotatedClass); if (annotations.size() > 0) { // process annotations and create descriptions final ClassDescription desc = new ClassDescription(annotatedClass, location); aProcessor.process(new ScannedClass(annotations, annotatedClass), desc); log.debug("Found descriptions " + desc + " in " + annotatedClass.getName()); return desc; } } catch (final IllegalArgumentException ioe) { throw new SCRDescriptorException("Unable to scan class files: " + annotatedClass.getName() + " (Class file format probably not supported by ASM ?)", location, ioe); } catch (final IOException ioe) { throw new SCRDescriptorException("Unable to scan class files: " + annotatedClass.getName(), location, ioe); } return null; }
From source file:org.apache.felix.scrplugin.tags.qdox.QDoxJavaClassDescription.java
License:Apache License
/** * @see org.apache.felix.scrplugin.tags.ModifiableJavaClassDescription#addMethods(java.lang.String, java.lang.String, boolean, boolean) *//* w ww.ja va2 s. c o m*/ public void addMethods(final String propertyName, final String className, final boolean createBind, final boolean createUnbind) throws SCRDescriptorException { // now do byte code manipulation final String targetDirectory = this.manager.getOutputDirectory(); final String fileName = targetDirectory + File.separatorChar + this.getName().replace('.', File.separatorChar) + ".class"; final ClassNode cn = new ClassNode(); try { final ClassReader reader = new ClassReader(new FileInputStream(fileName)); reader.accept(cn, 0); final ClassWriter writer = new ClassWriter(0); // remove existing implementation von previous builds final ClassAdapter adapter = new ClassAdapter(writer) { protected final String bindMethodName = "bind" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); protected final String unbindMethodName = "unbind" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); protected final String description = "(L" + className.replace('.', '/') + ";)V"; /** * @see org.objectweb.asm.ClassAdapter#visitMethod(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[]) */ public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if (createBind && name.equals(bindMethodName) && description.equals(desc)) { return null; } if (createUnbind && name.equals(unbindMethodName) && description.equals(desc)) { return null; } return super.visitMethod(access, name, desc, signature, exceptions); } }; cn.accept(adapter); if (createBind) { this.createMethod(writer, propertyName, className, true); } if (createUnbind) { this.createMethod(writer, propertyName, className, false); } final FileOutputStream fos = new FileOutputStream(fileName); fos.write(writer.toByteArray()); fos.close(); } catch (Exception e) { throw new SCRDescriptorException("Unable to add methods to " + this.getName(), className, 0, e); } }
From source file:org.apache.flink.api.java.sca.UdfAnalyzerUtils.java
License:Apache License
/** * @return array that contains the method node and the name of the class where * the method node has been found//from w ww.ja v a2s .c om */ @SuppressWarnings("unchecked") public static Object[] findMethodNode(String internalClassName, String name, String desc) { InputStream stream = null; try { // iterate through hierarchy and search for method node / // class that really implements the method while (internalClassName != null) { stream = Thread.currentThread().getContextClassLoader() .getResourceAsStream(internalClassName.replace('.', '/') + ".class"); ClassReader cr = new ClassReader(stream); final ClassNode cn = new ClassNode(); cr.accept(cn, 0); for (MethodNode mn : (List<MethodNode>) cn.methods) { if (mn.name.equals(name) && mn.desc.equals(desc)) { return new Object[] { mn, cr.getClassName() }; } } internalClassName = cr.getSuperName(); } } catch (IOException e) { throw new IllegalStateException("Method '" + name + "' could not be found", e); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { // best effort cleanup } } } throw new IllegalStateException("Method '" + name + "' could not be found"); }
From source file:org.apache.maven.diagrams.connectors.classes.asm_parser.AsmClassDataSource.java
License:Apache License
@SuppressWarnings("unchecked") public ClassModel translateToClassModel(Class c) throws ClassDataSourceException { ClassReader classReader;// w w w . java 2 s . com try { classReader = new ClassReader( AsmClassDataSource.class.getResourceAsStream("/" + c.getName().replace(".", "/") + ".class")); } catch (IOException e) { throw new ClassDataSourceException("Cannot instante class reader for:" + c.getName(), e); } ClassNode classNode = new ClassNode(); classReader.accept(classNode, 0); return translateClassNodeToClassModel(classNode); }
From source file:org.apache.maven.diagrams.connectors.classes.asm_parser.AsmClassDataSource.java
License:Apache License
public ClassModel translateToClassModel(InputStream is) throws ClassDataSourceException { try {/*from w w w .j av a 2 s. c o m*/ ClassReader classReader = new ClassReader(is); ClassNode classNode = new ClassNode(); classReader.accept(classNode, 0); return translateClassNodeToClassModel(classNode); } catch (IOException e) { throw new ClassDataSourceException("Cannot instante class reader for given inputStream.", e); } }
From source file:org.apache.maven.diagrams.connectors.classes.asm_parser.AsmClassDataSource.java
License:Apache License
public ClassModel translateToClassModel(String className) throws ClassDataSourceException { try {//from ww w. j a va 2s. c o m ClassReader classReader = new ClassReader( AsmClassDataSource.class.getResourceAsStream("/" + className.replace(".", "/") + ".class")); ClassNode classNode = new ClassNode(); classReader.accept(classNode, 0); return translateClassNodeToClassModel(classNode); } catch (IOException e) { throw new ClassDataSourceException("Cannot instante class reader for:" + className, e); } }
From source file:org.apache.maven.diagrams.connectors.classes.asm_parser.AsmClassDataSource.java
License:Apache License
public ClassModel translateToClassModel(ClassLoader classLoader, String className) throws ClassDataSourceException { try {// w w w .j a va 2 s . c om String res = className.replace(".", "/") + ".class"; InputStream is = classLoader.getResourceAsStream(res); if (is == null) throw new ClassDataSourceException("Cannot find resource :" + res); ClassReader classReader = new ClassReader(is); ClassNode classNode = new ClassNode(); classReader.accept(classNode, 0); return translateClassNodeToClassModel(classNode); } catch (IOException e) { throw new ClassDataSourceException("Cannot instante class reader for:" + className, e); } }