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();
}
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);
}
}
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();
}
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);
}
}
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;
}
Aggregations