use of org.objectweb.asm.Attribute in project sonar-java by SonarSource.
the class AsmExample method main.
public static void main(String[] args) throws Exception {
ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
System.out.println("CLASS");
System.out.println("access: " + asFlagSet(access));
System.out.println("name: " + name);
System.out.println("signature: " + signature);
System.out.println("superName: " + superName);
System.out.println("interfaces: " + Arrays.toString(interfaces));
System.out.println();
}
@Override
public void visitSource(String source, String debug) {
System.out.println("SOURCE");
System.out.println();
}
@Override
public void visitOuterClass(String owner, String name, String desc) {
System.out.println("OUTER CLASS");
System.out.println("owner: " + owner);
System.out.println("name: " + name);
System.out.println("desc: " + desc);
System.out.println();
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
System.out.println("ANNOTATION");
System.out.println("desc: " + desc);
System.out.println("visible: " + visible);
return null;
}
@Override
public void visitAttribute(Attribute attr) {
System.out.println("ATTRIBUTE");
System.out.println();
}
@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
System.out.println("INNER CLASS");
System.out.println("access: " + asFlagSet(access));
System.out.println("name: " + name);
System.out.println("outerName: " + outerName);
System.out.println("innerName: " + innerName);
System.out.println();
}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
System.out.println("FIELD");
System.out.println("access: " + asFlagSet(access));
System.out.println("name: " + name);
System.out.println("desc: " + desc);
System.out.println("signature: " + desc);
System.out.println();
return null;
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
System.out.println("METHOD");
System.out.println("access: " + asFlagSet(access));
System.out.println("name: " + name);
System.out.println("desc: " + desc);
System.out.println("signature: " + desc);
System.out.println("exceptions: " + Arrays.toString(exceptions));
System.out.println();
return null;
}
@Override
public void visitEnd() {
System.out.println("END");
}
public EnumSet<Flag> asFlagSet(int flags) {
EnumSet<Flag> result = EnumSet.noneOf(Flag.class);
int mask = 1;
for (int i = 0; i < 15; i++) {
if ((flags & mask) != 0) {
result.add(Flag.values()[i]);
}
mask = mask << 1;
}
return result;
}
};
InputStream in = AsmExample.class.getResourceAsStream("/org/sonar/java/resolve/AsmExample.class");
ClassReader classReader = new ClassReader(in);
classReader.accept(cv, 0);
}
use of org.objectweb.asm.Attribute in project bytecode-viewer by Konloch.
the class ClassNodeDecompiler method decompile.
public static PrefixedStringBuilder decompile(PrefixedStringBuilder sb, List<String> decompiledClasses, ClassNode cn) {
List<String> unableToDecompile = new ArrayList<>();
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));
for (int i = 1; i < amountOfInterfaces; i++) {
sb.append(", ");
sb.append(cn.interfaces.get(i));
}
}
sb.append(" {");
sb.append(nl);
sb.append(" ");
sb.append("<ClassVersion=" + cn.version + ">");
sb.append(nl);
if (cn.sourceDebug != null) {
sb.append(" ");
sb.append("<SourceDebug=" + cn.sourceDebug + ">");
sb.append(nl);
}
if (cn.sourceFile != null) {
sb.append(" ");
sb.append("<SourceFile=" + cn.sourceFile + ">");
sb.append(nl);
}
if (cn.signature != null) {
sb.append(" ");
sb.append("<Sig=" + cn.signature + ">");
}
for (FieldNode fn : cn.fields) {
sb.append(nl);
sb.append(" ");
FieldNodeDecompiler.decompile(sb, fn);
}
if (cn.fields.size() > 0) {
sb.append(nl);
}
for (MethodNode mn : cn.methods) {
sb.append(nl);
MethodNodeDecompiler.decompile(sb, mn, cn);
}
for (InnerClassNode o : cn.innerClasses) {
String innerClassName = o.name;
if ((innerClassName != null) && !decompiledClasses.contains(innerClassName)) {
decompiledClasses.add(innerClassName);
ClassNode cn1 = BytecodeViewer.blindlySearchForClassNode(innerClassName);
if (cn1 != null) {
sb.appendPrefix(" ");
sb.append(nl + nl);
sb = decompile(sb, decompiledClasses, cn1);
sb.trimPrefix(5);
sb.append(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(nl);
}
if (cn.attrs != null) {
sb.append(nl);
for (Attribute attr : cn.attrs) {
// TODO: finish attributes
// + attr.content.toString());
sb.append(attr.type + ": ");
}
}
// sb.append(BytecodeViewer.nl);
sb.append("}");
// " with prefix length: " + sb.prefix.length());
return sb;
}
use of org.objectweb.asm.Attribute in project robovm by robovm.
the class ObjCProtocolProxyPlugin method generateProxyMethods.
private void generateProxyMethods(Config config, List<String> interfazes, ClassWriter cw) throws IOException {
Clazzes clazzes = config.getClazzes();
final Set<String> addedMethods = new HashSet<>();
for (String interfaze : interfazes) {
Clazz clazz = clazzes.load(interfaze);
if (clazz == null) {
continue;
}
// Copy all abstract method (we skip default methods) to the proxy
// and make them native instead of abstract.
ClassReader classReader = new ClassReader(clazz.getBytes());
classReader.accept(new ClassVisitor(ASM4, cw) {
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
String key = name + desc;
if ((access & ACC_ABSTRACT) > 0 && !addedMethods.contains(key)) {
access &= ~ACC_ABSTRACT;
access |= ACC_NATIVE;
addedMethods.add(key);
return super.visitMethod(access, name, desc, signature, exceptions);
}
return null;
}
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
// Ignored
}
@Override
public void visitEnd() {
// Ignored
}
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
// Ignored
return null;
}
@Override
public void visitAttribute(Attribute attr) {
// Ignored
}
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
// Ignored
return null;
}
@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
// Ignored
}
@Override
public void visitOuterClass(String owner, String name, String desc) {
// Ignored
}
@Override
public void visitSource(String source, String debug) {
// Ignored
}
}, 0);
}
}
use of org.objectweb.asm.Attribute in project jacoco by jacoco.
the class ClassAnalyzerTest method should_collect_attributes.
@Test
public void should_collect_attributes() {
assertTrue(analyzer.getClassAttributes().isEmpty());
analyzer.visitAttribute(new Attribute("foo") {
});
assertTrue(analyzer.getClassAttributes().contains("foo"));
}
use of org.objectweb.asm.Attribute in project maple-ir by LLVM-but-worse.
the class ASMPrinter method emitLiteral.
@Override
public void emitLiteral(Object o) {
if (o instanceof AnnotationNode) {
AnnotationNode an = (AnnotationNode) o;
Map<Object, Object> map = new HashMap<>();
map.put("desc", an.desc);
if (isNonEmpty(an.values)) {
List<Object> valuesList = new ArrayList<>();
List<Object> realValues = an.values;
if ((realValues.size() % 2) != 0) {
throw new IllegalStateException(String.format("AN values: %s", realValues));
}
for (int i = 0; i < realValues.size() / 2; i++) {
int j = (i * 2);
String key = (String) realValues.get(j);
Object val = realValues.get(j + 1);
Map<String, Object> innerMap = new HashMap<>();
innerMap.put(key, val);
valuesList.add(innerMap);
}
map.put("values", valuesList);
}
this.emitDirectiveValue(map);
} else if (o instanceof Attribute) {
Attribute attr = (Attribute) o;
Map<String, Object> map = new HashMap<>();
map.put("type", attr.type);
// TODO: check
map.put("values", new String(attr.value));
this.emitDirectiveValue(map);
throw new UnsupportedOperationException("TODO");
} else if (o instanceof Type) {
this.sw.print("T\"").print(o.toString()).print("\"");
} else {
super.emitLiteral(o);
}
}
Aggregations