Example usage for org.objectweb.asm ClassReader SKIP_CODE

List of usage examples for org.objectweb.asm ClassReader SKIP_CODE

Introduction

In this page you can find the example usage for org.objectweb.asm ClassReader SKIP_CODE.

Prototype

int SKIP_CODE

To view the source code for org.objectweb.asm ClassReader SKIP_CODE.

Click Source Link

Document

A flag to skip the Code attributes.

Usage

From source file:ca.weblite.asm.ASMClassLoader.java

public ClassNode findClass(Type type) {
    if (nodeCache.containsKey(type.getInternalName())) {
        return nodeCache.get(type.getInternalName());
    }/*from  w ww. j  a  v a2  s . com*/
    String classFile = type.getInternalName() + ".class";
    while (true) {
        InputStream bytecode = loader.getResourceAsStream(classFile);
        if (bytecode != null) {
            try {
                ClassNode node = new ClassNode();
                ClassReader reader = new ClassReader(bytecode);
                reader.accept(node, ClassReader.SKIP_CODE);
                if ((node.name + ".class").equals(classFile)) {
                    nodeCache.put(type.getInternalName(), node);
                    return node;
                }
            } catch (IOException ex) {
                Logger.getLogger(ASMClassLoader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        int lastSlash = classFile.lastIndexOf("/");
        if (lastSlash == -1) {
            break;
        }
        classFile = classFile.substring(0, lastSlash) + "$" + classFile.substring(lastSlash + 1);
    }

    return super.findClass(type);
}

From source file:ca.weblite.asm.JavaSourceClassLoader.java

@Override
public ClassNode findClass(Type type) {
    String classFile = type.getInternalName() + ".class";
    ClassNode cached = cacheClassLoader.findClass(type);
    long cacheMtime = cacheClassLoader.getLastModified();

    String javaFile = type.getInternalName() + ".java";
    while (true) {
        File file = loader.getResource(javaFile);
        if (file != null) {
            try (FileInputStream bytecode = new FileInputStream(file)) {
                if (cached != null && cacheMtime >= lastModified) {
                    // The cached version is up to date
                    // no need to load the class
                    return cached;
                }//w w w .  j ava2s.  c om

                ClassNode node = new ClassNode();
                ClassReader reader = new ClassReader(bytecode);
                reader.accept(node, ClassReader.SKIP_CODE);
                if ((node.name + ".java").equals(javaFile)) {
                    return node;
                }
            } catch (IOException ex) {
                Logger.getLogger(JavaSourceClassLoader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        int lastSlash = javaFile.lastIndexOf("/");
        if (lastSlash == -1) {
            break;
        }
        javaFile = javaFile.substring(0, lastSlash) + ".java";
    }

    return super.findClass(type);
}

From source file:chibill.DeobLoader.loader.Remappers.java

License:Open Source License

private String getFieldType(String owner, String name) {
    if (fieldDescriptions.containsKey(owner)) {
        return fieldDescriptions.get(owner).get(name);
    }/* www .j  a  va  2 s  .  c  o m*/
    synchronized (fieldDescriptions) {
        byte[] classBytes = Byte;
        if (classBytes == null) {
            return null;
        }
        ClassReader cr = new ClassReader(classBytes);
        ClassNode classNode = new ClassNode();
        cr.accept(classNode, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
        Map<String, String> resMap = Maps.newHashMap();
        for (FieldNode fieldNode : (List<FieldNode>) classNode.fields) {
            resMap.put(fieldNode.name, fieldNode.desc);
        }
        fieldDescriptions.put(owner, resMap);
        return resMap.get(name);

    }
}

From source file:co.cask.cdap.app.runtime.spark.SparkRunnerClassLoader.java

License:Apache License

/**
 * Find the return type of the ActorSystem.dispatcher() method. It is ExecutionContextExecutor in
 * Akka 2.3 (Spark 1.2+) and ExecutionContext in Akka 2.2 (Spark < 1.2, which CDAP doesn't support,
 * however the Spark 1.5 in CDH 5.6. still has Akka 2.2, instead of 2.3).
 *
 * @return the return type of the ActorSystem.dispatcher() method or {@code null} if no such method
 *///from w  w w.j  a  va2 s . c o m
@Nullable
private Type determineAkkaDispatcherReturnType() {
    try (InputStream is = openResource("akka/actor/ActorSystem.class")) {
        if (is == null) {
            return null;
        }
        final AtomicReference<Type> result = new AtomicReference<>();
        ClassReader cr = new ClassReader(is);
        cr.accept(new ClassVisitor(Opcodes.ASM5) {
            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature,
                    String[] exceptions) {
                if (name.equals("dispatcher") && Type.getArgumentTypes(desc).length == 0) {
                    // Expected to be either ExecutionContext (akka 2.2, only in CDH spark)
                    // or ExecutionContextExecutor (akka 2.3, for open source, HDP spark).
                    Type returnType = Type.getReturnType(desc);
                    if (returnType.equals(EXECUTION_CONTEXT_TYPE)
                            || returnType.equals(EXECUTION_CONTEXT_EXECUTOR_TYPE)) {
                        result.set(returnType);
                    } else {
                        LOG.warn("Unsupported return type of ActorSystem.dispatcher(): {}",
                                returnType.getClassName());
                    }
                }
                return super.visitMethod(access, name, desc, signature, exceptions);
            }
        }, ClassReader.SKIP_DEBUG | ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES);
        return result.get();
    } catch (IOException e) {
        LOG.warn("Failed to determine ActorSystem dispatcher() return type.", e);
        return null;
    }
}

From source file:co.cask.cdap.common.security.AuthEnforceRewriter.java

License:Apache License

@Override
public byte[] rewriteClass(String className, InputStream input) throws IOException {
    byte[] classBytes = ByteStreams.toByteArray(input);
    // First pass: Check the class to have a method with AuthEnforce annotation if found store the annotation details
    // and parameters for the method for second pass in which class rewrite will be performed.
    ClassReader cr = new ClassReader(classBytes);

    // SKIP_CODE SKIP_DEBUG and SKIP_FRAMESto make the first pass faster since in the first pass we just want to
    // process annotations to check if the class has any method with AuthEnforce annotation. If such method is found
    // we also store the parameters which has the named annotations as specified in the entities field of the
    // AuthEnforce.
    AuthEnforceAnnotationVisitor classVisitor = new AuthEnforceAnnotationVisitor(className);
    cr.accept(classVisitor, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);

    Map<Method, AnnotationDetail> methodAnnotations = classVisitor.getMethodAnnotations();
    if (methodAnnotations.isEmpty()) {
        // if no AuthEnforce annotation was found then return the original class bytes
        return classBytes;
    }//w ww .j  av a2  s  .  c o  m
    // We found some method which has AuthEnforce annotation so we need a second pass in to rewrite the class
    // in second pass we COMPUTE_FRAMES and visit classes with EXPAND_FRAMES
    ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
    cr.accept(
            new AuthEnforceAnnotationRewriter(className, cw, classVisitor.getFieldDetails(), methodAnnotations),
            ClassReader.EXPAND_FRAMES);
    return cw.toByteArray();
}

From source file:co.cask.cdap.internal.app.runtime.artifact.ArtifactInspector.java

License:Apache License

/**
 * Detects if a class is annotated with {@link Plugin} without loading the class.
 *
 * @param className name of the class//from  w w  w  .  ja va  2  s . com
 * @param classLoader ClassLoader for loading the class file of the given class
 * @return true if the given class is annotated with {@link Plugin}
 */
private boolean isPlugin(String className, ClassLoader classLoader) {
    try (InputStream is = classLoader.getResourceAsStream(className.replace('.', '/') + ".class")) {
        if (is == null) {
            return false;
        }

        // Use ASM to inspect the class bytecode to see if it is annotated with @Plugin
        final boolean[] isPlugin = new boolean[1];
        ClassReader cr = new ClassReader(is);
        cr.accept(new ClassVisitor(Opcodes.ASM5) {
            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                if (Plugin.class.getName().equals(Type.getType(desc).getClassName()) && visible) {
                    isPlugin[0] = true;
                }
                return super.visitAnnotation(desc, visible);
            }
        }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);

        return isPlugin[0];
    } catch (IOException e) {
        // If failed to open the class file, then it cannot be a plugin
        LOG.warn("Failed to open class file for {}", className, e);
        return false;
    }
}

From source file:co.paralleluniverse.common.reflection.AnnotationUtil.java

License:Open Source License

private static boolean hasClassAnnotation(Class<? extends Annotation> annClass, ClassReader r) {
    // annotationName = annotationName.replace('.', '/');
    final String annDesc = Type.getDescriptor(annClass);
    final AtomicBoolean res = new AtomicBoolean(false);
    r.accept(new ClassVisitor(Opcodes.ASM4) {
        @Override// w ww  . ja v a2s .  c  om
        public void visit(int version, int access, String name, String signature, String superName,
                String[] interfaces) {
        }

        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            if (desc.equals(annDesc))
                res.set(true);
            return null;
        }
    }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG);
    return res.get();
}

From source file:co.paralleluniverse.common.reflection.ASMUtil.java

License:Open Source License

public static ClassNode getClassNode(String className, boolean skipCode, ClassLoader cl) throws IOException {
    if (className == null)
        return null;
    try (InputStream is = cl.getResourceAsStream(classToResource(className))) {
        if (is == null)
            throw new IOException("Resource " + classToResource(className) + " not found.");
        ClassReader cr = new ClassReader(is);
        ClassNode cn = new ClassNode();
        cr.accept(cn, ClassReader.SKIP_DEBUG | (skipCode ? 0 : ClassReader.SKIP_CODE));
        return cn;
    }/*ww w. j av  a  2s  .c om*/
}

From source file:co.paralleluniverse.common.reflection.ASMUtil.java

License:Open Source License

public static ClassNode getClassNode(File classFile, boolean skipCode) throws IOException {
    if (classFile == null)
        return null;
    if (!classFile.exists())
        return null;
    try (InputStream is = new FileInputStream(classFile)) {
        ClassReader cr = new ClassReader(is);
        ClassNode cn = new ClassNode();
        cr.accept(cn, ClassReader.SKIP_DEBUG | (skipCode ? 0 : ClassReader.SKIP_CODE));
        return cn;
    }//from w w w. ja  v  a2  s.c  o  m
}

From source file:co.paralleluniverse.fibers.instrument.MethodDatabase.java

License:Open Source License

private CheckInstrumentationVisitor checkFileAndClose(InputStream is, String name) {
    try {// w  ww. j a  v a  2 s .com
        try {
            ClassReader r = new ClassReader(is);

            CheckInstrumentationVisitor civ = new CheckInstrumentationVisitor(this);
            r.accept(civ, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES | ClassReader.SKIP_CODE);

            return civ;
        } finally {
            is.close();
        }
    } catch (UnableToInstrumentException ex) {
        throw ex;
    } catch (Exception ex) {
        error(name, ex);
    }
    return null;
}