List of usage examples for org.objectweb.asm.tree ClassNode ClassNode
public ClassNode()
From source file:org.friz.bytecode.Container.java
License:Open Source License
public Container(File file) { this.elements = new HashMap<>(); this.others = new HashMap<>(); this.refactorer = new Refactorer(this); try {//w w w. ja v a 2 s. c om if (file.getPath().endsWith(".jar")) { this.jar = true; try (JarFile jar = new JarFile(file)) { this.main_class = jar.getManifest().getMainAttributes().getValue("Main-Class"); Enumeration<JarEntry> enumeration = jar.entries(); while (enumeration.hasMoreElements()) { JarEntry next = enumeration.nextElement(); if (next.getName().endsWith(".class")) { ClassReader reader = new ClassReader(jar.getInputStream(next)); ClassNode node = new ClassNode(); reader.accept(node, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); ClassElement element = new ClassElement(node); elements.put(element.name(), element); } else if (!next.getName().contains("META-INF")) { others.put(next.getName(), ByteBuffer.wrap(ByteStreams.toByteArray(jar.getInputStream(next)))); } } jar.close(); } } else if (file.getPath().endsWith(".class")) { this.jar = false; ClassReader reader = new ClassReader(new FileInputStream(file)); ClassNode node = new ClassNode(); reader.accept(node, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); ClassElement element = new ClassElement(node); elements.put(element.name(), element); } logger.info("Loaded " + elements.size() + " Class(es)."); } catch (IOException e) { logger.error("Error loading class(es)!", e); } }
From source file:org.friz.bytecode.Container.java
License:Open Source License
public ClassElement getClass(String name) { if (name == null) { return null; }//w ww .jav a2s . c om ClassElement c = elements.get(name); if (c == null) { ClassNode n = new ClassNode(); ClassReader cr; try { cr = new ClassReader(name); } catch (IOException e) { return null; } cr.accept(n, 0); c = new ClassElement(n); } return c; }
From source file:org.friz.bytecode.Container.java
License:Open Source License
public String getName(String name) { if (name == null) { return null; }//from ww w. j a va 2s. c om String newName = null; if (refactorer().classes().containsKey(name)) newName = refactorer().classes().get(name).getRefName(); else { ClassElement el = elements.get(name); if (el != null) newName = el.name(); } if (newName == null) { ClassNode n = new ClassNode(); ClassReader cr; try { cr = new ClassReader(name); } catch (IOException e) { return null; } cr.accept(n, 0); newName = n.name; } return newName; }
From source file:org.friz.bytecode.refactor.Refactorer.java
License:Open Source License
public void transform() { Map<String, ClassElement> refactored = new HashMap<>(); for (ClassElement el : container().elements()) { String oldName = el.name(); ClassReader cr = new ClassReader(getClassNodeBytes(el.node())); ClassWriter cw = new ClassWriter(cr, 0); RemappingClassAdapter rca = new RemappingClassAdapter(cw, mapper); cr.accept(rca, ClassReader.EXPAND_FRAMES); cr = new ClassReader(cw.toByteArray()); ClassNode cn = new ClassNode(); cr.accept(cn, 0);//from w w w . ja va 2 s. c om refactored.put(oldName, new ClassElement(cn)); } for (Map.Entry<String, ClassElement> factor : refactored.entrySet()) { container.relocate(factor.getKey(), factor.getValue()); } }
From source file:org.glassfish.pfl.tf.tools.enhancer.Transformer.java
License:Open Source License
public byte[] evaluate(final byte[] arg) { final ClassNode cn = new ClassNode(); final ClassReader cr = new ClassReader(arg); cr.accept(cn, ClassReader.SKIP_FRAMES); // Ignore annotations and interfaces. if (util.hasAccess(cn.access, Opcodes.ACC_ANNOTATION) || util.hasAccess(cn.access, Opcodes.ACC_INTERFACE)) { return null; }//w w w. j a v a 2 s . c om try { ecd = new EnhancedClassDataASMImpl(util, annotationNames, cn); // If this class is not annotated as a traced class, ignore it. if (!ecd.isTracedClass()) { return null; } processTimers(); byte[] phase1 = null; if ((mode == EnhanceTool.ProcessingMode.UpdateSchemas) || (mode == EnhanceTool.ProcessingMode.TraceEnhance)) { phase1 = util.transform(false, arg, new UnaryFunction<ClassVisitor, ClassAdapter>() { public ClassAdapter evaluate(ClassVisitor arg) { return new ClassEnhancer(util, ecd, arg); } }); } // Only communication from part 1 to part2 is a byte[] and // the EnhancedClassData. A runtime version can be regenerated // as above from the byte[] from the class file as it is presented // to a ClassFileTransformer. // Implementation note: runtime would keep byte[] stored of // original version whenever tracing is enabled, so that // disabling tracing simply means using a ClassFileTransformer // to get back to the original code. // // Then add tracing code (part 2). // This is a pure visitor using the AdviceAdapter. // It must NOT modify its input visitor (or you get an // infinite series of calls to onMethodExit...) if (mode == EnhanceTool.ProcessingMode.TraceEnhance) { final byte[] phase2 = util.transform(util.getDebug(), phase1, new UnaryFunction<ClassVisitor, ClassAdapter>() { public ClassAdapter evaluate(ClassVisitor arg) { return new ClassTracer(util, ecd, arg); } }); return phase2; } else { return phase1; } } catch (TraceEnhancementException exc) { if (util.getDebug()) { util.info(1, "Could not enhance file: " + exc); } return null; } }
From source file:org.glowroot.agent.weaving.WeavingClassVisitor.java
License:Apache License
@RequiresNonNull("type") private void addMixin(MixinType mixinType) { ClassReader cr = new ClassReader(mixinType.implementationBytes()); ClassNode cn = new ClassNode(); cr.accept(cn, ClassReader.EXPAND_FRAMES); // SuppressWarnings because generics are explicitly removed from asm binaries // see http://forge.ow2.org/tracker/?group_id=23&atid=100023&func=detail&aid=316377 @SuppressWarnings("unchecked") List<FieldNode> fieldNodes = cn.fields; for (FieldNode fieldNode : fieldNodes) { fieldNode.accept(this); }//from w w w. j a v a 2 s . c om // SuppressWarnings because generics are explicitly removed from asm binaries @SuppressWarnings("unchecked") List<MethodNode> methodNodes = cn.methods; for (MethodNode mn : methodNodes) { if (mn.name.equals("<init>")) { continue; } // SuppressWarnings because generics are explicitly removed from asm binaries @SuppressWarnings("unchecked") String[] exceptions = Iterables.toArray(mn.exceptions, String.class); MethodVisitor mv = cw.visitMethod(mn.access, mn.name, mn.desc, mn.signature, exceptions); mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv, new SimpleRemapper(cn.name, type.getInternalName()))); } }
From source file:org.glowroot.weaving.WeavingClassVisitor.java
License:Apache License
@RequiresNonNull("type") private void addMixin(MixinType mixinType) { ClassReader cr = new ClassReader(mixinType.implementationBytes()); ClassNode cn = new ClassNode(); cr.accept(cn, ClassReader.EXPAND_FRAMES); // SuppressWarnings because generics are explicitly removed from asm binaries // see http://forge.ow2.org/tracker/?group_id=23&atid=100023&func=detail&aid=316377 @SuppressWarnings("unchecked") List<FieldNode> fieldNodes = cn.fields; for (FieldNode fieldNode : fieldNodes) { fieldNode.accept(this); }/*w ww .ja va 2 s. c o m*/ // SuppressWarnings because generics are explicitly removed from asm binaries @SuppressWarnings("unchecked") List<MethodNode> methodNodes = cn.methods; for (MethodNode mn : methodNodes) { if (mn.name.equals("<init>")) { continue; } // SuppressWarnings because generics are explicitly removed from asm binaries @SuppressWarnings("unchecked") String[] exceptions = Iterables.toArray(mn.exceptions, String.class); MethodVisitor mv = cv.visitMethod(mn.access, mn.name, mn.desc, mn.signature, exceptions); checkNotNull(mv); mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv, new SimpleRemapper(cn.name, type.getInternalName()))); } }
From source file:org.hua.ast.visitors.BytecodeGeneratorASTVisitor.java
@Override public void visit(ClassDefinition node) throws ASTVisitorException { ClassNode classNode = new ClassNode(); cn = classNode;/*from w w w . jav a2s . c om*/ cn.version = Opcodes.V1_5; cn.superName = "java/lang/Object"; cn.name = node.getIdentifier(); cn.access = Opcodes.ACC_PUBLIC; // create constructor mn = new MethodNode(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null); mn.instructions.add(new VarInsnNode(Opcodes.ALOAD, 0)); mn.instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false)); mn.instructions.add(new InsnNode(Opcodes.RETURN)); mn.maxLocals = 1; mn.maxStack = 1; classNode.methods.add(mn); node.getFfDefinitions().accept(this); // IMPORTANT: this should be dynamically calculated // use COMPUTE_MAXS when computing the ClassWriter, // e.g. new ClassWriter(ClassWriter.COMPUTE_MAXS) ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); // ClassVisitor ca = new CheckClassAdapter(cw); // cn.accept(new CheckClassAdapter(ca)); TraceClassVisitor cv = new TraceClassVisitor(cw, new PrintWriter(System.out)); cn.accept(cv); //get code byte code[] = cw.toByteArray(); Logger.getLogger(BytecodeGeneratorASTVisitor.class.getName()) .info("Writing " + node.getIdentifier() + " class to .class file"); // update to file FileOutputStream fos; try { fos = new FileOutputStream(node.getIdentifier() + ".class"); fos.write(code); fos.close(); } catch (FileNotFoundException ex) { Logger.getLogger(BytecodeGeneratorASTVisitor.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(BytecodeGeneratorASTVisitor.class.getName()).log(Level.SEVERE, null, ex); } cn = null; System.out.println("##### finished writing to file"); }
From source file:org.jacoco.core.internal.instr.CondyProbeArrayStrategyTest.java
License:Open Source License
@Test public void should_not_add_fields() { final ClassNode c = new ClassNode(); strategy.addMembers(c, 1); assertEquals(0, c.fields.size()); }
From source file:org.jacoco.core.internal.instr.CondyProbeArrayStrategyTest.java
License:Open Source License
@Test public void should_add_bootstrap_method() { final ClassNode c = new ClassNode(); strategy.addMembers(c, 1);/*from w ww. j a va2 s . co m*/ assertEquals(1, c.methods.size()); final MethodNode m = c.methods.get(0); assertEquals(Opcodes.ACC_SYNTHETIC | Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC, m.access); assertEquals("$jacocoInit", m.name); assertEquals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/Class;)[Z", m.desc); assertEquals(4, m.maxStack); assertEquals(3, m.maxLocals); }