Search in sources :

Example 6 with AnnotationNode

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

the class Utils method fieldNodeFormat.

@SuppressWarnings("unchecked")
public static String fieldNodeFormat(FieldNode fieldNode) {
    StringBuilder s = new StringBuilder();
    if (fieldNode.invisibleAnnotations != null) {
        List<AnnotationNode> annotations = fieldNode.invisibleAnnotations;
        for (AnnotationNode annotationNode : annotations) {
            s.append(annotationNodeFormat(annotationNode));
        }
        annotations = fieldNode.visibleAnnotations;
        for (AnnotationNode annotationNode : annotations) {
            s.append(annotationNodeFormat(annotationNode));
        }
    }
    s.append(Modifier.toString(fieldNode.access));
    s.append(' ');
    s.append(fieldNode.desc);
    s.append(' ');
    s.append(fieldNode.name);
    if (fieldNode.signature != null) {
        s.append("    ").append(fieldNode.signature);
    }
    // TODO include field attributes?
    return s.toString();
}
Also used : AnnotationNode(org.objectweb.asm.tree.AnnotationNode)

Example 7 with AnnotationNode

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

the class Utils method formatAnnotationNodeNameValuePairValue.

public static void formatAnnotationNodeNameValuePairValue(Object value, StringBuilder s) {
    if (value instanceof Type) {
        s.append(((org.objectweb.asm.Type) value).getDescriptor());
    } else if (value instanceof Array) {
        // enum node
        @SuppressWarnings("rawtypes") List l = Arrays.asList(value);
        s.append(l.get(0)).append(l.get(1));
    } else if (value instanceof List) {
        @SuppressWarnings("rawtypes") List l = (List) value;
        s.append("[");
        for (int i = 0, max = l.size(); i < max; i++) {
            if (i > 0) {
                s.append(',');
            }
            formatAnnotationNodeNameValuePairValue(l.get(i), s);
        }
    } else if (value instanceof AnnotationNode) {
        s.append(annotationNodeFormat((AnnotationNode) value));
    } else {
        s.append(value);
    }
}
Also used : Array(java.lang.reflect.Array) Type(org.objectweb.asm.Type) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) List(java.util.List)

Example 8 with AnnotationNode

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

the class TypeDiffComputer method compareAnnotations.

private static String compareAnnotations(List<AnnotationNode> oldAnnos, List<AnnotationNode> newAnnos) {
    if (oldAnnos == null) {
        if (newAnnos == null) {
            return "";
        }
        oldAnnos = Collections.emptyList();
    }
    if (newAnnos == null) {
        newAnnos = Collections.emptyList();
    }
    StringBuilder diff = new StringBuilder();
    // Which have been removed
    for (AnnotationNode o : oldAnnos) {
        boolean found = false;
        String oFormatted = Utils.annotationNodeFormat(o);
        for (AnnotationNode n : newAnnos) {
            String nFormatted = Utils.annotationNodeFormat(n);
            if (oFormatted.equals(nFormatted)) {
                found = true;
                break;
            }
        }
        if (!found) {
            diff.append("-").append(oFormatted);
        }
    }
    // Which have been added
    for (AnnotationNode n : newAnnos) {
        boolean found = false;
        String nFormatted = Utils.annotationNodeFormat(n);
        for (AnnotationNode o : oldAnnos) {
            String oFormatted = Utils.annotationNodeFormat(o);
            if (oFormatted.equals(nFormatted)) {
                found = true;
                break;
            }
        }
        if (!found) {
            diff.append("+").append(nFormatted);
        }
    }
    return diff.toString();
}
Also used : AnnotationNode(org.objectweb.asm.tree.AnnotationNode)

Example 9 with AnnotationNode

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

the class SpringLoadedTests method checkAnnotations.

