List of usage examples for org.objectweb.asm.tree ClassNode accept
public void accept(final ClassVisitor classVisitor)
From source file:org.tbot.core.bot.loader.asm.modifiers.adapters.tree.AddInterfaceAdapter.java
License:Open Source License
@Override public void visitEnd() { ClassNode cn = (ClassNode) cv; for (String i : interfacesToAdd) { cn.interfaces.add(i);//w w w.j a v a2 s . c o m System.out.println("[+Interface Addition] " + cn.name + " implements " + i); } cn.accept(next); }
From source file:org.tbot.core.bot.loader.asm.modifiers.adapters.tree.AddMethodAdapter.java
License:Open Source License
@Override public void visitEnd() { ClassNode cn = (ClassNode) cv; for (FieldNode f : cn.fields) { if (fieldName.equals(f.name) && fieldDescriptor.equals(f.desc)) { isFieldPresent = true;/*w w w . j av a 2 s . c o m*/ signature = f.signature; break; } } for (MethodNode mv : cn.methods) { if (getterName.equals(cn.name) && fieldDescriptor.equals(mv.desc)) { isMethodPresent = true; break; } } if (isFieldPresent && !isMethodPresent) { MethodNode mn = new MethodNode(ACC_PUBLIC, getterName, "()" + fieldDescriptor, signature, null); mn.instructions.add(new VarInsnNode(varInsn, 0)); mn.instructions.add(new FieldInsnNode(GETFIELD, cn.name, fieldName, fieldDescriptor)); mn.instructions.add(new InsnNode(retInsn)); cn.methods.add(mn); System.out.println( "[+Method Addition] " + fieldDescriptor + " " + getterName + "() returns " + fieldName); } try { cn.accept(next); } catch (Exception ez) { ez.printStackTrace(); } }
From source file:org.tbot.core.bot.loader.asm.modifiers.adapters.tree.generic.AbstractClassTransform.java
License:Open Source License
/** * Starts up the transform./* ww w . ja v a 2 s . c o m*/ * * @param cn - The class to use. */ public void process(final ClassNode cn) { try { //Set up the ClassWriter. cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); //Delegate cn method calls and data to 'cw'. cn.accept(cw); //Set up the ClassReader cr = new ClassReader(cw.toByteArray()); //Set the current adapter to be the ClassWriter. currentAdapter = cw; } catch (Exception ex) { ex.printStackTrace(); } }
From source file:org.tbot.core.bot.loader.commons.transformations.ClientTransform.java
License:Open Source License
@Override public boolean accept(ClassNode theClass) { Logger.setAppName("ClientTransform"); Logger.printlnInfo("Finding client.class"); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); theClass.accept(cw); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(cw.toByteArray())); ClassParser cp = new ClassParser(dis, theClass.name); int utfIdx = 0; try {//from ww w. j a va 2 s .co m JavaClass jc = cp.parse(); ClassGen cg = new ClassGen(jc); utfIdx = cg.getConstantPool().lookupUtf8("Welcome to Near Reality"); } catch (IOException e) { e.printStackTrace(); } Logger.printlnInfo("Class: " + theClass.name); return theClass.name.equals("client"); }
From source file:org.testeoa.estatica.AnaliseEstatica.java
License:Open Source License
public static Classe lerClasse(String nome) throws ExAnaliseEstatica { try {//from w w w .j av a2 s .c om ClassReader cr = new ClassReader(nome); ClassNode cn = new ClassNode(); cr.accept(cn, ClassReader.SKIP_FRAMES); if (isAspecto(cn)) { throw new ExAnaliseEstatica("[" + nome + "] no uma classe.", null); } Classe c = new Classe(nome); for (MethodNode mn : (List<MethodNode>) cn.methods) { if (isValido(mn)) { c.inserir(new Metodo(mn.name, mn.desc)); } } ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); // TraceClassVisitor tcv = // new TraceClassVisitor(cw, new PrintWriter(System.out)); ClassAdapter adapter = new AdapterClasse(c, cw); cn.accept(adapter); c.setBytecode(cw.toByteArray()); return c; } catch (IOException e) { throw new ExAnaliseEstatica("Erro ao tentar abrir a classe [" + nome + "].", e); } }
From source file:org.testeoa.estatica.AnaliseEstatica.java
License:Open Source License
public static Aspecto lerAspecto(String nome) throws ExAnaliseEstatica { try {// w ww. j a v a 2 s . c o m ClassReader cr = new ClassReader(nome); ClassNode cn = new ClassNode(); cr.accept(cn, ClassReader.SKIP_FRAMES); if (!isAspecto(cn)) { throw new ExAnaliseEstatica("[" + nome + "] no um aspecto.", null); } Aspecto a = new Aspecto(nome); for (MethodNode mn : (List<MethodNode>) cn.methods) { if (isAdendo(mn.name)) { a.inserir(new Adendo(mn.name, mn.desc)); } else { if (isValido(mn)) { a.inserir(new Metodo(mn.name, mn.desc)); } } } ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); // TraceClassVisitor tcv = // new TraceClassVisitor(cw, new PrintWriter(System.out)); ClassAdapter adapter = new AdapterAspecto(a, cw); cn.accept(adapter); a.setBytecode(cw.toByteArray()); return a; } catch (IOException e) { throw new ExAnaliseEstatica("Erro ao tentar abrir o aspecto [" + nome + "].", e); } }
From source file:org.wavescale.hotload.transformer.MethodTransformer.java
License:Open Source License
@Override public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { if (ClassUtil.inTabooListOfPackages(className)) { return classfileBuffer; }/* w w w .java 2 s . co m*/ ClassNode cn = new ClassNode(Opcodes.ASM5); ClassReader cr = new ClassReader(classfileBuffer); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); // create adapter for method insertion. cr.accept(cn, 0); // add the public var arg helper methods for (int i = 0; i < configManager.getNumberOfMethodsToBeAdded(); i++) { String methodName = ClassUtil.getPublicMethodName(classBeingRedefined, "MethodCall", i); if (canAddMethodName(cn, methodName)) { cn.methods.add(new VarArgsHelperMethod(classBeingRedefined, methodName)); } } // get the byte code cn.accept(cw); classfileBuffer = cw.toByteArray(); return classfileBuffer; }
From source file:pku.sei.checkedcoverage.tracer.instrumentation.Transformer.java
License:Creative Commons License
private byte[] transform0(final String className, final String javaClassName, final byte[] classfileBuffer) { final ClassReader reader = new ClassReader(classfileBuffer); final ClassNode classNode = new ClassNode(); reader.accept(classNode, 0);/*from w ww. j a v a2s . com*/ final ClassWriter writer; // we have to synchronize on System.out first. // otherwise it may lead to a deadlock if a thread calls removeStale() on ConcurrentReferenceHashMap // while he holds the lock for System.out, but another thread is inside the transformation step and // waits for the lock of System.out synchronized (System.out) { synchronized (this.transformationLock) { // register that class for later reconstruction of the trace List<Field> fields; if (classNode.fields.isEmpty()) fields = Collections.emptyList(); else fields = new ArrayList<Field>(classNode.fields.size()); final String javaSuperName = Type.getObjectType(classNode.superName).getClassName(); final ReadClass readClass = new ReadClass(className, AbstractInstruction.getNextIndex(), classNode.access, classNode.sourceFile, fields, javaSuperName); for (final Object fieldObj : classNode.fields) { final FieldNode f = (FieldNode) fieldObj; fields.add(new Field(f.name, f.desc, f.access, readClass)); } writer = new FixedClassWriter( COMPUTE_FRAMES ? ClassWriter.COMPUTE_FRAMES : ClassWriter.COMPUTE_MAXS); final ClassVisitor output = this.tracer.check ? new CheckClassAdapter(writer) : writer; if (Arrays.asList(this.pauseTracingClasses).contains(javaClassName) || className.startsWith("java/security/")) { new PauseTracingInstrumenter(readClass, this.tracer).transform(classNode); } else { if ("java/lang/Thread".equals(className)) new ThreadInstrumenter(readClass, this.tracer).transform(classNode); else new TracingClassInstrumenter(readClass, this.tracer).transform(classNode); } new IdentifiableInstrumenter(readClass, this.tracer).transform(classNode); classNode.accept(COMPUTE_FRAMES ? new JSRInliner(output) : output); readClass.setInstructionNumberEnd(AbstractInstruction.getNextIndex()); // now we can write the class out // NOTE: we do not write it out immediately, because this sometimes leads // to circular dependencies! //readClass.writeOut(this.readClassesOutputStream, this.readClassesStringCache); this.readClasses.add(readClass); } } final byte[] newClassfileBuffer = writer.toByteArray(); if (this.tracer.check) { checkClass(newClassfileBuffer, className); } //printClass(newClassfileBuffer, Type.getObjectType(className).getClassName()); /* if (className.endsWith("line/Main")) printClass(newClassfileBuffer, Type.getObjectType(className).getClassName()); */ return newClassfileBuffer; }
From source file:pl.asie.foamfix.coremod.BlockPosPatch.java
License:Open Source License
public static byte[] patchVec3i(byte[] data) { final ClassReader reader = new ClassReader(data); final ClassNode node = new ClassNode(); reader.accept(node, 8);/*from ww w . j a va 2s . c o m*/ for (FieldNode fn : node.fields) { if ("I".equals(fn.desc) && fn.access == (Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL)) { fn.access = Opcodes.ACC_PROTECTED; } } final ClassWriter writer = new ClassWriter(8); node.accept(writer); return writer.toByteArray(); }
From source file:pl.asie.foamfix.coremod.FoamFixTransformer.java
License:Open Source License
public byte[] spliceMethods(final byte[] data, final String className, final String targetClassName, final String... methods) { final Set<String> methodSet = Sets.newHashSet(methods); try {//from w w w . j av a2s .c o m final byte[] dataSplice = ByteStreams.toByteArray( this.getClass().getClassLoader().getResourceAsStream(className.replace('.', '/') + ".class")); final ClassReader readerData = new ClassReader(data); final ClassReader readerSplice = new ClassReader(dataSplice); final ClassWriter writer = new ClassWriter(8); final String className2 = className.replace('.', '/'); final String targetClassName2 = targetClassName.replace('.', '/'); final Remapper remapper = new Remapper() { public String map(final String name) { return className2.equals(name) ? targetClassName2 : name; } }; ClassNode nodeData = new ClassNode(); ClassNode nodeSplice = new ClassNode(); readerData.accept(nodeData, 8); readerSplice.accept(new RemappingClassAdapter(nodeSplice, remapper), 8); for (int i = 0; i < nodeSplice.methods.size(); i++) { if (methodSet.contains(nodeSplice.methods.get(i).name)) { MethodNode mn = nodeSplice.methods.get(i); boolean added = false; for (int j = 0; j < nodeData.methods.size(); j++) { if (nodeData.methods.get(j).name.equals(mn.name) && nodeData.methods.get(j).desc.equals(mn.desc)) { System.out.println("Spliced in: " + targetClassName + "." + mn.name); nodeData.methods.set(j, mn); added = true; break; } } if (!added) { System.out.println("Added: " + targetClassName + "." + mn.name); nodeData.methods.add(mn); added = true; } } } nodeData.accept(writer); return writer.toByteArray(); } catch (IOException e) { e.printStackTrace(); return null; } }