@Override public boolean choose(XMethod method) { return !method.isStatic(); } };
/** * Constructor. * * @param xmethod * a derived method */ public OverriddenMethodsVisitor(XMethod xmethod) { assert !xmethod.isStatic(); this.xmethod = xmethod; }
@Override public boolean choose(XMethod method) { return method.isStatic(); } };
/** * Set method hash for given method. * * @param method * the method * @param methodHash * the method hash */ public void setMethodHash(XMethod method, byte[] methodHash) { methodHashMap.put(method, new MethodHash(method.getName(), method.getSignature(), method.isStatic(), methodHash)); }
private static String getDetailedSignature(XMethod m2) { return m2.getName() + m2.getSignature() + m2.isStatic(); }
@Override public boolean parameterMustBeNonNull(XMethod m, int param) { if (DEBUG) { System.out.print("Checking " + m + " param " + param + " for @Nonnull..."); } TypeQualifierAnnotation tqa = TypeQualifierApplications.getEffectiveTypeQualifierAnnotation(m, param, nonnullTypeQualifierValue); if (tqa == null && param == 0) { String name = m.getName(); String signature = m.getSignature(); if ("main".equals(name) && "([Ljava/lang/String;)V".equals(signature) && m.isStatic() && m.isPublic()) { return true; } else if (assertsFirstParameterIsNonnull(m)) { return true; } else if ("compareTo".equals(name) && ")Z".equals(signature.substring(signature.indexOf(';') + 1)) && !m.isStatic()) { return true; } } boolean answer = (tqa != null) && tqa.when == When.ALWAYS; if (DEBUG) { System.out.println(answer ? "yes" : "no"); } return answer; }
@Override public boolean parameterMustBeNonNull(XMethod m, int param) { if (param == 0) { if ("equals".equals(m.getName()) && "(Ljava/lang/Object;)Z".equals(m.getSignature()) && !m.isStatic()) { return false; } else if ("main".equals(m.getName()) && "([Ljava/lang/String;)V".equals(m.getSignature()) && m.isStatic() && m.isPublic()) { return true; } else if (TypeQualifierNullnessAnnotationDatabase.assertsFirstParameterIsNonnull(m)) { return true; } else if ("compareTo".equals(m.getName()) && m.getSignature().endsWith(";)Z") && !m.isStatic()) { return true; } } if (!anyAnnotations(NullnessAnnotation.NONNULL)) { return false; } XMethodParameter xmp = new XMethodParameter(m, param); NullnessAnnotation resolvedAnnotation = getResolvedAnnotation(xmp, true); return resolvedAnnotation == NullnessAnnotation.NONNULL; }
public static @CheckForNull XMethod definedIn(JavaClass clazz, XMethod m) { for (Method m2 : clazz.getMethods()) { if (m.getName().equals(m2.getName()) && m.getSignature().equals(m2.getSignature()) && m.isStatic() == m2.isStatic()) { return XFactory.createXMethod(clazz, m2); } } return null; }
private boolean classDefinesMethod(JavaClass c, XMethod m) { for (Method definedMethod : c.getMethods()) { if (definedMethod.getName().equals(m.getName()) && definedMethod.getSignature().equals(m.getSignature()) && definedMethod.isStatic() == m.isStatic()) { return true; } } return false; }
public static boolean confusingMethodNamesWrongCapitalization(XMethod m1, XMethod m2) { if (m1.isStatic() != m2.isStatic()) { return false; } if (m1.getClassName().equals(m2.getClassName())) { return false; } if (m1.getName().equals(m2.getName())) { return false; } if (m1.getName().equalsIgnoreCase(m2.getName()) && removePackageNamesFromSignature(m1.getSignature()).equals(removePackageNamesFromSignature(m2.getSignature()))) { return true; } return false; }
private boolean bad(Item left, Item right) { XMethod m = left.getReturnValueOf(); if (m == null) { return false; } Object value = right.getConstant(); if (!(value instanceof Integer) || ((Integer) value).intValue() == 0) { return false; } if (m.isStatic() || !m.isPublic()) { return false; } if ("compareTo".equals(m.getName()) && "(Ljava/lang/Object;)I".equals(m.getSignature())) { return true; } if ("compare".equals(m.getName()) && "(Ljava/lang/Object;Ljava/lang/Object;)I".equals(m.getSignature())) { return true; } return false; }
/** * Create a MethodAnnotation from an XMethod. * * @param xmethod * the XMethod * @return the MethodAnnotation */ public static MethodAnnotation fromXMethod(XMethod xmethod) { return fromForeignMethod(xmethod.getClassName(), xmethod.getName(), xmethod.getSignature(), xmethod.isStatic()); }
public static boolean confusingMethodNamesWrongPackage(XMethod m1, XMethod m2) { if (m1.isStatic() != m2.isStatic()) { return false; } if (m1.getClassName().equals(m2.getClassName())) { return false; } if (!m1.getName().equals(m2.getName())) { return false; } if (m1.getSignature().equals(m2.getSignature())) { return false; } if (removePackageNamesFromSignature(m1.getSignature()).equals(removePackageNamesFromSignature(m2.getSignature()))) { return true; } return false; }
/** * Constructor. * * @param method * an XMethod specifying a specific method in a specific class * @throws ClassNotFoundException */ public JavaClassAndMethod(XMethod method) throws ClassNotFoundException { this.javaClass = Repository.lookupClass(method.getClassName()); for (Method m : javaClass.getMethods()) { if (m.getName().equals(method.getName()) && m.getSignature().equals(method.getSignature()) && m.isStatic() == method.isStatic()) { this.method = m; return; } } throw new IllegalArgumentException("Can't find " + method); }
@Override public void writeXML(XMLOutput xmlOutput) throws IOException { xmlOutput.startTag(CLASS_HASH_ELEMENT_NAME); xmlOutput.addAttribute("class", className); xmlOutput.addAttribute("value", hashToString(classHash)); xmlOutput.stopTag(false); for (Map.Entry<XMethod, MethodHash> entry : methodHashMap.entrySet()) { xmlOutput.startTag(METHOD_HASH_ELEMENT_NAME); xmlOutput.addAttribute("name", entry.getKey().getName()); xmlOutput.addAttribute("signature", entry.getKey().getSignature()); xmlOutput.addAttribute("isStatic", String.valueOf(entry.getKey().isStatic())); xmlOutput.addAttribute("value", hashToString(entry.getValue().getMethodHash())); xmlOutput.stopTag(true); } xmlOutput.closeTag(CLASS_HASH_ELEMENT_NAME); }
/** * Constructor. Creates an entry which matches the given XMethod. * * @param xmethod * an XMethod * @param action * ActionType (ADD or DEL, depending on whether obligation is * added or deleted) * @param entryType * entry type * @param obligations * Obligation to be added or deleted */ public MatchMethodEntry(XMethod xmethod, ObligationPolicyDatabaseActionType action, ObligationPolicyDatabaseEntryType entryType, Obligation... obligations) { this(new SubtypeTypeMatcher(xmethod.getClassDescriptor()), new ExactStringMatcher(xmethod.getName()), new ExactStringMatcher(xmethod.getSignature()), xmethod.isStatic(), action, entryType, obligations); }
public boolean isCalledDirectlyOrIndirectly(XMethod m) { if (isCalled(m)) { return true; } if (m.isStatic() || m.isPrivate() || Const.CONSTRUCTOR_NAME.equals(m.getName())) { return false; } try { IAnalysisCache analysisCache = Global.getAnalysisCache(); XClass clazz = analysisCache.getClassAnalysis(XClass.class, m.getClassDescriptor()); if (isCalledDirectlyOrIndirectly(clazz.getSuperclassDescriptor(), m)) { return true; } for (ClassDescriptor i : clazz.getInterfaceDescriptorList()) { if (isCalledDirectlyOrIndirectly(i, m)) { return true; } } return false; } catch (edu.umd.cs.findbugs.classfile.MissingClassException e) { // AnalysisContext.reportMissingClass(e.getClassNotFoundException()); return false; } catch (MissingClassException e) { AnalysisContext.reportMissingClass(e.getClassNotFoundException()); return false; } catch (Exception e) { AnalysisContext.logError("Error checking to see if " + m + " is called (" + e.getClass().getCanonicalName() + ")", e); return false; } }
private boolean checkNonSuper(XMethod m, Set<XMethod> others) { if (m.isStatic()) { return false; } if (m.getName().startsWith(Const.CONSTRUCTOR_NAME) || m.getName().startsWith(Const.STATIC_INITIALIZER_NAME)) { return false; } for (XMethod m2 : others) { if (confusingMethodNamesWrongCapitalization(m, m2)) { XMethod mm1; XMethod mm2; if (m.compareTo(m2) < 0) { mm1 = m; mm2 = m2; } else { mm1 = m2; mm2 = m; } bugReporter.reportBug(new BugInstance(this, "NM_CONFUSING", LOW_PRIORITY).addClass(mm1.getClassName()) .addMethod(mm1).addClass(mm2.getClassName()).addMethod(mm2)); return true; } } return false; }
private boolean isCalledDirectlyOrIndirectly(@CheckForNull ClassDescriptor clazzDescriptor, XMethod m) throws CheckedAnalysisException { if (clazzDescriptor == null) { return false; } IAnalysisCache analysisCache = Global.getAnalysisCache(); XClass clazz = analysisCache.getClassAnalysis(XClass.class, clazzDescriptor); XMethod m2 = clazz.findMethod(m.getName(), m.getSignature(), m.isStatic()); if (m2 != null && isCalled(m2)) { return true; } if (isCalledDirectlyOrIndirectly(clazz.getSuperclassDescriptor(), m)) { return true; } for (ClassDescriptor i : clazz.getInterfaceDescriptorList()) { if (isCalledDirectlyOrIndirectly(i, m)) { return true; } } return false; }
private void registerParameterSources() { ValueNumberFrame vnaFrameAtEntry = vnaDataflow.getStartFact(cfg.getEntry()); SignatureParser sigParser = new SignatureParser(xmethod.getSignature()); int firstParamSlot = xmethod.isStatic() ? 0 : 1; int param = 0; int slotOffset = 0; for ( String paramSig : sigParser.parameterSignatures()) { // Get the TypeQualifierAnnotation for this parameter SourceSinkInfo info; TypeQualifierAnnotation tqa = TypeQualifierApplications.getEffectiveTypeQualifierAnnotation(xmethod, param, typeQualifierValue); When when = (tqa != null) ? tqa.when : When.UNKNOWN; ValueNumber vn = vnaFrameAtEntry.getValue(slotOffset + firstParamSlot); info = new SourceSinkInfo(SourceSinkType.PARAMETER, cfg.getLocationAtEntry(), vn, when); info.setParameterAndLocal(param, slotOffset + firstParamSlot); registerSourceSink(info); param++; slotOffset += SignatureParser.getNumSlotsForType(paramSig); } }