Ejemplo n.º 1
0
  @SuppressWarnings("rawtypes")
  private static HashMap<String, ArrayList<FixTypes>> findFixes(ArrayList<SourceKey> files)
      throws ClassNotFoundException {
    HashMap<String, ArrayList<FixTypes>> ret = new HashMap<String, ArrayList<FixTypes>>();
    HashSet<String> added = new HashSet<String>();
    for (SourceKey src : files) {
      log("    " + src.name);
      ArrayList<IProblem> errors = new ArrayList<IProblem>();
      HashMap<String, ArrayList<IProblem>> duplicates = new HashMap<String, ArrayList<IProblem>>();

      for (IProblem p : src.cu.getProblems()) {
        if (p.isError()) {
          int id = (p.getID() & IProblem.IgnoreCategoriesMask);
          if (id == 169) continue; // Screw you switch errors that arnt errrors

          if (id == 355) // Duplicate methods
          {
            String name = p.getArguments()[0];
            if (!duplicates.containsKey(name)) duplicates.put(name, new ArrayList<IProblem>());
            duplicates.get(name).add(p);
          } else if (id == 101) // Non-visible method
          {
            String name = p.getArguments()[1];
            String args = p.getArguments()[2];
            String clsName = p.getArguments()[0];

            int start = p.getSourceStart();
            int length = p.getSourceEnd() - p.getSourceStart() + 1;
            String newName = name + "_CodeFix_Public";
            if (name.endsWith("_")) newName = name + "CodeFix_Public";

            if (!gatherMethod(ret, getClass(clsName, files), name, args, newName)) {
              log("      Could not find class: " + clsName);
              log("      " + p.toString());
              if (FATAL) System.exit(1);
              else continue;
            } else {
              String key = "PUBLIC_" + clsName.replace('.', '/') + "/" + name + "(" + args + ")";
              if (!added.contains(key)) {
                if (!ret.containsKey(src.name)) ret.put(src.name, new ArrayList<FixTypes>());
                ret.get(src.name).add(new FixTypes.PublicMethod(start, length, newName));
                added.add(key);
              }
            }
          } else if (id == 71) // Non-visible field
          {
            String find = p.getArguments()[0];
            TypeDeclaration cls = getClass(p.getArguments()[1], files);
            if (cls == null) {
              log("      Could not find class for field " + p.toString());
              if (FATAL) System.exit(1);
              else continue;
            }
            boolean exit = false;
            for (FieldDeclaration field : cls.getFields()) {
              for (VariableDeclarationFragment frag :
                  (List<VariableDeclarationFragment>) field.fragments()) {
                String name = frag.resolveBinding().getName();
                if (find.equals(name)) {
                  String clsName = cls.resolveBinding().getQualifiedName().replace('.', '/');
                  String key = "PUBLIC_" + clsName.replace('.', '/') + "/" + name;
                  if (!added.contains(key)) {
                    if (!ret.containsKey(clsName)) ret.put(clsName, new ArrayList<FixTypes>());
                    ret.get(clsName).add(new FixTypes.PublicField(field));
                    added.add(key);
                  }
                  exit = true;
                  break;
                }
              }
              if (exit) break;
            }
          } else if (id == 400) // Missing Method
          {
            String name = p.getArguments()[0];
            String tmp = p.getArguments()[1];
            String owner = p.getArguments()[2];
            String impl = p.getArguments()[3];
            String[] args = (tmp.length() == 0 ? new String[0] : tmp.split(", "));
            Class cls = Class.forName(owner, false, CodeFixer.class.getClassLoader());
            String signature = null;
            Class<?> returnType = null;

            for (Method m : cls.getMethods()) {
              if (m.getName().equals(name)) {
                Class[] types = m.getParameterTypes();
                if (types.length == args.length) {
                  boolean same = true;
                  for (int x = 0; x < args.length; x++) {
                    if (!args[x].equals(types[x].getName().toString())) {
                      same = false;
                      break;
                    }
                  }
                  if (same) {
                    signature = MethodSignatureHelper.getSignature(m);
                    returnType = m.getReturnType();
                  }
                }
              }
              if (signature != null) break;
            }

            SrgFile.Class icls = SRG.getClass2(impl.replace('.', '/'));
            if (icls == null) {
              log("      Could not find class in SRG " + impl);
              if (FATAL) System.exit(1);
              else continue;
            }

            SrgFile.Node mtd = icls.methods1.get(name + signature);

            ClassTree.Class treeNode = TREE.getClass(impl);
            treeNode = treeNode.getParent();
            if (treeNode == null) {
              log(
                  "    Could not find missing method, and parent was null: "
                      + impl
                      + "."
                      + name
                      + signature);
              if (FATAL) System.exit(1);
              else continue;
            }

            while (mtd == null && treeNode != null) {
              icls = SRG.getClass2(treeNode.name);
              treeNode = treeNode.getParent();
              if (icls != null) {
                mtd = icls.methods1.get(name + signature);
              }
            }

            String rename = null;

            String key = "BOUNCE_" + impl.replace('.', '/') + '/' + name + signature;
            if (mtd == null) {
              if (!FIXES.containsKey(key)) {
                log("      Could not find bounce rename " + key);
                if (FATAL) System.exit(1);
                else continue;
              } else {
                rename = FIXES.getProperty(key, null);
                if (name != null) {
                  log("      Loaded bounce rename " + key + " -> " + rename);
                }
              }
            } else {
              rename = mtd.rename;
            }

            if (rename != null && !added.contains(key)) {
              String clsName = impl.replace('.', '/');
              if (!ret.containsKey(clsName)) ret.put(clsName, new ArrayList<FixTypes>());
              ret.get(clsName)
                  .add(
                      new FixTypes.BounceMethod(
                          getClass(impl, files), name, rename, args, returnType));
              added.add(key);
            }
          } else if (id == 17) // Casting issue
          {
            String clsName = new String(p.getOriginatingFileName()).replace(".java", "");
            if (!ret.containsKey(clsName)) ret.put(clsName, new ArrayList<FixTypes>());
            ret.get(clsName)
                .add(new FixTypes.Cast(p.getSourceStart(), 0, "(" + p.getArguments()[1] + ")"));
          } else {
            errors.add(p);
          }
        }
      }

      gatherDuplicateFixes(duplicates, ret, src);

      if (errors.size() > 0) {
        log("      " + src.name);
        for (IProblem p : errors) {
          log("        " + p);
          // for (String s : p.getArguments()) System.out.println("        " + s);
        }
      }
    }
    return ret;
  }
Ejemplo n.º 2
0
 public boolean visit(VariableDeclarationFragment node) {
   if (found(node, node.getName()) && this.resolveBinding)
     this.foundBinding = node.resolveBinding();
   return true;
 }