Java Code Examples for org.apache.bcel.classfile.JavaClass#isAbstract()
The following examples show how to use
org.apache.bcel.classfile.JavaClass#isAbstract() .
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: BetaDetector.java From google-http-java-client with Apache License 2.0 | 6 votes |
/** * Reports bug in case the method defined by the given name and signature is {@link Beta}. * * <p>The method is searched in current class and all super classses as well. */ private void checkMethod(String methodName, String signature) { JavaClass javaClass = checkClass(); if (javaClass == null) { return; } for (JavaClass current = javaClass; current != null; current = getSuperclass(current)) { for (Method method : current.getMethods()) { if (methodName.equals(method.getName()) && signature.equals(method.getSignature())) { // method has been found - check if it's beta if (isBeta(method.getAnnotationEntries())) { bugReporter.reportBug(createBugInstance(BETA_METHOD_USAGE).addCalledMethod(this)); } return; } } } if (!javaClass.isAbstract()) { bugReporter.logError( "Can't locate method " + javaClass.getClassName() + "." + methodName + signature); } }
Example 2
Source File: ComparatorIdiom.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
@Override public void visit(JavaClass obj) { if (Subtypes2.instanceOf(obj, "java.util.Comparator") && !ClassName.isLocalOrAnonymous(getClassName()) && !Subtypes2.instanceOf(obj, "java.io.Serializable")) { int priority = NORMAL_PRIORITY; if (obj.isInterface() || obj.isAbstract()) { return; } double easilySerializable = 1.0; for (Field f : obj.getFields()) { try { if (f.getName().startsWith("this$")) { return; } String signature = f.getSignature(); char firstChar = signature.charAt(0); if (firstChar == 'L' || firstChar == '[') { easilySerializable *= DeepSubtypeAnalysis.isDeepSerializable(signature); } } catch (ClassNotFoundException e) { easilySerializable = 0.0; break; } } if (easilySerializable < 0.9) { priority = LOW_PRIORITY; } bugReporter.reportBug(new BugInstance(this, "SE_COMPARATOR_SHOULD_BE_SERIALIZABLE", priority).addClass(this)); } }
Example 3
Source File: LinkageChecker.java From cloud-opensource-java with Apache License 2.0 | 4 votes |
/** * Returns the linkage errors for unimplemented methods in {@code classFile}. Such unimplemented * methods manifest as {@link AbstractMethodError} in runtime. */ private ImmutableList<SymbolProblem> findInterfaceProblems( ClassFile classFile, InterfaceSymbol interfaceSymbol) { String interfaceName = interfaceSymbol.getClassBinaryName(); if (classDumper.isSystemClass(interfaceName)) { return ImmutableList.of(); } ImmutableList.Builder<SymbolProblem> builder = ImmutableList.builder(); try { JavaClass implementingClass = classDumper.loadJavaClass(classFile.getBinaryName()); if (implementingClass.isAbstract()) { // Abstract class does not need to implement methods in an interface. return ImmutableList.of(); } JavaClass interfaceDefinition = classDumper.loadJavaClass(interfaceName); for (Method interfaceMethod : interfaceDefinition.getMethods()) { if (interfaceMethod.getCode() != null) { // This interface method has default implementation. Subclass does not have to implement // it. continue; } String interfaceMethodName = interfaceMethod.getName(); String interfaceMethodDescriptor = interfaceMethod.getSignature(); boolean methodFound = false; Iterable<JavaClass> typesToCheck = Iterables.concat(getClassHierarchy(implementingClass)); for (JavaClass javaClass : typesToCheck) { for (Method method : javaClass.getMethods()) { if (method.getName().equals(interfaceMethodName) && method.getSignature().equals(interfaceMethodDescriptor)) { methodFound = true; break; } } } if (!methodFound) { MethodSymbol missingMethodOnClass = new MethodSymbol( classFile.getBinaryName(), interfaceMethodName, interfaceMethodDescriptor, false); builder.add( new SymbolProblem(missingMethodOnClass, ErrorType.ABSTRACT_METHOD, classFile)); } } } catch (ClassNotFoundException ex) { // Missing classes are reported by findSymbolProblem method. } return builder.build(); }
Example 4
Source File: LinkageChecker.java From cloud-opensource-java with Apache License 2.0 | 4 votes |
private ImmutableList<SymbolProblem> findAbstractParentProblems( ClassFile classFile, SuperClassSymbol superClassSymbol) { ImmutableList.Builder<SymbolProblem> builder = ImmutableList.builder(); String superClassName = superClassSymbol.getClassBinaryName(); if (classDumper.isSystemClass(superClassName)) { return ImmutableList.of(); } try { String className = classFile.getBinaryName(); JavaClass implementingClass = classDumper.loadJavaClass(className); if (implementingClass.isAbstract()) { return ImmutableList.of(); } JavaClass superClass = classDumper.loadJavaClass(superClassName); if (!superClass.isAbstract()) { return ImmutableList.of(); } JavaClass abstractClass = superClass; // Equality of BCEL's Method class is on its name and descriptor field Set<Method> implementedMethods = new HashSet<>(); implementedMethods.addAll(ImmutableList.copyOf(implementingClass.getMethods())); while (abstractClass.isAbstract()) { for (Method abstractMethod : abstractClass.getMethods()) { if (!abstractMethod.isAbstract()) { // This abstract method has implementation. Subclass does not have to implement it. implementedMethods.add(abstractMethod); } else if (!implementedMethods.contains(abstractMethod)) { String unimplementedMethodName = abstractMethod.getName(); String unimplementedMethodDescriptor = abstractMethod.getSignature(); MethodSymbol missingMethodOnClass = new MethodSymbol( className, unimplementedMethodName, unimplementedMethodDescriptor, false); builder.add( new SymbolProblem(missingMethodOnClass, ErrorType.ABSTRACT_METHOD, classFile)); } } abstractClass = abstractClass.getSuperClass(); } } catch (ClassNotFoundException ex) { // Missing classes are reported by findSymbolProblem method. } return builder.build(); }
Example 5
Source File: UnreadFields.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
@Override public void visit(JavaClass obj) { data.calledFromConstructors.clear(); hasNativeMethods = false; sawSelfCallInConstructor = false; publicOrProtectedConstructor = false; isSerializable = false; if (obj.isAbstract()) { data.abstractClasses.add(getDottedClassName()); } else { String superClass = obj.getSuperclassName(); if (superClass != null) { data.hasNonAbstractSubClass.add(superClass); } } data.classesScanned.add(getDottedClassName()); boolean superClassIsObject = Values.DOTTED_JAVA_LANG_OBJECT.equals(obj.getSuperclassName()); if (getSuperclassName().indexOf('$') >= 0 || getSuperclassName().indexOf('+') >= 0 || withinAnonymousClass.matcher(getDottedClassName()).find()) { // System.out.println("hicfsc: " + betterClassName); data.innerClassCannotBeStatic.add(getDottedClassName()); // System.out.println("hicfsc: " + betterSuperclassName); data.innerClassCannotBeStatic.add(getDottedSuperclassName()); } // Does this class directly implement Serializable? String[] interface_names = obj.getInterfaceNames(); for (String interface_name : interface_names) { if ("java.io.Externalizable".equals(interface_name)) { isSerializable = true; } else if ("java.io.Serializable".equals(interface_name)) { isSerializable = true; break; } } // Does this class indirectly implement Serializable? if ((!superClassIsObject || interface_names.length > 0) && !isSerializable) { try { Subtypes2 subtypes2 = AnalysisContext.currentAnalysisContext().getSubtypes2(); ClassDescriptor desc = DescriptorFactory.createClassDescriptor(obj); if (subtypes2.getSubtypes(serializable).contains(desc) || subtypes2.getSubtypes(externalizable).contains(desc) || subtypes2.getSubtypes(remote).contains(desc)) { isSerializable = true; } } catch (ClassNotFoundException e) { bugReporter.reportMissingClass(e); } } // System.out.println(getDottedClassName() + " is serializable: " + // isSerializable); super.visit(obj); }
Example 6
Source File: CloneIdiom.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
@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 7
Source File: FindHEmismatch.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
private void check(int pos) { OpcodeStack.Item item = stack.getStackItem(pos); JavaClass type = null; try { type = item.getJavaClass(); } catch (ClassNotFoundException e) { AnalysisContext.reportMissingClass(e); } if (type == null) { return; } String typeName = type.getClassName(); if (typeName.startsWith("java.lang")) { return; } int priority = NORMAL_PRIORITY; OpcodeStack.Item collection = stack.getStackItem(PreorderVisitor.getNumberArguments(getSigConstantOperand())); String collectionSignature = collection.getSignature(); if (collectionSignature.indexOf("Tree") >= 0 || collectionSignature.indexOf("Sorted") >= 0 || collectionSignature.indexOf("SkipList") >= 0) { return; } if (collectionSignature.indexOf("Hash") >= 0) { priority--; } if (!AnalysisContext.currentAnalysisContext()/* .getSubtypes() */.isApplicationClass(type)) { priority++; } if (type.isAbstract() || type.isInterface()) { priority++; } potentialBugs.put( type.getClassName(), new BugInstance(this, "HE_USE_OF_UNHASHABLE_CLASS", priority).addClassAndMethod(this) .addTypeOfNamedClass(type.getClassName()).describe(TypeAnnotation.UNHASHABLE_ROLE).addCalledMethod(this) .addSourceLine(this)); }
Example 8
Source File: FindHEmismatch.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
@Override public void visit(Signature obj) { if (!isApplicationClass) { return; } String sig = obj.getSignature(); String className = findHashedClassInSignature(sig); if (className == null) { return; } if (className.startsWith("java.lang")) { return; } JavaClass type = null; try { type = Repository.lookupClass(className); } catch (ClassNotFoundException e) { AnalysisContext.reportMissingClass(e); } if (type == null) { return; } int priority = NORMAL_PRIORITY; if (sig.indexOf("Hash") >= 0) { priority--; } if (type.isAbstract() || type.isInterface()) { priority++; } if (!AnalysisContext.currentAnalysisContext()/* .getSubtypes() */.isApplicationClass(type)) { priority++; } BugInstance bug = null; if (visitingField()) { bug = new BugInstance(this, "HE_SIGNATURE_DECLARES_HASHING_OF_UNHASHABLE_CLASS", priority).addClass(this) .addVisitedField(this).addTypeOfNamedClass(className).describe(TypeAnnotation.UNHASHABLE_ROLE); } else if (visitingMethod()) { bug = new BugInstance(this, "HE_SIGNATURE_DECLARES_HASHING_OF_UNHASHABLE_CLASS", priority).addClassAndMethod(this) .addTypeOfNamedClass(className).describe(TypeAnnotation.UNHASHABLE_ROLE); } else { bug = new BugInstance(this, "HE_SIGNATURE_DECLARES_HASHING_OF_UNHASHABLE_CLASS", priority).addClass(this) .addClass(this).addTypeOfNamedClass(className).describe(TypeAnnotation.UNHASHABLE_ROLE); } potentialBugs.put(className, bug); }
Example 9
Source File: DeepSubtypeAnalysis.java From spotbugs with GNU Lesser General Public License v2.1 | 4 votes |
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; }