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

The following examples show how to use org.apache.bcel.classfile.JavaClass#isPublic() . 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: LinkageChecker.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the {@code javaClass} is accessible {@code from sourceClassName} in terms of
 * the access modifiers in the {@code javaClass}.
 *
 * @see <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-ClassModifier">
 *     JLS 8.1.1. Class Modifiers</a>
 */
private boolean isClassAccessibleFrom(JavaClass javaClass, String sourceClassName)
    throws ClassNotFoundException {
  if (javaClass.isPrivate()) {
    // Nested class can be declared as private. Class reference within same file is allowed to
    // access private class. However, such cases are already filtered at
    // errorsFromSymbolReferences.
    return false;
  }

  String targetClassName = javaClass.getClassName();
  if (javaClass.isPublic()
      || ClassDumper.classesInSamePackage(targetClassName, sourceClassName)) {
    String enclosingClassName = ClassDumper.enclosingClassName(targetClassName);
    if (enclosingClassName != null) {
      // Nested class can be declared as private or protected, in addition to
      // public and package private. Protected is treated same as package private.
      // https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-ClassModifier
      JavaClass enclosingJavaClass = classDumper.loadJavaClass(enclosingClassName);
      return isClassAccessibleFrom(enclosingJavaClass, sourceClassName);
    } else {
      // Top-level class can be declared as public or package private.
      return true;
    }
  } else {
    // The class is not public and not in the same package as the source class.
    return false;
  }
}
 
Example 2
Source File: ObjectType.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
/**
 * Java Virtual Machine Specification edition 2, � 5.4.4 Access Control
 */
public boolean accessibleTo(ObjectType accessor) {
  JavaClass jc = Repository.lookupClass(class_name);

  if(jc.isPublic()) {
    return true;
  } else {
    JavaClass acc = Repository.lookupClass(accessor.class_name);
    return acc.getPackageName().equals(jc.getPackageName());
  }
}
 
Example 3
Source File: PublicSemaphores.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitClassContext(ClassContext classContext) {
    JavaClass cls = classContext.getJavaClass();
    if ((!cls.isPublic()) || (cls.getClassName().indexOf('$') >= 0)) {
        return;
    }

    alreadyReported = false;
    super.visitClassContext(classContext);
}
 
Example 4
Source File: ObjectType.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Java Virtual Machine Specification edition 2, � 5.4.4 Access Control
 * @throws ClassNotFoundException if the class referenced by this type
 *   can't be found
 */
public boolean accessibleTo( final ObjectType accessor ) throws ClassNotFoundException {
    final JavaClass jc = Repository.lookupClass(className);
    if (jc.isPublic()) {
        return true;
    }
    final JavaClass acc = Repository.lookupClass(accessor.className);
    return acc.getPackageName().equals(jc.getPackageName());
}
 
Example 5
Source File: Naming.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visit(JavaClass obj) {
    String name = obj.getClassName();
    String[] parts = name.split("[$+.]");
    baseClassName = parts[parts.length - 1];
    for (String p : name.split("[.]")) {
        if (p.length() == 1) {
            return;
        }
    }
    if (name.indexOf("Proto$") >= 0) {
        return;
    }
    classIsPublicOrProtected = obj.isPublic() || obj.isProtected();
    if (Character.isLetter(baseClassName.charAt(0)) && !Character.isUpperCase(baseClassName.charAt(0))
            && baseClassName.indexOf('_') == -1) {
        int priority = classIsPublicOrProtected ? NORMAL_PRIORITY : LOW_PRIORITY;

        bugReporter.reportBug(new BugInstance(this, "NM_CLASS_NAMING_CONVENTION", priority).addClass(this));
    }
    if (name.endsWith("Exception")) {
        // Does it ultimately inherit from Throwable?
        if (!mightInheritFromException(DescriptorFactory.createClassDescriptor(obj))) {
            // It doens't, so the name is misleading
            bugReporter.reportBug(new BugInstance(this, "NM_CLASS_NOT_EXCEPTION", NORMAL_PRIORITY).addClass(this));
        }
    }

    int badFieldNames = 0;
    for (Field f : obj.getFields()) {
        if (f.getName().length() >= 2 && badFieldName(f)) {
            badFieldNames++;
        }
    }
    hasBadFieldNames = badFieldNames > 3 && badFieldNames > obj.getFields().length / 3;
    int badMethodNames = 0;
    for (Method m : obj.getMethods()) {
        if (badMethodName(m.getName())) {
            badMethodNames++;
        }
    }
    hasBadMethodNames = badMethodNames > 3 && badMethodNames > obj.getMethods().length / 3;
    isEclipseNLS = "org.eclipse.osgi.util.NLS".equals(obj.getSuperclassName());
    super.visit(obj);
}
 
Example 6
Source File: InheritanceUnsafeGetResource.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visit(JavaClass obj) {
    classIsFinal = obj.isFinal();
    reportedForThisClass = false;
    classIsVisibleToOtherPackages = obj.isPublic() || obj.isProtected();
}
 
Example 7
Source File: FindReturnRef.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visit(JavaClass obj) {
    publicClass = obj.isPublic();
    super.visit(obj);
}