Search in sources :

Example 6 with FieldNode

use of org.objectweb.asm.tree.FieldNode in project pinpoint by naver.

the class ASMClassNodeAdapter method getField.

public ASMFieldNodeAdapter getField(final String fieldName, final String fieldDesc) {
    if (fieldName == null || this.classNode.fields == null) {
        return null;
    }
    final List<FieldNode> fields = this.classNode.fields;
    for (FieldNode fieldNode : fields) {
        if ((fieldNode.name != null && fieldNode.name.equals(fieldName)) && (fieldDesc == null || (fieldNode.desc != null && fieldNode.desc.equals(fieldDesc)))) {
            return new ASMFieldNodeAdapter(fieldNode);
        }
    }
    // find interface.
    final List<String> interfaces = this.classNode.interfaces;
    if (interfaces != null && interfaces.size() > 0) {
        for (String interfaceClassName : interfaces) {
            if (interfaceClassName == null) {
                continue;
            }
            final ASMClassNodeAdapter classNodeAdapter = ASMClassNodeAdapter.get(this.pluginContext, this.classLoader, interfaceClassName, true);
            if (classNodeAdapter != null) {
                final ASMFieldNodeAdapter fieldNode = classNodeAdapter.getField(fieldName, fieldDesc);
                if (fieldNode != null) {
                    return fieldNode;
                }
            }
        }
    }
    // find super class.
    if (this.classNode.superName != null) {
        final ASMClassNodeAdapter classNodeAdapter = ASMClassNodeAdapter.get(this.pluginContext, this.classLoader, this.classNode.superName, true);
        if (classNodeAdapter != null) {
            final ASMFieldNodeAdapter fieldNode = classNodeAdapter.getField(fieldName, fieldDesc);
            if (fieldNode != null) {
                return fieldNode;
            }
        }
    }
    return null;
}
Also used : FieldNode(org.objectweb.asm.tree.FieldNode)

Example 7 with FieldNode

use of org.objectweb.asm.tree.FieldNode in project drill by apache.

the class MergeAdapter method visitEnd.

