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

The following examples show how to use org.apache.bcel.classfile.JavaClass#isFinal() . 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: ClassDumper.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if {@code parentJavaClass} is not {@code final} and {@code childJavaClass} is not
 * overriding any {@code final} method of {@code parentJavaClass}.
 *
 * @see <a href="https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.10">Java
 *     Virtual Machine Specification: 4.10. Verification of class Files</a>
 */
static boolean hasValidSuperclass(JavaClass childJavaClass, JavaClass parentJavaClass) {
  if (parentJavaClass.isFinal()) {
    return false;
  }

  for (Method method : childJavaClass.getMethods()) {
    for (JavaClass parentClass : getClassHierarchy(parentJavaClass)) {
      for (final Method methodInParent : parentClass.getMethods()) {
        if (methodInParent.getName().equals(method.getName())
            && methodInParent.getSignature().equals(method.getSignature())
            && methodInParent.isFinal()) {
          return false;
        }
      }
    }
  }

  return true;
}
 
Example 2
Source File: FindNoSideEffectMethods.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visit(JavaClass obj) {
    super.visit(obj);
    allowedFields = new HashSet<>();
    fieldsModifyingMethods = new HashSet<>();
    subtypes = null;
    if (!obj.isFinal() && !obj.isEnum()) {
        try {
            Subtypes2 subtypes2 = AnalysisContext.currentAnalysisContext().getSubtypes2();
            subtypes = new HashSet<>(subtypes2.getSubtypes(getClassDescriptor()));
            subtypes.remove(getClassDescriptor());
        } catch (ClassNotFoundException e) {
        }
    }
}
 
Example 3
Source File: CloneIdiom.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visit(JavaClass obj) {
    implementsCloneableDirectly = false;
    invokesSuperClone = false;
    cloneOnlyThrowsException = false;
    isCloneable = false;
    check = false;
    isFinal = obj.isFinal();
    if (obj.isInterface()) {
        return;
    }
    if (obj.isAbstract()) {
        return;
    }
    // Does this class directly implement Cloneable?
    String[] interface_names = obj.getInterfaceNames();
    for (String interface_name : interface_names) {
        if ("java.lang.Cloneable".equals(interface_name)) {
            implementsCloneableDirectly = true;
            isCloneable = true;
            break;
        }
    }

    Subtypes2 subtypes2 = AnalysisContext.currentAnalysisContext().getSubtypes2();
    try {
        if (subtypes2.isSubtype(getClassDescriptor(), cloneDescriptor)) {
            isCloneable = true;
        }
        if (subtypes2.isSubtype(DescriptorFactory.createClassDescriptorFromDottedClassName(obj.getSuperclassName()),
                cloneDescriptor)) {
            implementsCloneableDirectly = false;
        }

    } catch (ClassNotFoundException e) {
        bugReporter.reportMissingClass(e);
    }

    hasCloneMethod = false;
    referencesCloneMethod = false;
    check = true;
    super.visit(obj);
}
 
Example 4
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 5
Source File: DeepSubtypeAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static double isDeepSerializable(JavaClass x) throws ClassNotFoundException {
    if (storedException != null) {
        throw storedException;
    }

    if (Values.DOTTED_JAVA_LANG_OBJECT.equals(x.getClassName())) {
        return 0.4;
    }

    if (DEBUG) {
        System.out.println("checking " + x.getClassName());
    }

    double result = Analyze.deepInstanceOf(x, serializable);
    if (result >= 0.9) {
        if (DEBUG) {
            System.out.println("Direct high serializable result: " + result);
        }
        return result;
    }

    if (x.isFinal()) {
        return result;
    }

    double collectionResult = Analyze.deepInstanceOf(x, collection);
    double mapResult = Analyze.deepInstanceOf(x, map);

    if (x.isInterface() || x.isAbstract()) {
        result = Math.max(result, Math.max(mapResult, collectionResult) * 0.95);
        if (result >= 0.9) {
            return result;
        }
    }
    ClassDescriptor classDescriptor = DescriptorFactory.createClassDescriptor(x);

    Subtypes2 subtypes2 = AnalysisContext.currentAnalysisContext().getSubtypes2();

    Set<ClassDescriptor> directSubtypes = subtypes2.getDirectSubtypes(classDescriptor);
    directSubtypes.remove(classDescriptor);


    double confidence = 0.6;
    if (x.isAbstract() || x.isInterface()) {
        confidence = 0.8;
        result = Math.max(result, 0.4);
    } else if (directSubtypes.isEmpty()) {
        confidence = 0.2;
    }

    double confidence2 = (1 + confidence) / 2;
    result = Math.max(result, confidence2 * collectionResult);
    if (result >= 0.9) {
        if (DEBUG) {
            System.out.println("High collection result: " + result);
        }
        return result;
    }
    result = Math.max(result, confidence2 * mapResult);
    if (result >= 0.9) {
        if (DEBUG) {
            System.out.println("High map result: " + result);
        }
        return result;
    }
    result = Math.max(result, confidence2 * 0.5 * Analyze.deepInstanceOf(x, comparator));
    if (result >= 0.9) {
        if (DEBUG) {
            System.out.println("High comparator result: " + result);
        }
        return result;
    }



    for (ClassDescriptor subtype : directSubtypes) {
        JavaClass subJavaClass = Repository.lookupClass(subtype.getDottedClassName());
        result = Math.max(result, confidence * Analyze.deepInstanceOf(subJavaClass, serializable));

        // result = Math.max(result, confidence * isDeepSerializable(subJavaClass));
        if (result >= 0.9) {
            return result;
        }
    }


    if (DEBUG) {
        System.out.println("No high results; max: " + result);
    }
    return result;
}
 
Example 6
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Ensures that every class has a super class and that
 * <B>final</B> classes are not subclassed.
 * This means, the class this Pass2Verifier operates
 * on has proper super classes (transitively) up to
 * java.lang.Object.
 * The reason for really loading (and Pass1-verifying)
 * all of those classes here is that we need them in
 * Pass2 anyway to verify no final methods are overridden
 * (that could be declared anywhere in the ancestor hierarchy).
 *
 * @throws ClassConstraintException otherwise.
 */
private void every_class_has_an_accessible_superclass() {
    try {
    final Set<String> hs = new HashSet<>(); // save class names to detect circular inheritance
    JavaClass jc = Repository.lookupClass(myOwner.getClassName());
    int supidx = -1;

    while (supidx != 0) {
        supidx = jc.getSuperclassNameIndex();

        if (supidx == 0) {
            if (jc != Repository.lookupClass(Type.OBJECT.getClassName())) {
                throw new ClassConstraintException("Superclass of '"+jc.getClassName()+
                        "' missing but not "+Type.OBJECT.getClassName()+" itself!");
            }
        }
        else{
            final String supername = jc.getSuperclassName();
            if (! hs.add(supername)) {    // If supername already is in the list
                throw new ClassConstraintException("Circular superclass hierarchy detected.");
            }
            final Verifier v = VerifierFactory.getVerifier(supername);
            final VerificationResult vr = v.doPass1();

            if (vr != VerificationResult.VR_OK) {
                throw new ClassConstraintException("Could not load in ancestor class '"+supername+"'.");
            }
            jc = Repository.lookupClass(supername);

            if (jc.isFinal()) {
                throw new ClassConstraintException("Ancestor class '"+supername+
                        "' has the FINAL access modifier and must therefore not be subclassed.");
            }
        }
    }

    } catch (final ClassNotFoundException e) {
    // FIXME: this might not be the best way to handle missing classes.
    throw new AssertionViolatedException("Missing class: " + e, e);
    }
}