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:edu.umd.cs.findbugs.classfile.engine.asm.ClassNodeAnalysisEngine.java

License:Open Source License

@Override
public ClassNode analyze(IAnalysisCache analysisCache, ClassDescriptor descriptor)
        throws CheckedAnalysisException {
    ClassReader classReader = analysisCache.getClassAnalysis(ClassReader.class, descriptor);

    ICodeBaseEntry entry = analysisCache.getClassPath().lookupResource(descriptor.toResourceName());

    // One of the less-than-ideal features of ASM is that
    // invalid classfile format is indicated by a
    // random runtime exception rather than something
    // indicative of the real problem.
    try {//from ww  w. j av  a  2s .  c  o m
        ClassNode cn = new ClassNode();
        classReader.accept(cn, 0);
        return cn;
    } catch (RuntimeException e) {
        throw new InvalidClassFileFormatException(descriptor, entry, e);
    }
}

From source file:fi.luontola.buildtest.AsmUtils.java

License:Open Source License

public static ClassNode readClass(InputStream in) throws IOException {
    ClassNode cn = new ClassNode();
    ClassReader cr = new ClassReader(in);
    cr.accept(cn, 0);/*from w  w  w. j  a  v  a2s .co  m*/
    return cn;
}

From source file:forgefix.ForgeFixTransformer.java

License:LGPL

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null)
        return bytes;

    if (transformedName.equals("com.xcompwiz.mystcraft.world.gen.structure.ComponentVillageArchivistHouse")
            || (transformedName.equals(/*from w  w  w  .  jav a2  s  .  c o  m*/
                    "com.xcompwiz.mystcraft.world.gen.structure.ComponentScatteredFeatureSmallLibrary"))) {
        ClassNode classNode = new ClassNode();
        ClassReader classReader = new ClassReader(bytes);

        classReader.accept(classNode, 0);

        MethodNode vanillaMethod = null;
        for (MethodNode method : classNode.methods) {
            if (method.name.equals("generateStructureLectern")) {
                vanillaMethod = method;
            }
        }

        MethodNode moddedMethod = new MethodNode(ASM4, ACC_PROTECTED, "generateStructureLectern",
                getMethodDescriptor(BOOLEAN_TYPE), null, null);
        moddedMethod.desc = "(Lnet/minecraft/world/World;Lnet/minecraft/world/gen/structure/StructureBoundingBox;Ljava/util/Random;IIIIILnet/minecraftforge/common/ChestGenHooks;)Z";
        moddedMethod.instructions.add(new InsnNode(ICONST_0));
        moddedMethod.instructions.add(new InsnNode(IRETURN));

        if (moddedMethod != null) {
            classNode.methods.remove(vanillaMethod);
            moddedMethod.accept(classNode);

            ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
            classNode.accept(writer);
            return writer.toByteArray();
        }
    }

    return bytes;
}

From source file:fr.theshark34.feelcraft.FeelcraftClassTransformer.java

License:Apache License

/**
 * Patchs the Minecraft class/* w w w  .  ja  va  2  s  .co m*/
 * 
 * @param name
 *            The class name
 * @param bytes
 *            The initial class bytes
 * @param obfuscated
 *            If the class is obfuscated
 * @return The modified bytes
 */
public byte[] patchClassASM(String name, byte[] bytes, boolean obfuscated) {
    // Starting the pre init
    Feelcraft.preInit();

    // Printing messages
    logger.info("[Feelcraft] Starting patching Minecraft");
    logger.info("[Feelcraft] Target class : " + name);

    // Getting the target method if the game is obfuscated or not
    String targetMethod;
    if (obfuscated)
        targetMethod = "ag";
    else
        targetMethod = "startGame";

    // Getting the class nod and reading the initial class
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);

    // Just a boolean that will be true if the patch will be done to check
    // if it was done
    boolean patchDone = false;

    // Getting the class methods
    Iterator<MethodNode> methods = classNode.methods.iterator();

    // For each method in the class
    while (methods.hasNext()) {
        // Getting the method
        MethodNode m = methods.next();

        // If the method is our target method
        if ((m.name.equals(targetMethod) && m.desc.equals("()V"))) {
            // Printing a message
            logger.info("[Feelcraft] Patching method : " + m.name + m.desc);

            // Insert a line that will call Feelcraft.start at the top of
            // the method
            m.instructions.insertBefore(m.instructions.getFirst(), new MethodInsnNode(Opcodes.INVOKESTATIC,
                    "fr/theshark34/feelcraft/Feelcraft", "start", "()V", false));

            // Setting patchDone to true
            patchDone = true;

            // Stopping the loop
            break;
        }
    }

    // If the patch wasn't done
    if (!patchDone)
        // Printing a warning message
        logger.warn("[Feelcraft] Warning the patch wasn't done ! Some things will not going to work !");

    // Writing the patched class
    logger.info("[Feelcraft] Writing the patched class");
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);

    // Printing a done message
    if (patchDone)
        logger.info("[Feelcraft] Successfully patched Minecraft !");
    else
        logger.info("[Feelcraft] Done");

    // Returning the modified bytes
    return writer.toByteArray();
}

From source file:gemlite.core.internal.asm.serialize.pdxserialize.PdxSFieldProcessor.java

License:Apache License