@Override
public void visitEnd() {
    // add all the fields of the class we're going to merge.
    for (Iterator<?> it = classToMerge.fields.iterator(); it.hasNext(); ) {
        // Special handling for nested classes. Drill uses non-static nested
        // "inner" classes in some templates. Prior versions of Drill would
        // create the generated nested classes as static, then this line
        // would copy the "this$0" field to convert the static nested class
        // into a non-static inner class. However, that approach is not
        // compatible with plain-old Java compilation. Now, Drill generates
        // the nested classes as non-static inner classes. As a result, we
        // do not want to copy the hidden fields; we'll end up with two if
        // we do.
        FieldNode field = (FieldNode) it.next();
        if (!field.name.startsWith("this$")) {
            field.accept(this);
        }
    }
    // add all the methods that we to include.
    for (Iterator<?> it = classToMerge.methods.iterator(); it.hasNext(); ) {
        MethodNode mn = (MethodNode) it.next();
        if (mn.name.equals("<init>")) {
            continue;
        }
        String[] exceptions = new String[mn.exceptions.size()];
        mn.exceptions.toArray(exceptions);
        MethodVisitor mv = cv.visitMethod(mn.access | Modifier.FINAL, mn.name, mn.desc, mn.signature, exceptions);
        if (verifyBytecode) {
            mv = new CheckMethodVisitorFsm(api, mv);
        }
        mn.instructions.resetLabels();
        // mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv, new
        // SimpleRemapper("org.apache.drill.exec.compile.ExampleTemplate", "Bunky")));
        ClassSet top = set;
        while (top.parent != null) {
            top = top.parent;
        }
        mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv, new SimpleRemapper(top.precompiled.slash, top.generated.slash)));
    }
    super.visitEnd();
}
Also used : SimpleRemapper(org.objectweb.asm.commons.SimpleRemapper) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassSet(org.apache.drill.exec.compile.ClassTransformer.ClassSet) RemappingMethodAdapter(org.objectweb.asm.commons.RemappingMethodAdapter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 8 with FieldNode

use of org.objectweb.asm.tree.FieldNode in project bytecode-viewer by Konloch.

the class ClassNodeDecompiler method decompile.

protected static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, ArrayList<String> decompiledClasses, String containerName, ClassNode cn) {
    ArrayList<String> unableToDecompile = new ArrayList<String>();
    decompiledClasses.add(cn.name);
    sb.append(getAccessString(cn.access));
    sb.append(" ");
    sb.append(cn.name);
    if (cn.superName != null && !cn.superName.equals("java/lang/Object")) {
        sb.append(" extends ");
        sb.append(cn.superName);
    }
    int amountOfInterfaces = cn.interfaces.size();
    if (amountOfInterfaces > 0) {
        sb.append(" implements ");
        sb.append(cn.interfaces.get(0));
        if (amountOfInterfaces > 1) {
        // sb.append(",");
        }
        for (int i = 1; i < amountOfInterfaces; i++) {
            sb.append(", ");
            sb.append(cn.interfaces.get(i));
        }
    }
    sb.append(" {");
    sb.append(BytecodeViewer.nl);
    for (FieldNode fn : (List<FieldNode>) cn.fields) {
        sb.append(BytecodeViewer.nl);
        sb.append("     ");
        FieldNodeDecompiler.decompile(sb, fn);
    }
    if (cn.fields.size() > 0) {
        sb.append(BytecodeViewer.nl);
    }
    for (MethodNode mn : (List<MethodNode>) cn.methods) {
        sb.append(BytecodeViewer.nl);
        MethodNodeDecompiler.decompile(sb, mn, cn);
    }
    for (Object o : cn.innerClasses) {
        InnerClassNode innerClassNode = (InnerClassNode) o;
        String innerClassName = innerClassNode.name;
        if ((innerClassName != null) && !decompiledClasses.contains(innerClassName)) {
            decompiledClasses.add(innerClassName);
            ClassNode cn1 = BytecodeViewer.getClassNode(containerName, innerClassName);
            if (cn1 != null) {
                sb.appendPrefix("     ");
                sb.append(BytecodeViewer.nl + BytecodeViewer.nl);
                sb = decompile(sb, decompiledClasses, containerName, cn1);
                sb.trimPrefix(5);
                sb.append(BytecodeViewer.nl);
            } else {
                unableToDecompile.add(innerClassName);
            }
        }
    }
    if (!unableToDecompile.isEmpty()) {
        sb.append("//the following inner classes couldn't be decompiled: ");
        for (String s : unableToDecompile) {
            sb.append(s);
            sb.append(" ");
        }
        sb.append(BytecodeViewer.nl);
    }
    sb.append("}");
    // " with prefix length: " + sb.prefix.length());
    return sb;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) InnerClassNode(org.objectweb.asm.tree.InnerClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) InnerClassNode(org.objectweb.asm.tree.InnerClassNode)

Example 9 with FieldNode

use of org.objectweb.asm.tree.FieldNode in project buck by facebook.

the class StaticStateAnalyzer method isClassSafe.

private boolean isClassSafe(ClassNode klass) {
    boolean isSafe = true;
    // Look for mutable static fields.
    for (FieldNode field : klass.fields) {
        if ((field.access & Opcodes.ACC_STATIC) == 0) {
            continue;
        }
        if ((field.access & Opcodes.ACC_FINAL) == 0) {
            log.add("Non-final static field: " + describe(klass, field));
            isSafe = false;
            continue;
        }
        if (!mutabilityAnalyzer.isTypeImmutable(field.desc)) {
            log.add("Mut-final static field: " + describe(klass, field));
            isSafe = false;
            continue;
        }
    }
    // Look for static synchronized methods.
    for (MethodNode method : klass.methods) {
        if ((method.access & Opcodes.ACC_STATIC) == 0) {
            continue;
        }
        if ((method.access & Opcodes.ACC_SYNCHRONIZED) != 0) {
            log.add("Synchronized static method: " + describe(klass, method));
            isSafe = false;
            continue;
        }
    }
    return isSafe;
}
Also used : FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode)

