Search in sources :

Example 11 with FieldNode

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

the class FMLDeobfuscatingRemapper method getFieldType.

@Nullable
private String getFieldType(String owner, String name) {
    if (fieldDescriptions.containsKey(owner)) {
        return fieldDescriptions.get(owner).get(name);
    }
    synchronized (fieldDescriptions) {
        try {
            byte[] classBytes = ClassPatchManager.INSTANCE.getPatchedResource(owner, map(owner).replace('/', '.'), classLoader);
            if (classBytes == null) {
                return null;
            }
            ClassReader cr = new ClassReader(classBytes);
            ClassNode classNode = new ClassNode();
            cr.accept(classNode, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
            Map<String, String> resMap = Maps.newHashMap();
            for (FieldNode fieldNode : classNode.fields) {
                resMap.put(fieldNode.name, fieldNode.desc);
            }
            fieldDescriptions.put(owner, resMap);
            return resMap.get(name);
        } catch (IOException e) {
            FMLRelaunchLog.log(Level.ERROR, e, "A critical exception occurred reading a class file %s", owner);
        }
        return null;
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Example 12 with FieldNode

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

the class ItemStackTransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
    if (!"net.minecraft.item.ItemStack".equals(name))
        return basicClass;
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    FieldNode itemField = null;
    for (FieldNode f : classNode.fields) {
        if (ITEM_TYPE.equals(f.desc) && itemField == null) {
            itemField = f;
        } else if (ITEM_TYPE.equals(f.desc)) {
            throw new RuntimeException("Error processing ItemStack - found a duplicate Item field");
        }
    }
    if (itemField == null) {
        throw new RuntimeException("Error processing ItemStack - no Item field declared (is the code somehow obfuscated?)");
    }
    MethodNode getItemMethod = null;
    for (MethodNode m : classNode.methods) {
        if (m.name.equals("getItemRaw"))
            continue;
        if (GETITEM_DESC.equals(m.desc) && getItemMethod == null) {
            getItemMethod = m;
        } else if (GETITEM_DESC.equals(m.desc)) {
            throw new RuntimeException("Error processing ItemStack - duplicate getItem method found");
        }
    }
    if (getItemMethod == null) {
        throw new RuntimeException("Error processing ItemStack - no getItem method found (is the code somehow obfuscated?)");
    }
    for (MethodNode m : classNode.methods) {
        if (m.name.equals("getItemRaw"))
            continue;
        for (ListIterator<AbstractInsnNode> it = m.instructions.iterator(); it.hasNext(); ) {
            AbstractInsnNode insnNode = it.next();
            if (insnNode.getType() == AbstractInsnNode.FIELD_INSN) {
                FieldInsnNode fi = (FieldInsnNode) insnNode;
                if (itemField.name.equals(fi.name) && fi.getOpcode() == Opcodes.GETFIELD) {
                    it.remove();
                    MethodInsnNode replace = new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "net/minecraft/item/ItemStack", getItemMethod.name, getItemMethod.desc, false);
                    it.add(replace);
                }
            }
        }
    }
    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) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) ClassWriter(org.objectweb.asm.ClassWriter)

Example 13 with FieldNode

use of org.objectweb.asm.tree.FieldNode in project spring-loaded by spring-projects.

the class SpringLoadedTests method getField.

@SuppressWarnings("unchecked")
private FieldNode getField(byte[] classbytes, String fieldName) {
    ClassReader super_cr = new ClassReader(classbytes);
    ClassNode cn = new ClassNode();
    super_cr.accept(cn, 0);
    List<FieldNode> fields = cn.fields;
    if (fields != null) {
        for (FieldNode fn : fields) {
            if (fn.name.equals(fieldName)) {
                return fn;
            }
        }
    }
    return null;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) ClassReader(org.objectweb.asm.ClassReader)

Example 14 with FieldNode

use of org.objectweb.asm.tree.FieldNode in project spring-loaded by spring-projects.

the class SpringLoadedTests method toStringField.

@SuppressWarnings("unchecked")
protected String toStringField(byte[] classdata, String fieldname) {
    StringBuilder sb = new StringBuilder();
    FieldNode fieldNode = getField(classdata, fieldname);
    if (fieldNode == null) {
        return null;
    }
    List<AnnotationNode> annos = fieldNode.visibleAnnotations;
    if (annos != null) {
        sb.append("vis(").append(toStringAnnotations(annos)).append(") ");
    }
    annos = fieldNode.invisibleAnnotations;
    if (annos != null) {
        sb.append("invis(").append(toStringAnnotations(annos)).append(") ");
    }
    // will need implementing at some point:
    //		List<Attribute> attrs = fieldNode.attrs;
    //		if (attrs = !null) {
    //			sb.append("attrs(").append(toStringAttributes(attrs)).append(") ");
    //		}
    sb.append("0x").append(Integer.toHexString(fieldNode.access)).append("(").append(ClassPrinter.toAccessForMember(fieldNode.access)).append(") ");
    sb.append(fieldNode.name).append(" ");
    sb.append(fieldNode.desc).append(" ");
    if (fieldNode.signature != null) {
        sb.append(fieldNode.signature).append(" ");
    }
    if (fieldNode.value != null) {
        sb.append(fieldNode.value).append(" ");
    }
    return sb.toString().trim();
}
Also used : FieldNode(org.objectweb.asm.tree.FieldNode) AnnotationNode(org.objectweb.asm.tree.AnnotationNode)

Example 15 with FieldNode

use of org.objectweb.asm.tree.FieldNode in project LogisticsPipes by RS485.

the class DevEnvHelper method resolveField.

// returns actual owner of field
// or null if the field could not be resolved
private static String resolveField(String owner, String name, String desc, Mapping m) throws IOException {
    if (owner == null) {
        return null;
    }
    byte[] bytes = Launch.classLoader.getClassBytes(owner);
    if (bytes == null) {
        return null;
    }
    final ClassNode cn = new ClassNode();
    ClassReader reader = new ClassReader(bytes);
    reader.accept(cn, 0);
    for (FieldNode fn : cn.fields) {
        String newName = m.getField(owner, name);
        String newDesc = m.mapTypeDescriptor(desc);
        if (fn.name.equals(newName) && fn.desc.equals(newDesc)) {
            return owner;
        }
    }
    for (String i : cn.interfaces) {
        String result = DevEnvHelper.resolveField(i, name, desc, m);
        if (result != null) {
            return result;
        }
    }
    return DevEnvHelper.resolveField(cn.superName, name, desc, m);
}
Also used : InnerClassNode(org.objectweb.asm.tree.InnerClassNode) ClassNode(org.objectweb.asm.tree.ClassNode) FieldNode(org.objectweb.asm.tree.FieldNode) ClassReader(org.objectweb.asm.ClassReader)

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