List of usage examples for org.objectweb.asm.tree ClassNode ClassNode
public ClassNode()
From source file:gemlite.maven.plugin.support.mapper.MapperToolProcessor.java
License:Apache License
/*** * @param domain//w ww. j av a2 s. com * @return * @throws IOException */ private ClassNode createIMapperToolClass(ClassNode domain) throws IOException { ClassNode mapper = new ClassNode(); mapper.version = V1_7; mapper.access = ACC_PUBLIC + ACC_SUPER; mapper.name = domain.name + DomainMapperHelper.DEFAULT_SUBFFIX; mapper.superName = "java/lang/Object"; mapper.interfaces.add(INTERFACE_NAME); addConstructFunction(domain, mapper); String keyDesc = implementInterface(domain, mapper); String name = AsmHelper.toFullName(keyDesc); mapper.signature = "Ljava/lang/Object;Lgemlite/core/internal/domain/IMapperTool<" + name + "L" + domain.name + ";>;"; return mapper; }
From source file:gemlite.maven.plugin.support.mapper.MapperToolProcessor.java
License:Apache License
private ClassNode findKeyClass(ClassNode domain) throws IOException { ClassNode key = null;//from w w w . j a va 2 s. c om if (DomainMojoHelper.log().isDebugEnabled()) DomainMojoHelper.log() .debug("Traverse domain annotations to find if there exists key class, domain:" + domain.name); for (Object o : domain.visibleAnnotations) { AnnotationNode an = (AnnotationNode) o; if (DomainMojoHelper.log().isDebugEnabled()) DomainMojoHelper.log().debug("Annotation:" + an.desc + " value:" + an.values); if (AN_Key.equals(an.desc)) { Type t = (Type) an.values.get(1); InputStream keyClassIn = DomainMojoHelper.getLoader() .getResourceAsStream(t.getInternalName() + ".class"); byte[] keyClassBytes = new byte[keyClassIn.available()]; keyClassIn.read(keyClassBytes); keyClassIn.close(); ClassReader cr = new ClassReader(keyClassBytes); key = new ClassNode(); cr.accept(key, 0); } } return key; }
From source file:hellfirepvp.astralsorcery.core.AstralTransformer.java
License:Open Source License
@Override public byte[] transform(String name, String transformedName, byte[] bytes) { if (!isTransformationRequired(transformedName)) return bytes; ClassNode node = new ClassNode(); ClassReader reader = new ClassReader(bytes); reader.accept(node, 0);/*ww w . j a v a 2s .c om*/ for (SubClassTransformer subTransformer : subTransformers) { try { subTransformer.transformClassNode(node, transformedName, name); } catch (ASMTransformationException asmException) { FMLLog.warning("Access transformation failed for Transformer: " + subTransformer.getIdentifier()); FMLLog.warning("Transformer added information:"); subTransformer.addErrorInformation(); asmException.printStackTrace(); throw asmException; //Rethrow } } ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); node.accept(writer); bytes = writer.toByteArray(); if (false) { try { File f = new File("C:/ASTestClasses/" + transformedName + ".class"); f.getParentFile().mkdirs(); f.createNewFile(); FileOutputStream out = new FileOutputStream(f); out.write(bytes); out.close(); } catch (Exception e) { e.printStackTrace(); } } return bytes; }
From source file:ht.misc.injectsocks.InjectSockstTransformerImpl.java
License:Apache License
public byte[] inject(byte[] classfileBuffer) { try {//from www . j a v a 2 s. c o m ClassReader cr = new ClassReader(classfileBuffer); ClassNode cn = new ClassNode(); cr.accept(cn, 0); ArrayList<AbstractInsnNode> injectPos = new ArrayList<AbstractInsnNode>(); @SuppressWarnings("unchecked") List<MethodNode> methods = (List<MethodNode>) cn.methods; for (int i = 0; i < methods.size(); ++i) { MethodNode method = methods.get(i); InsnList instructions = method.instructions; if (instructions.size() <= 0) continue; //System.out.println("Method: "+method.name+" "); for (int j = 0; j < instructions.size(); ++j) { AbstractInsnNode insn = (AbstractInsnNode) instructions.get(j); //System.out.println("\tInsn: opc="+OpcodeUtil.getOpcode(insn.getOpcode())+", type="+insn.getType()); if (insn.getType() == AbstractInsnNode.METHOD_INSN) { MethodInsnNode min = (MethodInsnNode) insn; //System.out.printf("\t\towner=%s, name=%s, desc=%s\n", min.owner, min.name, min.desc); if (min.owner.equals("java/net/Socket") && min.name.equals("<init>") && min.desc.equals("()V")) { min.desc = "(Ljava/net/Proxy;)V"; injectPos.add(min); } } } for (int k = 0; k < injectPos.size(); k++) { AbstractInsnNode pos = injectPos.get(k); MethodInsnNode newMin = new MethodInsnNode(Opcodes.INVOKESTATIC, "ht/misc/injectsocks/ProxyManager", "getProxy", "()Ljava/net/Proxy;", false); instructions.insertBefore(pos, newMin); } } ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cn.accept(cw); byte[] injectedClassfileBuffer = cw.toByteArray(); System.out.printf("INFO: classfileBuffer.legnth=%d, injectedClassfileBuffer.length=%d\n", classfileBuffer.length, injectedClassfileBuffer.length); return injectedClassfileBuffer; } catch (Throwable e) { e.printStackTrace(); return classfileBuffer; } }
From source file:ht.misc.injectsocks.InjectSockstTransformerImpl.java
License:Apache License
public void printClassByteCode(byte code[]) { ClassReader cr = new ClassReader(code); ClassNode cn = new ClassNode(); cr.accept(cn, 0);// w w w .j a v a 2 s. co m @SuppressWarnings("unchecked") List<MethodNode> methods = (List<MethodNode>) cn.methods; for (int i = 0; i < methods.size(); ++i) { MethodNode method = methods.get(i); InsnList instructions = method.instructions; if (instructions.size() <= 0) continue; System.out.println("Method: " + method.name + " "); for (int j = 0; j < instructions.size(); ++j) { AbstractInsnNode insn = (AbstractInsnNode) instructions.get(j); System.out.println( "\tInsn: opc=" + OpcodeUtil.getOpcode(insn.getOpcode()) + ", type=" + insn.getType()); if (insn.getType() == AbstractInsnNode.METHOD_INSN) { MethodInsnNode min = (MethodInsnNode) insn; System.out.printf("\t\towner=%s, name=%s, desc=%s\n", min.owner, min.name, min.desc); } } } }
From source file:io.airlift.drift.codec.internal.compiler.byteCode.ClassDefinition.java
License:Apache License
public ClassNode getClassNode() { ClassNode classNode = new ClassNode(); classNode.version = V1_6;//from w w w. j a v a2 s . co m classNode.access = access; classNode.name = name; classNode.superName = superClass.getClassName(); for (ParameterizedType interfaceType : interfaces) { classNode.interfaces.add(interfaceType.getClassName()); } // add generic signature if super class or any interface is generic if (superClass.isGeneric() || interfaces.stream().anyMatch(ParameterizedType::isGeneric)) { classNode.signature = genericClassSignature(superClass, interfaces); } for (FieldDefinition field : fields) { classNode.fields.add(field.getFieldNode()); } for (MethodDefinition method : methods) { classNode.methods.add(method.getMethodNode()); } return classNode; }
From source file:io.awacs.plugin.springmvc.SpringmvcPlugin.java
License:Apache License
@Override public void boot() { inst.addTransformer(new ClassFileTransformer() { @Override/* ww w . ja va2s .c o m*/ public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { final ServiceWrapper wrapper = new ServiceWrapper(); ClassNode cn = new ClassNode(); ClassReader cr = new ClassReader(classfileBuffer); cr.accept(cn, 0); boolean transformed = wrapper.transform(cn); ClassWriter cw = new ClassWriter(0); cn.accept(cw); if (transformed) { logger.info(className + " has been catched by springmvc plugin."); } return cw.toByteArray(); } }); }
From source file:io.moshisho.plugins.MyMojo.java
License:Apache License
private static void betterCompile(String classFile) throws IOException { InputStream is = new FileInputStream(classFile); ClassReader cr = new ClassReader(is); is.close();/* w ww. j a v a 2 s . co m*/ ClassNode cn = new ClassNode(); cr.accept(cn, 0); List methods = cn.methods; for (int i = 0; i < methods.size(); ++i) { MethodNode method = (MethodNode) methods.get(i); if (!isStaticllyBound(method)) { continue; } InsnList instructions = method.instructions; AbstractInsnNode last = instructions.getLast(); while (last != null && last.getType() == AbstractInsnNode.LABEL) { last = last.getPrevious(); } if (last == null || !isReturnInstruction(last) || last.getPrevious().getType() != AbstractInsnNode.METHOD_INSN) { continue; } MethodInsnNode methodInv = (MethodInsnNode) last.getPrevious(); if (!isRecursionCall(cn, method, methodInv)) { continue; } System.out.println("TailRec Optimizaing: " + method.name); // get arguments and types String methodDesc = method.desc; String argsDesc = methodDesc.substring(methodDesc.indexOf('(') + 1, methodDesc.indexOf(')')); System.out.println(argsDesc); // work with Type.getArgumentTypes List<AbstractInsnNode> listInstNodes = new LinkedList<AbstractInsnNode>(); for (int j = argsDesc.length() - 1; j >= 0; j--) { char c = argsDesc.charAt(j); switch (c) { case 'I': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'Z': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'C': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'B': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'S': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'F': listInstNodes.add(new VarInsnNode(FSTORE, argsDesc.length() - j)); break; case 'J': listInstNodes.add(new VarInsnNode(LSTORE, argsDesc.length() - j)); break; case 'D': listInstNodes.add(new VarInsnNode(DSTORE, argsDesc.length() - j)); break; case '[': // TODO: case 'L': // TODO: default: System.out.println("NOT TREATING: " + c); } } // remove the last aload_0 of the recursion AbstractInsnNode pnt = last; while (pnt != null && pnt.getOpcode() != 42 && !(pnt.getOpcode() == 25 && ((VarInsnNode) pnt).var == 0)) { pnt = pnt.getPrevious(); } method.instructions.remove(pnt); Collections.reverse(listInstNodes); for (AbstractInsnNode abstractInsnNode : listInstNodes) { method.instructions.insertBefore(last.getPrevious(), abstractInsnNode); } // place instead of return //goto LabelNode startOfMethodLabel = new LabelNode(new Label()); method.instructions.insertBefore(method.instructions.getFirst(), startOfMethodLabel); JumpInsnNode gotoInst = new JumpInsnNode(GOTO, startOfMethodLabel); method.instructions.set(last.getPrevious(), gotoInst); method.instructions.remove(last); ClassWriter cw = new ClassWriter(0); cn.accept(cw); FileOutputStream fos = new FileOutputStream(classFile); fos.write(cw.toByteArray()); fos.close(); } }
From source file:io.moshisho.plugins.OptimizeClasses.java
License:Apache License
private void optimize(File clz) throws Exception { InputStream is = new FileInputStream(clz); ClassReader cr = new ClassReader(is); is.close();//from ww w . j a va2s . c o m ClassNode cn = new ClassNode(); cr.accept(cn, 0); List methods = cn.methods; for (int i = 0; i < methods.size(); ++i) { MethodNode method = (MethodNode) methods.get(i); if (!isStaticllyBound(method)) { continue; } InsnList instructions = method.instructions; AbstractInsnNode last = instructions.getLast(); while (last != null && last.getType() == AbstractInsnNode.LABEL) { last = last.getPrevious(); } if (last == null || !isReturnInstruction(last) || last.getPrevious().getType() != AbstractInsnNode.METHOD_INSN) { continue; } MethodInsnNode methodInv = (MethodInsnNode) last.getPrevious(); if (!isRecursionCall(cn, method, methodInv)) { continue; } getLog().info("TailRec Optimizaing: " + method.name); // get arguments and types String methodDesc = method.desc; String argsDesc = methodDesc.substring(methodDesc.indexOf('(') + 1, methodDesc.indexOf(')')); //System.out.println(argsDesc); // work with Type.getArgumentTypes List<AbstractInsnNode> listInstNodes = new LinkedList<AbstractInsnNode>(); for (int j = argsDesc.length() - 1; j >= 0; j--) { char c = argsDesc.charAt(j); switch (c) { case 'I': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'Z': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'C': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'B': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'S': listInstNodes.add(new VarInsnNode(ISTORE, argsDesc.length() - j)); break; case 'F': listInstNodes.add(new VarInsnNode(FSTORE, argsDesc.length() - j)); break; case 'J': listInstNodes.add(new VarInsnNode(LSTORE, argsDesc.length() - j)); break; case 'D': listInstNodes.add(new VarInsnNode(DSTORE, argsDesc.length() - j)); break; case '[': // TODO: case 'L': // TODO: default: System.out.println("NOT TREATING: " + c); } } // remove the last aload_0 of the recursion AbstractInsnNode pnt = last; while (pnt != null && pnt.getOpcode() != 42 && !(pnt.getOpcode() == 25 && ((VarInsnNode) pnt).var == 0)) { pnt = pnt.getPrevious(); } method.instructions.remove(pnt); Collections.reverse(listInstNodes); for (AbstractInsnNode abstractInsnNode : listInstNodes) { method.instructions.insertBefore(last.getPrevious(), abstractInsnNode); } // place instead of return //goto LabelNode startOfMethodLabel = new LabelNode(new Label()); method.instructions.insertBefore(method.instructions.getFirst(), startOfMethodLabel); JumpInsnNode gotoInst = new JumpInsnNode(GOTO, startOfMethodLabel); method.instructions.set(last.getPrevious(), gotoInst); method.instructions.remove(last); ClassWriter cw = new ClassWriter(0); cn.accept(cw); FileOutputStream fos = new FileOutputStream(clz); fos.write(cw.toByteArray()); fos.close(); } }
From source file:io.tesla.zipcomparator.internal.ClassfileComparator.java
License:Open Source License
private String disassemble(byte[] bytes) { ClassReader reader = new ClassReader(bytes); ClassNode clazz = new ClassNode(); reader.accept(clazz, Opcodes.ASM4 | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); // inner class list gets reordered during pack200 normalization if (clazz.innerClasses != null) { List<InnerClassNode> sorted = new ArrayList<InnerClassNode>(clazz.innerClasses); Collections.sort(sorted, new Comparator<InnerClassNode>() { public int compare(InnerClassNode o1, InnerClassNode o2) { return o1.name.compareTo(o2.name); }//from ww w . jav a 2 s .c om }); clazz.innerClasses = sorted; } // rendering human-readable bytecode is an eyecandy, we can compare ClassNodes directly StringWriter buffer = new StringWriter(); PrintWriter writer = new PrintWriter(buffer); clazz.accept(new TraceClassVisitor(writer)); writer.flush(); writer.close(); return buffer.toString(); }