@SuppressWarnings("unchecked")
protected void checkAnnotations(byte[] bytes, String methodNameAndDescriptor, String... expected) {
    ClassNode cn = new ClassNode();
    ClassReader cr = new ClassReader(bytes);
    cr.accept(cn, 0);
    if (expected == null) {
        expected = new String[0];
    }
    boolean checked = false;
    List<MethodNode> methods = cn.methods;
    for (MethodNode mn : methods) {
        if (methodNameAndDescriptor.equals(mn.name + mn.desc)) {
            List<AnnotationNode> annotations = mn.visibleAnnotations;
            if (annotations == null) {
                annotations = Collections.emptyList();
            }
            Assert.assertEquals(expected.length, annotations.size());
            for (int i = 0; i < expected.length; i++) {
                //					StringTokenizer tokenizer = new StringTokenizer(expected[i], ":");
                //					String expectedName = tokenizer.nextToken();
                //					String expectedDesc = tokenizer.nextToken();
                AnnotationNode annotation = annotations.get(i);
                Assert.assertEquals(expected[i], toString(annotation));
            }
            checked = true;
        }
    }
    if (!checked) {
        for (MethodNode mn : methods) {
            System.out.println(mn.name + mn.desc);
        }
        Assert.fail("Unable to find method " + methodNameAndDescriptor);
    }
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) ClassReader(org.objectweb.asm.ClassReader)

Example 10 with AnnotationNode

use of org.objectweb.asm.tree.AnnotationNode in project Engine by VoltzEngine-Project.

the class ClassTransformer method transform.

@Override
public byte[] transform(String name, String transformedName, byte[] bytes) {
    if (EngineCoreMod.devMode && enableSuperDetails) {
        EngineCoreMod.logger.info("com.builtbroken.mc.core.asm.template.transform(name='" + name + "'  transformedName='" + transformedName + "',  bytes);");
    }
    try {
        //Only functions on our classes
        if (!shouldProcess(transformedName) || TemplateManager.templates.isEmpty()) {
            if (EngineCoreMod.devMode && enableSuperDetails) {
                EngineCoreMod.logger.info("\tClass is not in processing list so is ignored");
            }
            return bytes;
        }
        boolean changed = false;
        ClassNode cnode = ASMHelper.createClassNode(bytes);
        if (cnode != null && cnode.visibleAnnotations != null) {
            for (AnnotationNode nodes : cnode.visibleAnnotations) {
                if (nodes.desc.equals("Lcom/builtbroken/mc/api/InjectTemplate;")) {
                    if (EngineCoreMod.devMode && enableSuperDetails) {
                        EngineCoreMod.logger.info("\t Class contains annotation, injecting code");
                    }
                    /*
                         * The 2nd value in UniversalClass is the annotation we're looking for to filter
                         * out which mod to deal with.
                         */
                    String flags = null;
                    if (nodes.values != null && nodes.values.size() >= 2) {
                        flags = (String) nodes.values.get(1);
                    }
                    changed |= injectTemplate(cnode, flags);
                    break;
                }
            }
        }
        if (changed) {
            byte[] data = ASMHelper.createBytes(cnode, ClassWriter.COMPUTE_FRAMES);
            if (EngineCoreMod.devMode) {
                try {
                    File file = new File(System.getProperty("user.dir"), "asmTestFolder/" + name + ".class");
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(data);
                    fos.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return data;
        }
    } catch (Exception e) {
        throw new RuntimeException("TemplateClassTransformer: Failed to process class " + transformedName, e);
    }
    return bytes;
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) AnnotationNode(org.objectweb.asm.tree.AnnotationNode) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

AnnotationNode (org.objectweb.asm.tree.AnnotationNode)16 ClassNode (org.objectweb.asm.tree.ClassNode)6 List (java.util.List)4 ClassReader (org.objectweb.asm.ClassReader)4 MethodNode (org.objectweb.asm.tree.MethodNode)4 FieldNode (org.objectweb.asm.tree.FieldNode)3 File (java.io.File)2 Type (org.objectweb.asm.Type)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 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 Array (java.lang.reflect.Array)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1