Search in sources :

Example 11 with ClassFile

use of org.hotswap.agent.javassist.bytecode.ClassFile in project HotswapAgent by HotswapProjects.

the class FieldInitLink method subtypeOf.

public boolean subtypeOf(CtClass clazz) throws NotFoundException {
    int i;
    String cname = clazz.getName();
    if (this == clazz || getName().equals(cname))
        return true;
    ClassFile file = getClassFile2();
    String supername = file.getSuperclass();
    if (supername != null && supername.equals(cname))
        return true;
    String[] ifs = file.getInterfaces();
    int num = ifs.length;
    for (i = 0; i < num; ++i) if (ifs[i].equals(cname))
        return true;
    if (supername != null && classPool.get(supername).subtypeOf(clazz))
        return true;
    for (i = 0; i < num; ++i) if (classPool.get(ifs[i]).subtypeOf(clazz))
        return true;
    return false;
}
Also used : ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile)

Example 12 with ClassFile

use of org.hotswap.agent.javassist.bytecode.ClassFile in project HotswapAgent by HotswapProjects.

the class ClassPathAnnotationScanner method scanPlugins.

/**
 * Run the scan - search path for files containing annotation.
 *
 * @param classLoader classloader to resolve path
 * @param path        path to scan {@link org.hotswap.agent.util.scanner.Scanner#scan(ClassLoader, String, ScannerVisitor)}
 * @return list of class names containing the annotation
 * @throws IOException scan exception.
 */
public List<String> scanPlugins(ClassLoader classLoader, String path) throws IOException {
    final List<String> files = new LinkedList<String>();
    scanner.scan(classLoader, path, new ScannerVisitor() {

        @Override
        public void visit(InputStream file) throws IOException {
            ClassFile cf;
            try {
                DataInputStream dstream = new DataInputStream(file);
                cf = new ClassFile(dstream);
            } catch (IOException e) {
                throw new IOException("Stream not a valid classFile", e);
            }
            if (hasAnnotation(cf))
                files.add(cf.getName());
        }
    });
    return files;
}
Also used : ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) LinkedList(java.util.LinkedList)

Example 13 with ClassFile

use of org.hotswap.agent.javassist.bytecode.ClassFile in project HotswapAgent by HotswapProjects.

the class FactoryHelper method writeFile0.

private static void writeFile0(ClassFile cf, String directoryName) throws CannotCompileException, IOException {
    String classname = cf.getName();
    String filename = directoryName + File.separatorChar + classname.replace('.', File.separatorChar) + ".class";
    int pos = filename.lastIndexOf(File.separatorChar);
    if (pos > 0) {
        String dir = filename.substring(0, pos);
        if (!dir.equals("."))
            new File(dir).mkdirs();
    }
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
    try {
        cf.write(out);
    } catch (IOException e) {
        throw e;
    } finally {
        out.close();
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 14 with ClassFile

use of org.hotswap.agent.javassist.bytecode.ClassFile in project HotswapAgent by HotswapProjects.

the class Dump method main.

/**
 * Main method.
 *
 * @param args           <code>args[0]</code> is the class file name.
 */
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.err.println("Usage: java Dump <class file name>");
        return;
    }
    DataInputStream in = new DataInputStream(new FileInputStream(args[0]));
    ClassFile w = new ClassFile(in);
    PrintWriter out = new PrintWriter(System.out, true);
    out.println("*** constant pool ***");
    w.getConstPool().print(out);
    out.println();
    out.println("*** members ***");
    ClassFilePrinter.print(w, out);
}
Also used : ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile)

Example 15 with ClassFile

use of org.hotswap.agent.javassist.bytecode.ClassFile in project HotswapAgent by HotswapProjects.

the class AnnotationImpl method getDefault.

private Object getDefault(String name, Method method) throws ClassNotFoundException, RuntimeException {
    String classname = annotation.getTypeName();
    if (pool != null) {
        try {
            CtClass cc = pool.get(classname);
            ClassFile cf = cc.getClassFile2();
            MethodInfo minfo = cf.getMethod(name);
            if (minfo != null) {
                AnnotationDefaultAttribute ainfo = (AnnotationDefaultAttribute) minfo.getAttribute(AnnotationDefaultAttribute.tag);
                if (ainfo != null) {
                    MemberValue mv = ainfo.getDefaultValue();
                    return mv.getValue(classLoader, pool, method);
                }
            }
        } catch (NotFoundException e) {
            throw new RuntimeException("cannot find a class file: " + classname);
        }
    }
    throw new RuntimeException("no default value: " + classname + "." + name + "()");
}
Also used : CtClass(org.hotswap.agent.javassist.CtClass) ClassFile(org.hotswap.agent.javassist.bytecode.ClassFile) AnnotationDefaultAttribute(org.hotswap.agent.javassist.bytecode.AnnotationDefaultAttribute) NotFoundException(org.hotswap.agent.javassist.NotFoundException) MethodInfo(org.hotswap.agent.javassist.bytecode.MethodInfo)

Aggregations

ClassFile (org.hotswap.agent.javassist.bytecode.ClassFile)28 MethodInfo (org.hotswap.agent.javassist.bytecode.MethodInfo)5 IOException (java.io.IOException)4 InnerClassesAttribute (org.hotswap.agent.javassist.bytecode.InnerClassesAttribute)4 ArrayList (java.util.ArrayList)3 AnnotationsAttribute (org.hotswap.agent.javassist.bytecode.AnnotationsAttribute)3 ParameterAnnotationsAttribute (org.hotswap.agent.javassist.bytecode.ParameterAnnotationsAttribute)3 DataInputStream (java.io.DataInputStream)2 InputStream (java.io.InputStream)2 List (java.util.List)2 EnclosingMethodAttribute (org.hotswap.agent.javassist.bytecode.EnclosingMethodAttribute)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataOutputStream (java.io.DataOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 LinkedList (java.util.LinkedList)1 CtClass (org.hotswap.agent.javassist.CtClass)1 NotFoundException (org.hotswap.agent.javassist.NotFoundException)1