Search in sources :

Example 1 with ClassReader

use of org.powermock.api.mockito.repackaged.asm.ClassReader in project powermock by powermock.

the class DebuggingClassWriter method toByteArray.

public byte[] toByteArray() {
    return (byte[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

        public Object run() {
            byte[] b = DebuggingClassWriter.super.toByteArray();
            if (debugLocation != null) {
                String dirs = className.replace('.', File.separatorChar);
                try {
                    new File(debugLocation + File.separatorChar + dirs).getParentFile().mkdirs();
                    File file = new File(new File(debugLocation), dirs + ".class");
                    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                    try {
                        out.write(b);
                    } finally {
                        out.close();
                    }
                    if (traceEnabled) {
                        file = new File(new File(debugLocation), dirs + ".asm");
                        out = new BufferedOutputStream(new FileOutputStream(file));
                        try {
                            ClassReader cr = new ClassReader(b);
                            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
                            TraceClassVisitor tcv = new TraceClassVisitor(null, pw);
                            cr.accept(tcv, 0);
                            pw.flush();
                        } finally {
                            out.close();
                        }
                    }
                } catch (IOException e) {
                    throw new CodeGenerationException(e);
                }
            }
            return b;
        }
    });
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) TraceClassVisitor(org.powermock.api.mockito.repackaged.asm.util.TraceClassVisitor) FileOutputStream(java.io.FileOutputStream) ClassReader(org.powermock.api.mockito.repackaged.asm.ClassReader) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) PrintWriter(java.io.PrintWriter)

Example 2 with ClassReader

use of org.powermock.api.mockito.repackaged.asm.ClassReader in project powermock by powermock.

the class TraceClassVisitor method main.

/**
     * Prints a disassembled view of the given class to the standard output. <p>
     * Usage: TraceClassVisitor [-debug] &lt;fully qualified class name or class
     * file name &gt;
     *
     * @param args the command line arguments.
     *
     * @throws Exception if the class cannot be found, or if an IO exception
     *         occurs.
     */
public static void main(final String[] args) throws Exception {
    int i = 0;
    int flags = ClassReader.SKIP_DEBUG;
    boolean ok = true;
    if (args.length < 1 || args.length > 2) {
        ok = false;
    }
    if (ok && "-debug".equals(args[0])) {
        i = 1;
        flags = 0;
        if (args.length != 2) {
            ok = false;
        }
    }
    if (!ok) {
        System.err.println("Prints a disassembled view of the given class.");
        System.err.println("Usage: TraceClassVisitor [-debug] " + "<fully qualified class name or class file name>");
        return;
    }
    ClassReader cr;
    if (args[i].endsWith(".class") || args[i].indexOf('\\') > -1 || args[i].indexOf('/') > -1) {
        cr = new ClassReader(new FileInputStream(args[i]));
    } else {
        cr = new ClassReader(args[i]);
    }
    cr.accept(new TraceClassVisitor(new PrintWriter(System.out)), getDefaultAttributes(), flags);
}
Also used : ClassReader(org.powermock.api.mockito.repackaged.asm.ClassReader) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter)

Example 3 with ClassReader

use of org.powermock.api.mockito.repackaged.asm.ClassReader in project powermock by powermock.

the class AbstractClassGenerator method create.

protected Object create(Object key) {
    try {
        Class gen = null;
        synchronized (source) {
            ClassLoader loader = getClassLoader();
            Map cache2 = null;
            cache2 = (Map) source.cache.get(loader);
            if (cache2 == null) {
                cache2 = new HashMap();
                cache2.put(NAME_KEY, new HashSet());
                source.cache.put(loader, cache2);
            } else if (useCache) {
                Reference ref = (Reference) cache2.get(key);
                gen = (Class) ((ref == null) ? null : ref.get());
            }
            if (gen == null) {
                Object save = CURRENT.get();
                CURRENT.set(this);
                try {
                    this.key = key;
                    if (attemptLoad) {
                        try {
                            gen = loader.loadClass(getClassName());
                        } catch (ClassNotFoundException e) {
                        // ignore
                        }
                    }
                    if (gen == null) {
                        byte[] b = strategy.generate(this);
                        String className = ClassNameReader.getClassName(new ClassReader(b));
                        getClassNameCache(loader).add(className);
                        gen = ReflectUtils.defineClass(className, b, loader);
                    }
                    if (useCache) {
                        cache2.put(key, new WeakReference(gen));
                    }
                    return firstInstance(gen);
                } finally {
                    CURRENT.set(save);
                }
            }
        }
        return firstInstance(gen);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new CodeGenerationException(e);
    }
}
Also used : HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) Reference(java.lang.ref.Reference) WeakReference(java.lang.ref.WeakReference) WeakReference(java.lang.ref.WeakReference) ClassReader(org.powermock.api.mockito.repackaged.asm.ClassReader) Map(java.util.Map) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) HashSet(java.util.HashSet)

