Example usage for org.objectweb.asm.tree ClassNode ClassNode

List of usage examples for org.objectweb.asm.tree ClassNode ClassNode

Introduction

In this page you can find the example usage for org.objectweb.asm.tree ClassNode ClassNode.

Prototype

public ClassNode() 

Source Link

Document

Constructs a new ClassNode .

Usage

From source file:org.eclipse.pde.api.tools.internal.builder.ReferenceExtractor.java

License:Open Source License

/**
 * Constructor/*from  www.  java 2  s  .c o m*/
 * 
 * @param type
 * @param collector
 * @param referenceKinds
 * @param tracker
 */
protected ReferenceExtractor(IApiType type, Set<Reference> collector, int referenceKinds,
        FieldTracker tracker) {
    super(Opcodes.ASM5, new ClassNode());
    fType = type;
    this.collector = collector;
    fReferenceKinds = referenceKinds;
    fIsVisitMembers = (VISIT_MEMBERS_MASK & fReferenceKinds) > 0;
    fieldtracker = tracker;
}

From source file:org.eclipse.pde.api.tools.internal.model.TypeStructureBuilder.java

License:Open Source License

/**
 * Builds a type structure with the given .class file bytes in the specified
 * API component./* w w  w . j  ava  2 s.co  m*/
 * 
 * @param bytes class file bytes
 * @param component originating API component
 * @param file associated class file
 * @return
 */
public static IApiType buildTypeStructure(byte[] bytes, IApiComponent component, IApiTypeRoot file) {
    TypeStructureBuilder visitor = new TypeStructureBuilder(new ClassNode(), component, file);
    try {
        ClassReader classReader = new ClassReader(bytes);
        classReader.accept(visitor, ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES);
    } catch (ArrayIndexOutOfBoundsException e) {
        logAndReturn(file, e);
        return null;
    } catch (IllegalArgumentException iae) {
        // thrown from ASM 5.0 for bad bytecodes
        return logAndReturn(file, iae);
    }
    return visitor.fType;
}

From source file:org.eclipse.pde.api.tools.internal.model.TypeStructureBuilder.java

License:Open Source License

/**
 * Builds a type structure with the given .class file bytes in the specified
 * API component./*  www.  j  av  a 2s.co  m*/
 * 
 * @param bytes class file bytes
 * @param component originating API component
 * @param file associated class file
 * @return
 */
public static void setEnclosingMethod(IApiType enclosingType, ApiType currentAnonymousLocalType) {
    IApiTypeRoot typeRoot = enclosingType.getTypeRoot();
    if (typeRoot instanceof AbstractApiTypeRoot) {
        AbstractApiTypeRoot abstractApiTypeRoot = (AbstractApiTypeRoot) typeRoot;
        EnclosingMethodSetter visitor = new EnclosingMethodSetter(new ClassNode(),
                currentAnonymousLocalType.getName());
        try {
            ClassReader classReader = new ClassReader(abstractApiTypeRoot.getContents());
            classReader.accept(visitor, ClassReader.SKIP_FRAMES);
        } catch (ArrayIndexOutOfBoundsException e) {
            ApiPlugin.log(e);
        } catch (CoreException e) {
            // bytes could not be retrieved for abstractApiTypeRoot
            ApiPlugin.log(e);
        }
        if (visitor.found) {
            currentAnonymousLocalType.setEnclosingMethodInfo(visitor.name, visitor.signature);
        }
    }
}

From source file:org.eclipse.tycho.zipcomparator.internal.ClassfileComparator.java

License:Open Source License

