Java Code Examples for org.apache.bcel.classfile.JavaClass#getPackageName()

The following examples show how to use org.apache.bcel.classfile.JavaClass#getPackageName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PreorderVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void setupVisitorForClass(JavaClass obj) {
    constantPool = obj.getConstantPool();
    thisClass = obj;
    ConstantClass c = (ConstantClass) constantPool.getConstant(obj.getClassNameIndex());
    className = getStringFromIndex(c.getNameIndex());
    dottedClassName = className.replace('/', '.');
    packageName = obj.getPackageName();
    sourceFile = obj.getSourceFileName();
    dottedSuperclassName = obj.getSuperclassName();
    superclassName = dottedSuperclassName.replace('.', '/');

    ClassDescriptor cDesc = DescriptorFactory.createClassDescriptor(className);
    if (!FindBugs.isNoAnalysis()) {
        try {
            thisClassInfo = (ClassInfo) Global.getAnalysisCache().getClassAnalysis(XClass.class, cDesc);
        } catch (CheckedAnalysisException e) {
            throw new AssertionError("Can't find ClassInfo for " + cDesc);
        }
    }

    super.visitJavaClass(obj);
}
 
Example 2
Source File: BadlyOverriddenAdapter.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void visit(JavaClass obj) {
    try {
        methodMap.clear();
        badOverrideMap.clear();
        JavaClass superClass = obj.getSuperClass();
        if (superClass == null) {
            return;
        }
        String packageName = superClass.getPackageName();
        String className = superClass.getClassName();

        // A more generic way to add Adapters would be nice here
        isAdapter = ((className.endsWith("Adapter")) && ("java.awt.event".equals(packageName) || "javax.swing.event".equals(packageName)))
                || (("DefaultHandler".equals(className) && ("org.xml.sax.helpers".equals(packageName))));
        if (isAdapter) {
            Method[] methods = superClass.getMethods();
            for (Method method1 : methods) {
                methodMap.put(method1.getName(), method1.getSignature());
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
 
Example 3
Source File: Storage.java    From j-j-jvm with Apache License 2.0 6 votes vote down vote up
public void addClass(final InputStream classStream, final boolean doNotify) throws IOException {
  final JavaClass jclazz = new ClassParser(classStream, null).parse();

  String packageName = jclazz.getPackageName();
  if (packageName == null || packageName.length() == 0) {
    packageName = "<default>";
  }

  PackageItem packageItem = getPackageForName(packageName);
  if (packageItem == null) {
    packageItem = addPackage(packageName);
  }

  packageItem.addClass(new ClassItem(packageItem, jclazz));

  if (doNotify) {
    for (final TreeModelListener l : this.treeModelListeners) {
      l.treeStructureChanged(new TreeModelEvent(this, new TreePath(this)));
    }
  }
}
 
Example 4
Source File: BCELifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitJavaClass( final JavaClass clazz ) {
    String class_name = clazz.getClassName();
    final String super_name = clazz.getSuperclassName();
    final String package_name = clazz.getPackageName();
    final String inter = Utility.printArray(clazz.getInterfaceNames(), false, true);
    if (!"".equals(package_name)) {
        class_name = class_name.substring(package_name.length() + 1);
        _out.println("package " + package_name + ";");
        _out.println();
    }
    _out.println("import " + BASE_PACKAGE + ".generic.*;");
    _out.println("import " + BASE_PACKAGE + ".classfile.*;");
    _out.println("import " + BASE_PACKAGE + ".*;");
    _out.println("import java.io.*;");
    _out.println();
    _out.println("public class " + class_name + "Creator {");
    _out.println("  private InstructionFactory _factory;");
    _out.println("  private ConstantPoolGen    _cp;");
    _out.println("  private ClassGen           _cg;");
    _out.println();
    _out.println("  public " + class_name + "Creator() {");
    _out.println("    _cg = new ClassGen(\""
            + (("".equals(package_name)) ? class_name : package_name + "." + class_name)
            + "\", \"" + super_name + "\", " + "\"" + clazz.getSourceFileName() + "\", "
            + printFlags(clazz.getAccessFlags(), FLAGS.CLASS) + ", "
            + "new String[] { " + inter + " });");
    _out.println("    _cg.setMajor(" + clazz.getMajor() +");");
    _out.println("    _cg.setMinor(" + clazz.getMinor() +");");
    _out.println();
    _out.println("    _cp = _cg.getConstantPool();");
    _out.println("    _factory = new InstructionFactory(_cg, _cp);");
    _out.println("  }");
    _out.println();
    printCreate();
    final Field[] fields = clazz.getFields();
    if (fields.length > 0) {
        _out.println("  private void createFields() {");
        _out.println("    FieldGen field;");
        for (final Field field : fields) {
            field.accept(this);
        }
        _out.println("  }");
        _out.println();
    }
    final Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        _out.println("  private void createMethod_" + i + "() {");
        methods[i].accept(this);
        _out.println("  }");
        _out.println();
    }
    printMain();
    _out.println("}");
}
 
Example 5
Source File: ClassFilePreDecompilationScan.java    From windup with Eclipse Public License 1.0 4 votes vote down vote up
private void addClassFileMetadata(GraphRewrite event, EvaluationContext context, JavaClassFileModel javaClassFileModel)
{
    try (FileInputStream fis = new FileInputStream(javaClassFileModel.getFilePath()))
    {
        final ClassParser parser = new ClassParser(fis, javaClassFileModel.getFilePath());
        final JavaClass bcelJavaClass = parser.parse();
        final String packageName = bcelJavaClass.getPackageName();

        final String qualifiedName = bcelJavaClass.getClassName();

        final JavaClassService javaClassService = new JavaClassService(event.getGraphContext());
        final JavaClassModel javaClassModel = javaClassService.create(qualifiedName);
        int majorVersion = bcelJavaClass.getMajor();
        int minorVersion = bcelJavaClass.getMinor();

        String simpleName = qualifiedName;
        if (packageName != null && !packageName.isEmpty() && simpleName != null)
        {
            simpleName = StringUtils.substringAfterLast(simpleName, ".");
        }

        javaClassFileModel.setMajorVersion(majorVersion);
        javaClassFileModel.setMinorVersion(minorVersion);
        javaClassFileModel.setPackageName(packageName);

        javaClassModel.setSimpleName(simpleName);
        javaClassModel.setPackageName(packageName);
        javaClassModel.setQualifiedName(qualifiedName);
        javaClassModel.setClassFile(javaClassFileModel);
        javaClassModel.setPublic(bcelJavaClass.isPublic());
        javaClassModel.setInterface(bcelJavaClass.isInterface());

        final String[] interfaceNames = bcelJavaClass.getInterfaceNames();
        if (interfaceNames != null)
        {
            for (final String interfaceName : interfaceNames)
            {
                JavaClassModel interfaceModel = javaClassService.getOrCreatePhantom(interfaceName);
                javaClassService.addInterface(javaClassModel, interfaceModel);
            }
        }

        String superclassName = bcelJavaClass.getSuperclassName();
        if (!bcelJavaClass.isInterface() && !StringUtils.isBlank(superclassName))
            javaClassModel.setExtends(javaClassService.getOrCreatePhantom(superclassName));

        javaClassFileModel.setJavaClass(javaClassModel);
    }
    catch (Exception ex)
    {
        String nl = ex.getMessage() != null ? Util.NL + "\t" : " ";
        final String message = "BCEL was unable to parse class file '" + javaClassFileModel.getFilePath() + "':" + nl + ex.toString();
        LOG.log(Level.WARNING, message);
        ClassificationService classificationService = new ClassificationService(event.getGraphContext());
        classificationService.attachClassification(event, context, javaClassFileModel, UNPARSEABLE_CLASS_CLASSIFICATION, UNPARSEABLE_CLASS_DESCRIPTION);
        javaClassFileModel.setParseError(message);
        javaClassFileModel.setSkipDecompilation(true);
    }
}