Search in sources :

Example 66 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project jop by jop-devel.

the class TransitiveHull method start.

/**
 * Start traversal using DescendingVisitor pattern.
 */
public void start() {
    while (!_queue.empty()) {
        JavaClass clazz = _queue.dequeue();
        _cp = clazz.getConstantPool();
        new org.apache.bcel.classfile.DescendingVisitor(clazz, this).visit();
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass)

Example 67 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project jop by jop-devel.

the class OldAppInfo method load.

/**
 * Load all classes that belong to the application.
 * @throws ClassNotFoundException
 */
public void load() throws IOException, ClassNotFoundException {
    JavaClass[] jcl = new JavaClass[clsArgs.size()];
    Repository.setRepository(SyntheticRepository.getInstance(classpath));
    Iterator<String> i = clsArgs.iterator();
    for (int nr = 0; i.hasNext(); ++nr) {
        String clname = i.next();
        jcl[nr] = Repository.lookupClass(clname);
        if (jcl[nr] == null)
            throw new IOException("lookupClass(" + clname + ") failed");
        System.out.println(jcl[nr].getClassName());
    }
    TransitiveHull hull = new TransitiveHull(classpath, jcl);
    String[] excl = new String[exclude.size()];
    hull.setExcluded(exclude.toArray(excl));
    hull.start();
    System.out.println(Arrays.asList(hull.getClassNames()));
    JavaClass[] jclazz = hull.getClasses();
    // jclazz now contains the closure of the application
    cliMap = template.genClassInfoMap(jclazz, this);
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) IOException(java.io.IOException)

Example 68 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project jop by jop-devel.

the class ConstantPoolReferenceFinder method findReferencedMembers.

/**
 * Find all referenced members in a class.
 * @param classInfo the class to search.
 * @param checkMembers if false, do not check fields and methods. Else check everything.
 * @return a set of class names and class member signatures found in the class.
 */
public static Set<String> findReferencedMembers(ClassInfo classInfo, boolean checkMembers) {
    // Else we need to go into details..
    Set<String> members = new HashSet<String>();
    ClassMemberVisitor visitor = new ClassMemberVisitor(members);
    JavaClass javaClass = classInfo.compile();
    Set<Integer> ids = findPoolReferences(classInfo, javaClass);
    List<InvokeSite> invokes = new ArrayList<InvokeSite>();
    if (checkMembers) {
        for (Field field : javaClass.getFields()) {
            FieldInfo fieldInfo = classInfo.getFieldInfo(field.getName());
            // add members found in the field
            visitor.visitField(fieldInfo);
            // there are no invokesites in a field, only add found ids
            ids.addAll(findPoolReferences(fieldInfo, field));
        }
        for (Method method : javaClass.getMethods()) {
            MethodInfo methodInfo = classInfo.getMethodInfo(method.getName() + method.getSignature());
            // add members found in the method
            visitor.visitMethod(methodInfo);
            // add all ids for checking, add all invoke sites
            ids.addAll(findPoolReferences(methodInfo, method));
        }
    }
    // fill the members list with all found constantpool references
    visitor.visitClass(classInfo);
    visitPoolReferences(classInfo, visitor, ids);
    return members;
}
Also used : ArrayList(java.util.ArrayList) ConstantString(org.apache.bcel.classfile.ConstantString) Method(org.apache.bcel.classfile.Method) EnclosingMethod(com.jopdesign.common.bcel.EnclosingMethod) ConstantInteger(org.apache.bcel.classfile.ConstantInteger) Field(org.apache.bcel.classfile.Field) JavaClass(org.apache.bcel.classfile.JavaClass) MethodInfo(com.jopdesign.common.MethodInfo) ConstantMethodInfo(com.jopdesign.common.type.ConstantMethodInfo) InvokeSite(com.jopdesign.common.code.InvokeSite) ConstantFieldInfo(com.jopdesign.common.type.ConstantFieldInfo) FieldInfo(com.jopdesign.common.FieldInfo) HashSet(java.util.HashSet)

Example 69 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project candle-decompiler by bradsdavis.

the class CandleDecompiler method decompile.

public void decompile(String clazzName, Writer writer) throws DecompilerException {
    Validate.isTrue(org.apache.commons.lang.StringUtils.isNotBlank(clazzName), "Classname is required field, but was not provided.");
    Validate.notNull(writer, "Writer is required, but not provided.");
    JavaClass clz;
    try {
        clz = Repository.lookupClass(clazzName);
        decompile(clz, writer);
    } catch (ClassNotFoundException e) {
        throw new DecompilerException("Exception while decompiling.", e);
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass)

Example 70 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project kanonizo by kanonizo.

the class Framework method loadClassFromFile.

private Class<?> loadClassFromFile(File file) {
    Class<?> cl = null;
    try {
        ClassParser parser = new ClassParser(file.getAbsolutePath());
        JavaClass jcl = parser.parse();
        List<String> forbiddenClasses = Arrays.asList(FORBIDDEN_CLASSNAMES.split(","));
        forbiddenClasses = forbiddenClasses.stream().filter(name -> !name.isEmpty()).collect(Collectors.toList());
        if (forbiddenClasses.size() > 0 && forbiddenClasses.stream().anyMatch(f -> jcl.getClassName().substring(jcl.getPackageName().length() + 1).startsWith(f))) {
            logger.info("Ignoring class " + jcl.getClassName() + " because it is forbidden");
            return null;
        } else {
            cl = Class.forName(jcl.getClassName(), true, Thread.currentThread().getContextClassLoader());
        }
    } catch (ClassNotFoundException | NoClassDefFoundError e) {
        logger.error(e);
    } catch (IOException e) {
        logger.error(e);
    } catch (ExceptionInInitializerError e) {
        logger.error(e);
    }
    return cl;
}
Also used : Arrays(java.util.Arrays) Reflections(org.reflections.Reflections) GsonBuilder(com.google.gson.GsonBuilder) TypeAdapter(com.google.gson.TypeAdapter) ClassUnderTest(org.kanonizo.framework.objects.ClassUnderTest) TestCase(org.kanonizo.framework.objects.TestCase) APLCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APLCFunction) CoverageWriter(org.kanonizo.reporting.CoverageWriter) Gson(com.google.gson.Gson) Method(java.lang.reflect.Method) TestCaseSelectionListener(org.kanonizo.listeners.TestCaseSelectionListener) ParameterisedTestCase(org.kanonizo.framework.objects.ParameterisedTestCase) SearchAlgorithm(org.kanonizo.algorithms.SearchAlgorithm) TestingUtils(org.kanonizo.junit.TestingUtils) Expose(com.google.gson.annotations.Expose) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Serializable(java.io.Serializable) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Util(org.kanonizo.util.Util) Logger(org.apache.logging.log4j.Logger) PropertyChangeListener(java.beans.PropertyChangeListener) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) Display(org.kanonizo.display.Display) InstrumentedFitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.InstrumentedFitnessFunction) APBCFunction(org.kanonizo.algorithms.metaheuristics.fitness.APBCFunction) Parameters(org.junit.runners.Parameterized.Parameters) Parameter(com.scythe.instrumenter.InstrumentationProperties.Parameter) ClassParser(org.apache.bcel.classfile.ClassParser) JsonReader(com.google.gson.stream.JsonReader) APFDFunction(org.kanonizo.algorithms.metaheuristics.fitness.APFDFunction) ArrayList(java.util.ArrayList) CsvWriter(org.kanonizo.reporting.CsvWriter) Instrumenter(org.kanonizo.framework.instrumentation.Instrumenter) Prerequisite(org.kanonizo.annotations.Prerequisite) JsonWriter(com.google.gson.stream.JsonWriter) PropertyChangeEvent(java.beans.PropertyChangeEvent) MutationSearchAlgorithm(org.kanonizo.algorithms.MutationSearchAlgorithm) JavaClass(org.apache.bcel.classfile.JavaClass) FitnessFunction(org.kanonizo.algorithms.metaheuristics.fitness.FitnessFunction) MiscStatsWriter(org.kanonizo.reporting.MiscStatsWriter) Iterator(java.util.Iterator) NullInstrumenter(org.kanonizo.instrumenters.NullInstrumenter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Algorithm(org.kanonizo.annotations.Algorithm) TestCaseOrderingWriter(org.kanonizo.reporting.TestCaseOrderingWriter) File(java.io.File) SystemUnderTest(org.kanonizo.framework.objects.SystemUnderTest) PropertyChangeSupport(java.beans.PropertyChangeSupport) FileReader(java.io.FileReader) NullDisplay(org.kanonizo.display.NullDisplay) Comparator(java.util.Comparator) LogManager(org.apache.logging.log4j.LogManager) JavaClass(org.apache.bcel.classfile.JavaClass) IOException(java.io.IOException) ClassParser(org.apache.bcel.classfile.ClassParser)

Aggregations

JavaClass (org.apache.bcel.classfile.JavaClass)144 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)45 BugInstance (edu.umd.cs.findbugs.BugInstance)43 Method (org.apache.bcel.classfile.Method)28 ToString (com.mebigfatguy.fbcontrib.utils.ToString)27 Field (org.apache.bcel.classfile.Field)17 HashSet (java.util.HashSet)14 HashMap (java.util.HashMap)11 ClassParser (org.apache.bcel.classfile.ClassParser)10 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)8 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)8 XField (edu.umd.cs.findbugs.ba.XField)7 Nullable (javax.annotation.Nullable)7 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)7 Type (org.apache.bcel.generic.Type)7 Iterator (java.util.Iterator)6 List (java.util.List)6 Map (java.util.Map)6 Set (java.util.Set)6