private String disassemble(byte[] bytes) {
    ClassReader reader = new ClassReader(bytes);
    ClassNode clazz = new ClassNode();
    reader.accept(clazz, Opcodes.ASM5 | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);

    // inner class list gets reordered during pack200 normalization
    if (clazz.innerClasses != null) {
        List<InnerClassNode> sorted = new ArrayList<>(clazz.innerClasses);
        Collections.sort(sorted, new Comparator<InnerClassNode>() {
            @Override//from   w  w  w.  j a va2s  .com
            public int compare(InnerClassNode o1, InnerClassNode o2) {
                return o1.name.compareTo(o2.name);
            }
        });
        clazz.innerClasses = sorted;
    }

    // rendering human-readable bytecode is an eyecandy, we can compare ClassNodes directly

    StringWriter buffer = new StringWriter();
    PrintWriter writer = new PrintWriter(buffer);
    clazz.accept(new TraceClassVisitor(writer));
    writer.flush();
    writer.close();
    return buffer.toString();
}

From source file:org.evosuite.classpath.ResourceList.java

License:Open Source License

private static boolean isClassAnInterface(InputStream input) throws IOException {
    try {//from   w w w  .j  av a2s  .  c  o m
        ClassReader reader = new ClassReader(input);
        ClassNode cn = new ClassNode();
        reader.accept(cn, ClassReader.SKIP_FRAMES);
        return (cn.access & Opcodes.ACC_INTERFACE) == Opcodes.ACC_INTERFACE;
    } finally {
        input.close(); //VERY IMPORTANT, as ASM does not close the stream
    }
}

From source file:org.evosuite.classpath.ResourceList.java

License:Open Source License

/**
 * Returns {@code true} if the class is deprecated; returns {@code false} otherwise.
 * /* w  w w .  ja v  a 2s . com*/
 * @param input the input stream
 * @return {@code true} if the class is deprecated, {@code false} otherwise
 * @throws IOException if an error occurs while reading the input stream
 */

private static boolean isClassDeprecated(InputStream input) throws IOException {
    try {
        ClassReader reader = new ClassReader(input);
        ClassNode cn = new ClassNode();
        reader.accept(cn, ClassReader.SKIP_FRAMES);
        return (cn.access & Opcodes.ACC_DEPRECATED) == Opcodes.ACC_DEPRECATED;
    } finally {
        input.close(); //VERY IMPORTANT, as ASM does not close the stream
    }
}

From source file:org.evosuite.classpath.ResourceList.java

License:Open Source License

/**
 * Returns {@code true} if there is at least one public method in the class; returns {@code false} otherwise.
 * /*from www  . ja v a  2s . c o  m*/
 * @param input the input stream
 * @return {@code true} if there is at least one public method in the class, {@code false} otherwise
 * @throws IOException if an error occurs while reading the input stream
 */
private static boolean isClassTestable(InputStream input) throws IOException {
    try {
        ClassReader reader = new ClassReader(input);
        ClassNode cn = new ClassNode();
        reader.accept(cn, ClassReader.SKIP_FRAMES);
        @SuppressWarnings("unchecked")
        List<MethodNode> l = cn.methods;
        for (MethodNode m : l) {
            if ((m.access & Opcodes.ACC_PUBLIC) == Opcodes.ACC_PUBLIC
                    || (m.access & Opcodes.ACC_PROTECTED) == Opcodes.ACC_PROTECTED
                    || (m.access & Opcodes.ACC_PRIVATE) == 0 /* default */ ) {
                return true;
            }
        }
        return false;
    } finally {
        input.close(); //VERY IMPORTANT, as ASM does not close the stream
    }
}

From source file:org.evosuite.instrumentation.BytecodeInstrumentation.java

License:Open Source License

private byte[] handleCarving(String className, ClassWriter writer) {
    ClassReader cr = new ClassReader(writer.toByteArray());
    ClassNode cn2 = new ClassNode();
    cr.accept(cn2, ClassReader.EXPAND_FRAMES);

    this.testCarvingInstrumenter.transformClassNode(cn2, className);
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    cn2.accept(cw);/*from   ww w .  j  a v  a2  s . co m*/

    if (logger.isDebugEnabled()) {
        final StringWriter sw = new StringWriter();
        cn2.accept(new TraceClassVisitor(new PrintWriter(sw)));
        logger.debug("test carving instrumentation result:\n{}", sw);
    }

    return cw.toByteArray();
}

