コード例 #1
0
 public static @CheckForNull LocalVariableAnnotation findUniqueBestMatchingParameter(
     ClassContext classContext, Method method, String name, String signature) {
   LocalVariableAnnotation match = null;
   int localsThatAreParameters = PreorderVisitor.getNumberArguments(method.getSignature());
   int startIndex = 0;
   if (!method.isStatic()) startIndex = 1;
   SignatureParser parser = new SignatureParser(method.getSignature());
   Iterator<String> signatureIterator = parser.parameterSignatureIterator();
   int lowestCost = Integer.MAX_VALUE;
   for (int i = startIndex; i < localsThatAreParameters + startIndex; i++) {
     String sig = signatureIterator.next();
     if (signature.equals(sig)) {
       LocalVariableAnnotation potentialMatch =
           LocalVariableAnnotation.getLocalVariableAnnotation(method, i, 0, 0);
       if (!potentialMatch.isNamed()) continue;
       int distance = EditDistance.editDistance(name, potentialMatch.getName());
       if (distance < lowestCost) {
         match = potentialMatch;
         match.setDescription(DID_YOU_MEAN_ROLE);
         lowestCost = distance;
       } else if (distance == lowestCost) {
         // not unique best match
         match = null;
       }
       // signatures match
     }
   }
   if (lowestCost < 5) return match;
   return null;
 }
コード例 #2
0
  public static @CheckForNull LocalVariableAnnotation findMatchingIgnoredParameter(
      ClassContext classContext, Method method, String name, String signature) {
    try {
      Dataflow<BitSet, LiveLocalStoreAnalysis> llsaDataflow =
          classContext.getLiveLocalStoreDataflow(method);
      CFG cfg;

      cfg = classContext.getCFG(method);
      LocalVariableAnnotation match = null;
      int lowestCost = Integer.MAX_VALUE;
      BitSet liveStoreSetAtEntry = llsaDataflow.getAnalysis().getResultFact(cfg.getEntry());
      int localsThatAreParameters = PreorderVisitor.getNumberArguments(method.getSignature());
      int startIndex = 0;
      if (!method.isStatic()) startIndex = 1;
      SignatureParser parser = new SignatureParser(method.getSignature());
      Iterator<String> signatureIterator = parser.parameterSignatureIterator();
      for (int i = startIndex; i < localsThatAreParameters + startIndex; i++) {
        String sig = signatureIterator.next();
        if (!liveStoreSetAtEntry.get(i) && signature.equals(sig)) {
          // parameter isn't live and signatures match
          LocalVariableAnnotation potentialMatch =
              LocalVariableAnnotation.getLocalVariableAnnotation(method, i, 0, 0);
          potentialMatch.setDescription(DID_YOU_MEAN_ROLE);
          if (!potentialMatch.isNamed()) return potentialMatch;
          int distance = EditDistance.editDistance(name, potentialMatch.getName());
          if (distance < lowestCost) {
            match = potentialMatch;
            match.setDescription(DID_YOU_MEAN_ROLE);
            lowestCost = distance;
          } else if (distance == lowestCost) {
            // not unique best match
            match = null;
          }
        }
      }
      return match;
    } catch (DataflowAnalysisException e) {
      AnalysisContext.logError("", e);
    } catch (CFGBuilderException e) {
      AnalysisContext.logError("", e);
    }
    return null;
  }