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.qkit.core.asm.adapters.AddInterfaceAdapter.java

License:Open Source License

public AddInterfaceAdapter(final ClassVisitor cv, final String... interfacesToAdd) {
    super(ASM4, new ClassNode());
    next = cv;/*from ww  w. ja  va  2s .c  o  m*/

    this.interfacesToAdd = interfacesToAdd;
}

From source file:org.qkit.core.asm.adapters.AddMethodAdapter.java

License:Open Source License

public AddMethodAdapter(final ClassVisitor cv, final String fieldName, final String fieldDescriptor,
        final String getterName, final int varInsn, final int retInsn) {
    super(ASM4, new ClassNode());
    next = cv;//from ww  w. ja v  a  2  s  .  co  m

    this.fieldName = fieldName;
    this.getterName = getterName;
    this.varInsn = varInsn;
    this.retInsn = retInsn;
    this.fieldDescriptor = fieldDescriptor;
}

From source file:org.qkit.core.asm.adapters.ChangeMethodNameAdapter.java

License:Open Source License

public ChangeMethodNameAdapter(final ClassVisitor cv, String targetMethodName, String wantedMethodName) {
    super(ASM4, new ClassNode());
    next = cv;/*from w w  w .j a  v a  2  s  .c o  m*/
    this.targetMethodName = targetMethodName;
    this.wantedMethodName = wantedMethodName;
}

From source file:org.qkit.core.asm.adapters.ChangeSuperclassAdapter.java

License:Open Source License

public ChangeSuperclassAdapter(final ClassVisitor cv, final String superClass) {
    super(ASM4, new ClassNode());
    next = cv;//w  w  w. j  a  v a 2 s .c o  m

    this.superClass = superClass;
}

From source file:org.qkit.core.updater.JarConstruct.java

License:Open Source License

/**
 * Loads all classes from the given JAR file and stores them into a HashMap for future use.
 *
 * @return True if it was successful, False if it was not.
 *///from   w w  w. ja va 2s .  c o m