Example 4 with ClassReader

use of org.powermock.api.mockito.repackaged.asm.ClassReader in project powermock by powermock.

the class AbstractClassLoader method loadClass.

public Class loadClass(String name) throws ClassNotFoundException {
    Class loaded = findLoadedClass(name);
    if (loaded != null) {
        if (loaded.getClassLoader() == this) {
            return loaded;
        }
    //else reload with this class loader
    }
    if (!filter.accept(name)) {
        return super.loadClass(name);
    }
    ClassReader r;
    try {
        java.io.InputStream is = classPath.getResourceAsStream(name.replace('.', '/') + ".class");
        if (is == null) {
            throw new ClassNotFoundException(name);
        }
        try {
            r = new ClassReader(is);
        } finally {
            is.close();
        }
    } catch (IOException e) {
        throw new ClassNotFoundException(name + ":" + e.getMessage());
    }
    try {
        ClassWriter w = new DebuggingClassWriter(ClassWriter.COMPUTE_MAXS);
        getGenerator(r).generateClass(w);
        byte[] b = w.toByteArray();
        Class c = super.defineClass(name, b, 0, b.length, DOMAIN);
        postProcess(c);
        return c;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new CodeGenerationException(e);
    }
}
Also used : DebuggingClassWriter(org.powermock.api.mockito.repackaged.cglib.core.DebuggingClassWriter) CodeGenerationException(org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException) ClassReader(org.powermock.api.mockito.repackaged.asm.ClassReader) IOException(java.io.IOException) ClassWriter(org.powermock.api.mockito.repackaged.asm.ClassWriter) DebuggingClassWriter(org.powermock.api.mockito.repackaged.cglib.core.DebuggingClassWriter) CodeGenerationException(org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException) IOException(java.io.IOException)

Example 5 with ClassReader

use of org.powermock.api.mockito.repackaged.asm.ClassReader in project powermock by powermock.

the class ASMifierClassVisitor method main.

/**
     * Prints the ASM source code to generate the given class to the standard
     * output. <p> Usage: ASMifierClassVisitor [-debug] &lt;fully qualified
     * class name or class file name&gt;
     *
     * @param args the command line arguments.
     *
     * @throws Exception if the class cannot be found, or if an IO exception
     *         occurs.
     */
public static void main(final String[] args) throws Exception {
    int i = 0;
    int flags = ClassReader.SKIP_DEBUG;
    boolean ok = true;
    if (args.length < 1 || args.length > 2) {
        ok = false;
    }
    if (ok && "-debug".equals(args[0])) {
        i = 1;
        flags = 0;
        if (args.length != 2) {
            ok = false;
        }
    }
    if (!ok) {
        System.err.println("Prints the ASM code to generate the given class.");
        System.err.println("Usage: ASMifierClassVisitor [-debug] " + "<fully qualified class name or class file name>");
        return;
    }
    ClassReader cr;
    if (args[i].endsWith(".class") || args[i].indexOf('\\') > -1 || args[i].indexOf('/') > -1) {
        cr = new ClassReader(new FileInputStream(args[i]));
    } else {
        cr = new ClassReader(args[i]);
    }
    cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), getDefaultAttributes(), flags);
}
Also used : ClassReader(org.powermock.api.mockito.repackaged.asm.ClassReader) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter)

Aggregations

ClassReader (org.powermock.api.mockito.repackaged.asm.ClassReader)6 PrintWriter (java.io.PrintWriter)4 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)2 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Reference (java.lang.ref.Reference)1 WeakReference (java.lang.ref.WeakReference)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 WeakHashMap (java.util.WeakHashMap)1 ClassWriter (org.powermock.api.mockito.repackaged.asm.ClassWriter)1 TraceClassVisitor (org.powermock.api.mockito.repackaged.asm.util.TraceClassVisitor)1 CodeGenerationException (org.powermock.api.mockito.repackaged.cglib.core.CodeGenerationException)1 DebuggingClassWriter (org.powermock.api.mockito.repackaged.cglib.core.DebuggingClassWriter)1