From source file:org.evosuite.instrumentation.DescriptorMapping.java

License:Open Source License

private boolean isOutsideMethod(String className, String methodName, String desc) {
    Set<String> visited = new HashSet<String>();
    Queue<String> parents = new LinkedList<String>();
    parents.add(className);//from   www. ja  v  a 2 s.  c  om

    while (!parents.isEmpty()) {
        String name = parents.poll();
        if (name == null)
            continue;

        visited.add(name);
        logger.info("Visiting class {} while looking for source of {}.{}", name, className, methodName);
        ClassReader reader;
        try {
            reader = new ClassReader(name);
            ClassNode parent = new ClassNode();
            reader.accept(parent, ClassReader.EXPAND_FRAMES);

            boolean isInside = isInside(parent.name);

            //boolean isInside = parent.name.startsWith(Properties.PROJECT_PREFIX.replace(".",
            //                                                                            "/"))
            //        || (!Properties.TARGET_CLASS_PREFIX.isEmpty() && parent.name.startsWith(Properties.TARGET_CLASS_PREFIX.replace(".",
            //                                                                                                                                   "/")));

            logger.info("Checking {}", parent.name);
            for (Object o : parent.methods) {
                MethodNode mn2 = (MethodNode) o;
                if (mn2.name.equals(methodName) && mn2.desc.equals(desc)) {
                    if (!isInside) {
                        logger.info("Method {} was defined outside the test package", name);
                        return true;
                    } else {
                        logger.info("Method {} was defined outside the test package", name);
                        //return false;
                    }
                }
            }

            for (Object o : parent.interfaces) {
                String par = (String) o;
                if (!visited.contains(par) && !parents.contains(par)) {
                    parents.add(par);
                }
            }
            if (!visited.contains(parent.superName) && !parents.contains(parent.superName)) {
                parents.add(parent.superName);
            }
        } catch (IOException e) {
            logger.info("Error reading class {}", name);
        }
    }

    return false;
}

From source file:org.evosuite.instrumentation.DescriptorMapping.java

License:Open Source License

private String transformMethodName(String className, String methodName, String desc, String transformedDesc) {
    Set<String> visited = new HashSet<String>();
    Queue<String> parents = new LinkedList<String>();
    parents.add(className);/*from w  ww .j  av  a2  s .  c o m*/

    while (!parents.isEmpty()) {
        String name = parents.poll();
        if (name == null)
            continue;

        visited.add(name);
        logger.info("Visiting class {} while looking for name clashes of {}.{}{}", name, className, methodName,
                transformedDesc);
        ClassReader reader;
        try {
            reader = new ClassReader(name);
            ClassNode parent = new ClassNode();
            reader.accept(parent, ClassReader.EXPAND_FRAMES);

            if (originalDesc.containsKey(className + "." + methodName + transformedDesc)) {
                logger.info("Method {} has conflicting transformed method", methodName);
                return methodName + "_transformed" + (id++);
            }

            for (Object o : parent.methods) {
                MethodNode mn2 = (MethodNode) o;
                //logger.info("Checking " + parent.name + "." + mn2.name + mn2.desc);
                if (mn2.name.equals(methodName) && mn2.desc.equals(transformedDesc)) {
                    logger.info("Method {} has conflicting method", methodName);
                    if (methodName.equals("<init>"))
                        return null; // TODO: This should be a bit nicer
                    return methodName + "_transformed" + (id++);
                }
            }

            for (Object o : parent.interfaces) {
                String par = (String) o;
                if (!visited.contains(par) && !parents.contains(par)) {
                    parents.add(par);
                }
            }
            if (!visited.contains(parent.superName) && !parents.contains(parent.superName)) {
                parents.add(parent.superName);
            }
        } catch (IOException e) {
            logger.info("Error reading class {}", name);
        }
    }

    return methodName;
}