@SuppressWarnings("resource")
public boolean loadClasses() {
    short count = 0;

    try {
        //Startup
        p("---------------------------------------------------------------------");
        p("--------------------      Jar Loader     ----------------------------");
        p("File: " + jarPath);

        p("JC Hash: " + getClass().hashCode());

        JarFile jf;

        //Referencing the JAR file.
        if (jarUrl != null) {

            System.out.println(jarUrlPath.toString());

            //Connect to the JAR directly online
            JarURLConnection u = (JarURLConnection) jarUrlPath.openConnection();

            //Get the JarFile representation from the JarURLConnection
            jf = u.getJarFile();

        } else {

            //If the if statement leads here, then the JAR is being run locally
            jf = new JarFile(jarPath);

        }

        //Make sure that a non-null value was passed in from either constructor
        assert jarPath != null || jarUrl != null : "Jar loader failure!";

        //Print out the size of the JarFile
        p("JarFile Size = " + jf.size());
        p("-----------------------------------------------------------------");

        //Referencing the entries.
        Enumeration<? extends JarEntry> en = jf.entries();

        //Looping through the elements (the entries).
        while (en.hasMoreElements()) {

            //The entry to work with.
            JarEntry entry = en.nextElement();

            //Grabbing solely the class files
            if (entry.getName().endsWith(".class")) {

                //Count out the entries
                ++count;

                //Print out the entry
                p("Entry " + count + ") " + entry.getName());
                p(" -> Decompressed Size = " + entry.getSize() + " Bytes" + "\n");

                //ClassReader retrieves the bytes from a given entry.
                ClassReader cr = new ClassReader(jf.getInputStream(entry));

                //Creating a new ClassNode to act as a representative of a class.
                ClassNode cn = new ClassNode();

                //Delegating all method calls and data from ClassReader to ClassNode.
                //Think of it as data from 'cr' are being entrusted or put into 'cn' (such as the class bytes).
                cr.accept(cn, 0);

                //Put it into the local package's HashMap as a ClassNode.
                loadedClassNodes.put(cn.name, cn);
            }
        }

        System.out.println(count + " classes were loaded and stored!");
        p("-----------------------------------------------------------------");
        p("-----------------------------------------------------------------");

        ConsoleLogger.printlnInfo("Load successful.");

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:org.rascalmpl.library.lang.java.m3.internal.JarConverter.java

License:Open Source License

@SuppressWarnings("unchecked")
public void convert(ISourceLocation jarLoc, IEvaluatorContext ctx) {

    this.loc = jarLoc;
    this.jarFile = extractJarName(jarLoc);
    this.ClassFile = extractClassName(jarLoc);
    this.LogPath = this.ClassFile.replace(".class", "");
    String packageName;//  www  .j a v a 2  s. c  o m

    // System.out.println(this.ClassFile);
    packageName = LogPath.substring(0, LogPath.lastIndexOf("/"));
    if (this.LogPath.contains("$")) {
        this.LogPath = LogPath.replace("$", "/");
    }
    try {
        ClassReader cr = new ClassReader(URIResolverRegistry.getInstance().getInputStream(jarLoc));
        ClassNode cn = new ClassNode();

        cr.accept(cn, ClassReader.SKIP_DEBUG);

        this.className = cn.name.replace("$", "/");
        classIsEnum = false;
        if ((cn.access & Opcodes.ACC_INTERFACE) != 0)
            classScheme = "java+interface";
        else if ((cn.access & Opcodes.ACC_ENUM) != 0) {
            classScheme = "java+enum";
            classIsEnum = true;
        } else
            this.classScheme = "java+class";

        this.insert(this.containment, values.sourceLocation(classScheme, "", "/" + className),
                values.sourceLocation("java+compilationUnit", "", "/jar:///" + jarFile));
        this.insert(this.containment, values.sourceLocation("java+package", "", "/" + packageName),
                values.sourceLocation("java+compilationUnit", "", "/jar:///" + jarFile));
        this.insert(this.containment, values.sourceLocation("java+compilationUnit", "", "/jar:///" + jarFile),
                values.sourceLocation("java+class", "", "/" + LogPath));

        // <|java+package:///Main|,|java+compilationUnit:///src/Main/BaseInt.java|>,

        this.insert(this.declarations, values.sourceLocation(classScheme, "", "/" + className),
                values.sourceLocation(jarFile + "!" + ClassFile));

        if (cn.superName != null && !(cn.superName.equalsIgnoreCase("java/lang/Object")
                || cn.superName.equalsIgnoreCase("java/lang/Enum"))) {
            this.insert(this.extendsRelations, values.sourceLocation(classScheme, "", "/" + className),
                    values.sourceLocation(classScheme, "", cn.superName));
        }

        for (int fs = 0; fs < 15; fs++) {
            if ((cn.access & (0x0001 << fs)) != 0) {
                IConstructor cons = mapFieldAccesCode(0x0001 << fs, CLASSE);
                if (cons != null)
                    this.insert(this.modifiers, values.sourceLocation(classScheme, "", "/" + className), cons);
            }
        }

        // Deprecated method emit type annotation dependency Deprecated.
        if ((cn.access & 0x20000) == 0x20000)
            this.insert(this.annotations, values.sourceLocation(classScheme, "", "/" + className),
                    values.sourceLocation("java+interface", "", "/java/lang/Deprecated"));

        // @implements={<|java+class:///m3startv2/viaInterface|,|java+interface:///m3startv2/m3Interface|>},
        for (int i = 0; i < cn.interfaces.size(); ++i) {
            String iface = (String) cn.interfaces.get(i);
            // System.out.println(iface);
            this.insert(this.implementsRelations, values.sourceLocation(classScheme, "", "/" + className),
                    values.sourceLocation("java+interface", "", "/" + iface));
        }

        for (int fs = 0; fs < cn.innerClasses.size(); fs++) {
            InnerClassNode a = (InnerClassNode) cn.innerClasses.get(fs);
            String parsedName = a.name.replace("$", "/");
            this.insert(this.containment, values.sourceLocation(classScheme, "", "/" + className),
                    values.sourceLocation(classScheme, "", "/" + parsedName));
        }

        emitMethods(cn.methods);
        emitFields(cn.fields);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        throw new RuntimeException("Should not happen", e);
    }
}

From source file:org.rascalmpl.library.lang.jvm.transform.Rascalify.java

License:Open Source License

@SuppressWarnings("unchecked")
public static void deserializeToDisk(ISourceLocation source, ISourceLocation destination, IString moduleName) {
    try {//from w w w . j a v  a2 s.c o m
        ClassNode cn = new ClassNode();
        ClassReader cr = new ClassReader(_resolver.getInputStream(source.getURI()));
        cr.accept(cn, 0);

        OutputStreamWriter writer = new OutputStreamWriter(
                _resolver.getOutputStream(destination.getURI(), false));
        writer.write("module " + moduleName.getValue() + "\n\n");
        writer.write("import experiments::JVMBytecode::Opcodes;\n");
        writer.write("import experiments::JVMBytecode::SerializeClass;\n\n");
        writer.write("public Class generate" + moduleName.getValue() + "Class() {\n");
        writer.write("\treturn class(" + cn.version + ", " + cn.access + ", \"" + escape(cn.name) + "\", "
                + checkNull(cn.signature) + ", " + checkNull(cn.superName) + ",\n\t\t");
        writeStrings(writer, cn.interfaces);
        writer.write(", " + checkNull(cn.sourceFile) + ", " + checkNull(cn.sourceDebug) + ", "
                + checkNull(cn.outerClass) + ", " + checkNull(cn.outerMethod) + ", "
                + checkNull(cn.outerMethodDesc) + ",\n\t\t");
        writerInnerClasses(cn, writer);
        writer.write(",\n\t\t");
        writeFields(cn, writer);
        writer.write(",\n\t\t");
        writeMethods(cn, writer);
        writer.write("\n\t);\n");
        writer.write("}");
        writer.close();
    } catch (FileNotFoundException e) {
        throw RuntimeExceptionFactory.pathNotFound(source, null, null);
    } catch (IOException e) {
        throw RuntimeExceptionFactory.io(ValueFactoryFactory.getValueFactory().string(e.getMessage()), null,
                null);
    }
}

From source file:org.sonar.java.bytecode.loader.SquidClassLoaderTest.java

License:Open Source License

@Test
public void test_loading_class() {
    SquidClassLoader classLoader = new SquidClassLoader(
            Collections.singletonList(new File("target/test-classes")));
    String className = getClass().getCanonicalName();
    byte[] bytes = classLoader.getBytesForClass(className);
    assertThat(bytes).isNotNull();/*from   w w  w  . j  a v  a2 s .  co m*/
    ClassReader cr = new ClassReader(bytes);
    ClassNode classNode = new ClassNode();
    cr.accept(classNode, 0);
    assertThat(classNode.name).isEqualTo("org/sonar/java/bytecode/loader/SquidClassLoaderTest");
}

From source file:org.sonar.java.bytecode.loader.SquidClassLoaderTest.java

License:Open Source License

@Test
public void test_loading_java9_class() throws Exception {
    SquidClassLoader classLoader = new SquidClassLoader(
            Collections.singletonList(new File("src/test/files/bytecode/java9/bin")));
    byte[] bytes = classLoader.getBytesForClass("org.test.Hello9");
    assertThat(bytes).isNotNull();//from ww  w  .j  a  v a2  s  .  c  o m
    ClassReader cr = new ClassReader(bytes);
    ClassNode classNode = new ClassNode();
    cr.accept(classNode, 0);
    assertThat(classNode.version).isEqualTo(Opcodes.V9);
    classLoader.close();
}

From source file:org.sonar.java.bytecode.loader.SquidClassLoaderTest.java

License:Open Source License

@Test
public void test_loading_java10_class() throws Exception {
    SquidClassLoader classLoader = new SquidClassLoader(
            Collections.singletonList(new File("src/test/files/bytecode/java10/bin")));
    byte[] bytes = classLoader.getBytesForClass("org.foo.A");
    assertThat(bytes).isNotNull();//w  ww.  j  av a 2  s. c  om
    ClassReader cr = new ClassReader(bytes);
    ClassNode classNode = new ClassNode();
    cr.accept(classNode, 0);
    assertThat(classNode.version).isEqualTo(Opcodes.V10);
    classLoader.close();
}