List of usage examples for org.objectweb.asm.util CheckClassAdapter verify
public static void verify(final ClassReader classReader, final boolean printResults, final PrintWriter printWriter)
From source file:Test1.java
License:Apache License
private static void implementInterface(ClassNode cn) throws Exception { // MethodNode fromMethod = new MethodNode(ACC_PUBLIC, "fromData", // "(Ljava/io/DataInput;)V", null, new String[] { // "java/io/IOException", // "java/lang/ClassNotFoundException" }); // mv = cw.visitMethod(ACC_PUBLIC, "toData", "(Ljava/io/DataOutput;)V", // null, new String[] { "java/io/IOException" }); MethodNode toMethod = new MethodNode(ACC_PUBLIC, "toData", "(Ljava/io/DataOutput;)V", null, new String[] { "java/io/IOException" }); InsnList instToMethod = toMethod.instructions; for (int i = 0; i < cn.fields.size(); i++) { FieldNode fn = (FieldNode) cn.fields.get(i); toMethod(cn.name, fn, instToMethod); }// w ww.j a v a2 s.co m instToMethod.add(new InsnNode(RETURN)); cn.methods.add(toMethod); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); cn.accept(cw); byte[] bt = cw.toByteArray(); ClassReader cr = new ClassReader(bt); CheckClassAdapter ca = new CheckClassAdapter(cw); FileOutputStream fo = new FileOutputStream("d:/x1.log"); PrintWriter pw = new PrintWriter(fo); ca.verify(cr, true, pw); ByteArrayClassLoader bacl = new ByteArrayClassLoader(bt); Class cls = bacl.loadClass("ynd.test.Ac01"); DataSerializable di = (DataSerializable) cls.newInstance(); di.toData(null); }
From source file:ch.eiafr.cojac.Agent.java
License:Apache License
@Override public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { // TODO: verify if we correctly handle lambdas... : for them className==null; // we try to discover the "enclosing class" from classfileBuffer, // to decide if we should instrument or not. // if (VERBOSE && className==null) { // System.out.println("null className... "+extractedClassname(classfileBuffer)); // //dumpIt(classfileBuffer); // //CheckClassAdapter.verify(new ClassReader(classfileBuffer), true, new PrintWriter(System.out)); // }//from w w w. j a v a2s . com if (className == null) className = extractedClassname(classfileBuffer); try { if ("ch/eiafr/cojac/models/FloatReplacerClasses".equals(className)) { if (VERBOSE) { System.out.println("Agent handling the FloatReplacerClasses under " + loader); } return setGlobalFields(classfileBuffer, loader); } if (!references.hasToBeInstrumented(className)) { if (VERBOSE) { System.out.println("Agent NOT instrumenting " + className + " under " + loader); } return classfileBuffer; } if (VERBOSE) { System.out.println("Agent instrumenting " + className + " under " + loader); } byte[] instrumented = instrumenter.instrument(classfileBuffer, loader); trackForDebuggingPurposes(className, instrumented); if (VERBOSE) { /* The interfaces are loaded by this class, the loading of a class by the agent is done without the instrumentation. Once the interface is loaded, it is never reloaded for the same classloader. That means the interface will never be instrumented in verbose mode. */ if (!REPLACE_FLOATS) CheckClassAdapter.verify(new ClassReader(instrumented), PRINT_INSTR_RESULT, new PrintWriter(System.out)); } return instrumented; } catch (Throwable e) { System.err.println("COJAC-AGENT EXCEPTION FOR CLASS " + className); e.printStackTrace(); // Otherwise it'll be hidden! System.err.flush(); throw e; } }
From source file:com.friz.instruction.Main.java
License:Open Source License
public static void main(String[] args) throws IOException, AnalyzerException { NodeTable<ClassNode> nt = new NodeTable<ClassNode>(); SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<ClassNode>( new JarInfo(new File("gamepack.jar"))); dl.download();/*from w w w . j a v a 2s. c o m*/ nt.putAll(dl.getJarContents().getClassContents().namedMap()); IContext ctx = new IContext() { @Override public NodeTable<ClassNode> getNodes() { return nt; } }; final ClassTree tree = new ClassTree(ctx.getNodes()); final Map<String, ClassFactory> factories = new HashMap<>(); for (ClassNode cn : tree) { factories.put(cn.name, new ClassFactory(cn)); } for (GraphInstructionVisitor visitor : graphs) { visitor.start(); for (ClassFactory cf : factories.values()) { for (ClassMethod cm : cf.methods) { InstructionTree.build(cm).accept(visitor); } } for (ClassFactory cf : factories.values()) { visitor.finish(cf); } visitor.clock(); } ; for (InstructionVisitor visitor : visitors) { visitor.start(); for (ClassFactory cf : factories.values()) { for (ClassMethod cm : cf.methods) { InstructionTree.build(cm).accept(visitor); } } visitor.clock(); } Timer.start(); for (ClassFactory cf : factories.values()) { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); cf.node.accept(cw); ClassReader cr = new ClassReader(cw.toByteArray()); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); CheckClassAdapter.verify(cr, false, pw); if (sw.toString().length() > 0 && !sw.toString().contains("ClassNotFound")) { System.out.println(sw.toString().substring(0, 1000)); //System.out.println(sw.toString()); System.exit(0); } } Timer.clock("Verified " + factories.size() + " Class(es) in "); CompleteResolvingJarDumper dumper = new CompleteResolvingJarDumper(dl); dumper.dump(new File("transformed.jar")); }
From source file:com.gargoylesoftware.js.nashorn.internal.tools.nasgen.Main.java
License:Open Source License
private static void verify(final byte[] buf) { final ClassReader cr = new ClassReader(buf); CheckClassAdapter.verify(cr, false, new PrintWriter(System.err)); }
From source file:com.github.cojac.Agent.java
License:Apache License
@Override public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { // TODO: verify if we correctly handle lambdas... : for them className==null; // we try to discover the "enclosing class" from classfileBuffer, // to decide if we should instrument or not. // if (VERBOSE && className==null) { // System.out.println("null className... "+extractedClassname(classfileBuffer)); // //dumpIt(classfileBuffer); // //CheckClassAdapter.verify(new ClassReader(classfileBuffer), true, new PrintWriter(System.out)); // }/* w ww.j ava 2 s . c o m*/ if (className == null) className = extractedClassname(classfileBuffer); try { if ("com/github/cojac/models/FloatReplacerClasses".equals(className)) { if (VERBOSE) { System.out.println("Agent handling the FloatReplacerClasses under " + loader); } return setGlobalFields(classfileBuffer, loader); } if (!references.hasToBeInstrumented(className)) { if (VERBOSE) { System.out.println("Agent NOT instrumenting " + className + " under " + loader); } return classfileBuffer; } if (VERBOSE) { System.out.println("Agent instrumenting " + className + " under " + loader); } byte[] instrumented = instrumenter.instrument(classfileBuffer, loader); trackForDebuggingPurposes(className, instrumented); if (VERBOSE) { /* The interfaces are loaded by this class, the loading of a class by the agent is done without the instrumentation. Once the interface is loaded, it is never reloaded for the same classloader. That means the interface will never be instrumented in verbose mode. */ if (!REPLACE_FLOATS) CheckClassAdapter.verify(new ClassReader(instrumented), PRINT_INSTR_RESULT, new PrintWriter(System.out)); } return instrumented; } catch (Throwable e) { System.err.println("COJAC-AGENT EXCEPTION FOR CLASS " + className); e.printStackTrace(); // Otherwise it'll be hidden! System.err.flush(); throw e; } }
From source file:com.navercorp.pinpoint.profiler.instrument.ASMAspectWeaverTest.java
License:Apache License
private Object getInstnace(final String originalName, final String aspectName) throws Exception { final ClassLoader defaultClassLoader = Thread.currentThread().getContextClassLoader(); final ClassLoader classLoader = new ClassLoader() { @Override/* ww w . j av a 2 s . c o m*/ public Class<?> loadClass(String name) throws ClassNotFoundException { if (name.equals(originalName)) { try { final ClassReader cr = new ClassReader( getClass().getResourceAsStream("/" + name.replace('.', '/') + ".class")); final ClassNode classNode = new ClassNode(); cr.accept(classNode, 0); final ASMClassNodeAdapter sourceClassNode = new ASMClassNodeAdapter(defaultClassLoader, classNode); final ASMClassNodeAdapter adviceClassNode = ASMClassNodeAdapter.get(defaultClassLoader, aspectName.replace('.', '/')); final ASMAspectWeaver aspectWeaver = new ASMAspectWeaver(); aspectWeaver.weaving(sourceClassNode, adviceClassNode); final ClassWriter cw = new ClassWriter(0); classNode.accept(cw); final byte[] bytecode = cw.toByteArray(); CheckClassAdapter.verify(new ClassReader(bytecode), false, new PrintWriter(System.out)); return super.defineClass(name, bytecode, 0, bytecode.length); } catch (Exception ex) { throw new ClassNotFoundException("Load error: " + ex.toString(), ex); } } else { return super.loadClass(name); } } }; Class clazz = classLoader.loadClass(originalName); return clazz.newInstance(); }
From source file:com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapterTestMain.java
License:Apache License
private void addInterceptor(final String className) throws Exception { final boolean trace = false; final boolean verify = false; final String classInternalName = className.replace('/', '.'); ClassLoader classLoader = new ClassLoader() { @Override/*from w w w . ja v a2 s.co m*/ public Class<?> loadClass(String name) throws ClassNotFoundException { System.out.println("## load=" + name + ", internal=" + classInternalName); if (!name.startsWith("java") && !name.startsWith("sun") && super.findLoadedClass(name) == null) { try { ClassNode classNode = ASMClassNodeLoader.get(name.replace('.', '/')); ASMClass asmClass = new ASMClass(null, interceptorRegistryBinder, null, classNode); if (asmClass.isInterceptable()) { for (InstrumentMethod method : asmClass.getDeclaredMethods()) { try { method.addInterceptor(interceptorId); } catch (Throwable t) { t.printStackTrace(); } } } byte[] bytes = asmClass.toBytecode(); System.out.println("bytes=" + bytes + ", " + bytes.length); if (trace) { ClassReader classReader = new ClassReader(bytes); ClassWriter cw = new ClassWriter(0); TraceClassVisitor tcv = new TraceClassVisitor(cw, new PrintWriter(System.out)); classReader.accept(tcv, 0); } if (verify) { CheckClassAdapter.verify(new ClassReader(bytes), false, new PrintWriter(System.out)); } System.out.println("define"); return super.defineClass(name, bytes, 0, bytes.length); } catch (Throwable ex) { ex.printStackTrace(); return null; } } else { try { return super.loadClass(name); } catch (Throwable t) { t.printStackTrace(); return null; } } } }; try { classLoader.loadClass(classInternalName); } catch (ClassNotFoundException cnfe) { } }
From source file:com.sun.btrace.runtime.InstrumentorTestBase.java
License:Open Source License
protected void checkTransformation(String expected) throws IOException { org.objectweb.asm.ClassReader cr = new org.objectweb.asm.ClassReader(transformedBC); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); CheckClassAdapter.verify(cr, true, pw); if (sw.toString().contains("AnalyzerException")) { System.err.println(sw.toString()); fail();/* w w w .ja v a2 s .c o m*/ } String diff = diff(); if (DEBUG) { System.err.println(diff); } assertEquals(expected, diff.substring(0, diff.length() > expected.length() ? expected.length() : diff.length())); }
From source file:com.sun.btrace.runtime.InstrumentorTestBase.java
License:Open Source License
protected void checkTrace(String expected) throws IOException { org.objectweb.asm.ClassReader cr = new org.objectweb.asm.ClassReader(traceCode); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); CheckClassAdapter.verify(cr, false, pw); if (sw.toString().contains("AnalyzerException")) { System.err.println(sw.toString()); fail();/*from w w w . j a v a 2 s . co m*/ } }
From source file:com.sun.fortress.compiler.asmbytecodeoptimizer.ByteCodeVisitor.java
License:Open Source License
public void toAsm(JarOutputStream jos) { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); // ClassWriter cw = new ClassWriter(0); CheckClassAdapter cca = new CheckClassAdapter(cw); cca.visit(version, access, name, sig, superName, interfaces); Iterator it = fieldVisitors.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); ByteCodeFieldVisitor fv = (ByteCodeFieldVisitor) pairs.getValue(); fv.toAsm(cca);/*ww w . j av a2 s .c om*/ } it = methodVisitors.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); ByteCodeMethodVisitor mv = (ByteCodeMethodVisitor) pairs.getValue(); mv.toAsm(cca); } byte[] bytes = cw.toByteArray(); if (ProjectProperties.getBoolean("fortress.bytecode.verify", false)) { PrintWriter pw = new PrintWriter(System.out); CheckClassAdapter.verify(new ClassReader(bytes), true, pw); } System.out.println("About to write " + name); ByteCodeWriter.writeJarredClass(jos, name, bytes); }