private List<FieldNode> findKeyNodeListByKeyClass(ClassNode domain) throws IOException {
    List<FieldNode> fieldNodeList = new ArrayList<FieldNode>();

    ClassNode key = null;/*from www  .  j  a v  a  2s.c o m*/

    for (Object o : domain.visibleAnnotations) {
        AnnotationNode an = (AnnotationNode) o;

        if (PdxConstants.AN_Key.equals(an.desc)) {
            Type t = (Type) an.values.get(1);
            InputStream keyClassIn = PdxSerializeHelper.class.getClassLoader()
                    .getResourceAsStream(t.getInternalName() + ".class");
            byte[] keyClassBytes = new byte[keyClassIn.available()];
            keyClassIn.read(keyClassBytes);
            keyClassIn.close();
            ClassReader cr = new ClassReader(keyClassBytes);
            key = new ClassNode();
            cr.accept(key, 0);
            break;
        }
    }

    if (key != null) {
        for (int i = 0; i < key.fields.size(); i++) {
            FieldNode fn = (FieldNode) key.fields.get(i);
            if (fn != null)
                fieldNodeList.add(fn);
        }
    }
    return fieldNodeList;
}

From source file:gemlite.core.util.GemliteHelper.java

License:Apache License

public final static ClassNode toClassNode(byte[] content) {
    ClassReader cr = new ClassReader(content);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);/*from   ww  w. j  av a 2  s. c o  m*/
    return cn;
}

From source file:gemlite.core.util.GemliteHelper.java

License:Apache License

public final static ClassNode toClassNode(InputStream in) {
    ClassReader cr = null;//w  w  w.  jav  a2  s .  c  o m
    try {
        cr = new ClassReader(in);
    } catch (IOException e) {
        LogUtil.getCoreLog().error("ClassReader new error", e);
        return null;
    }
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);
    return cn;
}

From source file:gemlite.core.util.GemliteHelper.java

License:Apache License

public final static void dumpBytecode(byte[] bytes, PrintWriter pw) {
    ClassReader cr = new ClassReader(bytes);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);/*from w w  w.  j  a  v a  2s. com*/
    dumpBytecode(cn, pw);
}

From source file:gemlite.maven.plugin.DomainMojoExecutor.java

License:Apache License

public void processClassFile(Path file) throws IOException {
    String autoSerialType = autoSerializeType;

    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file.toFile()));
    byte[] bytes = new byte[in.available()];
    in.read(bytes);/*from   w  w w .j  av  a2 s .  co  m*/
    in.close();
    ClassReader cr = new ClassReader(bytes);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);
    Set<ClassProcessor> used = new HashSet<>();
    if (DomainMojoHelper.log().isDebugEnabled())
        DomainMojoHelper.log().debug("Class name:" + cn.name + " Annotation:" + cn.visibleAnnotations);

    if (cn.visibleAnnotations == null)
        return;

    String desc = "";
    for (Object a : cn.visibleAnnotations) {
        AnnotationNode an = (AnnotationNode) a;

        desc = an.desc;
        ClassProcessor cp = null;
        if (DomainMojoConstant.AN_AutoSerialize.equals(desc)) {
            autoSerialType = getAutoSerializeType(an);
            cp = getClassProcessor(autoSerialType);
        } else
            cp = AnnotationProcessorFactory.getProcessor(desc);

        if (cp == null)
            continue;
        DomainMojoExecutor.usedProcessorSet.add(cp);
        if (DomainMojoHelper.log().isDebugEnabled())
            DomainMojoHelper.log().debug("Annotation:" + desc + " processor:" + cp);
        // ?
        if (cp != null && !used.contains(cp)) {
            if (DomainMojoHelper.log().isDebugEnabled())
                DomainMojoHelper.log().debug("Processor " + cp + " processing...");
            used.add(cp);
            long l1 = System.currentTimeMillis();
            cp.process(file.toFile(), bytes, cn);
            long l2 = System.currentTimeMillis();
            if (DomainMojoHelper.log().isDebugEnabled())
                DomainMojoHelper.log().debug("Processor " + cp + " processed, cost:" + (l2 - l1));
        }
    }
}

From source file:gemlite.maven.plugin.support.mapper.CreateMapperRegister.java

License:Apache License

/***
 * @param domain//from w w  w  . ja va 2s.co  m
 * @return
 * @throws IOException
 */

public synchronized void getIDomainRegistryClass(ClassNode domain) throws IOException {
    if (reg == null) {
        projectName = filterProjectName(projectName);
        String packagePath = DomainMapperHelper.defaultMpperPackage.replaceAll("\\.", "\\/");
        String internalName = packagePath + projectName + "$$$IDomainRegisterImpl";
        String path = projectPath + "/" + packagePath + "/";
        File f = new File(path);
        if (!f.exists())
            f.mkdirs();
        fileName = path + "/" + projectName + "$$$IDomainRegisterImpl.class";
        findTableNameAndRegionName(domain);
        reg = new ClassNode();
        reg.version = V1_7;
        reg.access = ACC_PUBLIC + ACC_SUPER;
        reg.name = internalName;
        reg.superName = "java/lang/Object";
        reg.interfaces.add(INTERFACE_NAME);
        reg.signature = null;
        addConstructFunction(reg);
        insn = implementInterface(reg);
    }
}