List of usage examples for org.objectweb.asm.tree ClassNode ClassNode
public ClassNode()
From source file:org.jacoco.core.internal.instr.NoneProbeArrayStrategyTest.java
License:Open Source License
@Test public void addMembers_should_not_add_members() { final ClassNode c = new ClassNode(); strategy.addMembers(c, 0);//from w ww .j a va 2 s.com assertEquals(0, c.methods.size()); assertEquals(0, c.fields.size()); }
From source file:org.jacoco.core.test.filter.FinallyTest.java
License:Open Source License
/** * This test studies placement of GOTO instructions. *///from w w w. ja v a2 s .c o m @Test public void gotos() throws IOException { final Source source = Source.getSourceFor("src", Finally.class); final ClassNode classNode = new ClassNode(); new ClassReader(TargetLoader.getClassDataAsBytes(Finally.class)).accept(classNode, 0); final Set<String> tags = new HashSet<String>(); for (final MethodNode m : classNode.methods) { if ("main".equals(m.name)) { // skip it continue; } int lineNumber = -1; for (AbstractInsnNode i = m.instructions.getFirst(); i != null; i = i.getNext()) { if (AbstractInsnNode.LINE == i.getType()) { lineNumber = ((LineNumberNode) i).line; } if (Opcodes.GOTO == i.getOpcode()) { final String line = source.getLine(lineNumber); if (line.indexOf('$') < 0) { throw new AssertionError("No tag at line " + lineNumber); } final String tag = line.substring(line.indexOf('$') + "$line-".length(), line.lastIndexOf('$')); tags.add(tag); } } } final Set<String> expected = new HashSet<String>(); if (isJDKCompiler) { expected.add("example.2"); } else { expected.add("example.0"); } expected.add("breakStatement.for"); if (isJDKCompiler) { expected.add("breakStatement.1"); expected.add("breakStatement.2"); } else { expected.add("breakStatement"); } if (isJDKCompiler) { expected.add("emptyCatch.2"); } else { expected.add("emptyCatch"); expected.add("emptyCatch.1"); } if (isJDKCompiler) { expected.add("catchNotExecuted.2"); } else { expected.add("catchNotExecuted"); expected.add("catchNotExecuted.1"); } if (isJDKCompiler) { expected.add("nested.5"); expected.add("nested.6"); } else { expected.add("nested.0"); expected.add("nested.3"); } if (isJDKCompiler && !isJDK8) { expected.add("emptyTry.2"); } if (!isJDKCompiler) { expected.add("alwaysCompletesAbruptly.0"); } assertEquals(expected, tags); }
From source file:org.jacoco.core.test.validation.java5.FinallyTest.java
License:Open Source License
private Set<String> getTagsWithGotos() throws IOException { final Set<String> gotoTags = new HashSet<String>(); byte[] b = TargetLoader.getClassDataAsBytes(FinallyTarget.class); final ClassNode classNode = new ClassNode(); InstrSupport.classReaderFor(b).accept(classNode, 0); for (final MethodNode m : classNode.methods) { if ("main".equals(m.name)) { // skip it continue; }//from w ww .j a va 2 s . c o m int lineNumber = -1; for (AbstractInsnNode i : m.instructions) { if (AbstractInsnNode.LINE == i.getType()) { lineNumber = ((LineNumberNode) i).line; } if (Opcodes.GOTO == i.getOpcode()) { String tag = tags.get(Integer.valueOf(lineNumber)); if (tag == null) { throw new AssertionError("No tag at line " + lineNumber); } gotoTags.add(tag); } } } return gotoTags; }
From source file:org.jacoco.core.test.validation.java5.StructuredLockingTest.java
License:Open Source License
private void assertStructuredLocking(byte[] source) throws Exception { IRuntime runtime = new SystemPropertiesRuntime(); Instrumenter instrumenter = new Instrumenter(runtime); byte[] instrumented = instrumenter.instrument(source, "TestTarget"); ClassNode cn = new ClassNode(); InstrSupport.classReaderFor(instrumented).accept(cn, 0); for (MethodNode mn : cn.methods) { assertStructuredLocking(cn.name, mn); }//from w w w.j av a 2 s . c o m }
From source file:org.jacoco.core.test.validation.StructuredLockingTest.java
License:Open Source License
private void assertStructuredLocking(byte[] source) throws Exception { IRuntime runtime = new SystemPropertiesRuntime(); Instrumenter instrumenter = new Instrumenter(runtime); byte[] instrumented = instrumenter.instrument(source, "TestTarget"); ClassNode cn = new ClassNode(); new ClassReader(instrumented).accept(cn, 0); for (MethodNode mn : cn.methods) { assertStructuredLocking(cn.name, mn); }//from w w w.java 2 s . c o m }
From source file:org.jacoco.playground.filter.MethodDumper.java
License:Open Source License
protected static final MethodNode getMethod(String classfile, String name, String desc) throws IOException { ClassNode node = new ClassNode(); InputStream in = new FileInputStream(classfile); new ClassReader(in).accept(node, ClassReader.EXPAND_FRAMES); in.close();//from w ww.j ava 2 s . c o m for (Object m : node.methods) { MethodNode method = (MethodNode) m; if (name.equals(method.name) && desc.equals(method.desc)) { return method; } } fail(String.format("Method %s%s not found.", name, desc)); return null; }
From source file:org.jetbrains.android.anko.ClassTreeTest.java
License:Apache License
@Test(expected = NoSuchClassException.class) public void testNoSuchClassException() throws Exception { ClassTree tree = new ClassTree(); ClassNode cn = new ClassNode(); cn.name = "java.lang.Integer"; tree.isSuccessorOf(cn, "java.util.List"); }
From source file:org.jetbrains.android.anko.ClassTreeTest.java
License:Apache License
private static ClassNode createNode(String name, String superName) { ClassNode node = new ClassNode(); node.name = name;//from w ww . j av a 2s . c o m node.superName = superName; return node; }
From source file:org.jooby.internal.apitool.BytecodeRouteParser.java
License:Apache License
private ClassNode loadClass(String name) { return Try.apply(() -> { String cname = name.replace("/", "."); ClassNode node = cache.get(cname); if (node == null) { String rname = cname.replace(".", "/") + ".class"; try (InputStream in = loader.getResourceAsStream(rname)) { if (in == null) { throw new FileNotFoundException(rname + " using " + loader); }//ww w .ja va2s .c o m ClassReader reader = new ClassReader(ByteStreams.toByteArray(in)); node = new ClassNode(); reader.accept(node, 0); cache.put(cname, node); if (log.isDebugEnabled()) { log.info("Source: {}; Class: {}", node.sourceFile, node.name); reader.accept( new TraceClassVisitor(null, new ASMifier(), new PrintWriter(writer(log, name))), 0); } } } return node; }).get(); }
From source file:org.jvnet.jax_ws_commons.beans_generator.ambassador.impl.asm.ASMRequestBeanGenerator.java
License:Open Source License
public byte[] generate() { ClassNode tree = new ClassNode(); tree.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC, methodInfo.getRequestBeanClassName().replace(".", "/"), null, Type.getInternalName(Object.class), new String[0]); generateNoArgsConstructor();// ww w .ja v a 2s . c o m generateAnnotations(); generateFields(); tree.visitEnd(); tree.accept(writer); return writer.toByteArray(); }