Example 10 with FieldNode

use of org.objectweb.asm.tree.FieldNode in project MinecraftForge by MinecraftForge.

the class AccessTransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (bytes == null) {
        return null;
    }
    if (DEBUG) {
        FMLRelaunchLog.fine("Considering all methods and fields on %s (%s)\n", transformedName, name);
    }
    if (!modifiers.containsKey(transformedName)) {
        return bytes;
    }
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(bytes);
    classReader.accept(classNode, 0);
    Collection<Modifier> mods = modifiers.get(transformedName);
    for (Modifier m : mods) {
        if (m.modifyClassVisibility) {
            classNode.access = getFixedAccess(classNode.access, m);
            if (DEBUG) {
                System.out.println(String.format("Class: %s %s -> %s", name, toBinary(m.oldAccess), toBinary(m.newAccess)));
            }
            continue;
        }
        if (m.desc.isEmpty()) {
            for (FieldNode n : classNode.fields) {
                if (n.name.equals(m.name) || m.name.equals("*")) {
                    n.access = getFixedAccess(n.access, m);
                    if (DEBUG) {
                        System.out.println(String.format("Field: %s.%s %s -> %s", name, n.name, toBinary(m.oldAccess), toBinary(m.newAccess)));
                    }
                    if (!m.name.equals("*")) {
                        break;
                    }
                }
            }
        } else {
            List<MethodNode> nowOverrideable = Lists.newArrayList();
            for (MethodNode n : classNode.methods) {
                if ((n.name.equals(m.name) && n.desc.equals(m.desc)) || m.name.equals("*")) {
                    n.access = getFixedAccess(n.access, m);
                    // constructors always use INVOKESPECIAL
                    if (!n.name.equals("<init>")) {
                        // if we changed from private to something else we need to replace all INVOKESPECIAL calls to this method with INVOKEVIRTUAL
                        // so that overridden methods will be called. Only need to scan this class, because obviously the method was private.
                        boolean wasPrivate = (m.oldAccess & ACC_PRIVATE) == ACC_PRIVATE;
                        boolean isNowPrivate = (m.newAccess & ACC_PRIVATE) == ACC_PRIVATE;
                        if (wasPrivate && !isNowPrivate) {
                            nowOverrideable.add(n);
                        }
                    }
                    if (DEBUG) {
                        System.out.println(String.format("Method: %s.%s%s %s -> %s", name, n.name, n.desc, toBinary(m.oldAccess), toBinary(m.newAccess)));
                    }
                    if (!m.name.equals("*")) {
                        break;
                    }
                }
            }
            replaceInvokeSpecial(classNode, nowOverrideable);
        }
    }
    ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassReader(org.objectweb.asm.ClassReader) ClassWriter(org.objectweb.asm.ClassWriter)

Aggregations

FieldNode (org.objectweb.asm.tree.FieldNode)19 ClassNode (org.objectweb.asm.tree.ClassNode)11 MethodNode (org.objectweb.asm.tree.MethodNode)8 ClassReader (org.objectweb.asm.ClassReader)6 ArrayList (java.util.ArrayList)3 ClassWriter (org.objectweb.asm.ClassWriter)3 AnnotationNode (org.objectweb.asm.tree.AnnotationNode)3 List (java.util.List)2 Type (org.objectweb.asm.Type)2 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)2 InnerClassNode (org.objectweb.asm.tree.InnerClassNode)2 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)2 ModContainer (cpw.mods.fml.common.ModContainer)1 ArtifactVersion (cpw.mods.fml.common.versioning.ArtifactVersion)1 DefaultArtifactVersion (cpw.mods.fml.common.versioning.DefaultArtifactVersion)1 VersionRange (cpw.mods.fml.common.versioning.VersionRange)1 IOException (java.io.IOException)1 Nullable (javax.annotation.Nullable)1 Event (net.minecraftforge.fml.common.eventhandler.Event)1 ClassSet (org.apache.drill.exec.compile.ClassTransformer.ClassSet)1