Program: 25579
Project: 25579
Buggy source:
/** 
 * Given a set of referenced types, works out which of them should be imported and what the resulting spelling of each one is. <p>This method operates on a   {@code Set<TypeMirror>} rather than just a {@code Set<String>}because it is not strictly possible to determine what part of a fully-qualified type name is the package and what part is the top-level class. For example,   {@code java.util.Map.Entry} isa class called  {@code Map.Entry} in a package called {@code java.util} assuming Javaconventions are being followed, but it could theoretically also be a class called  {@code Entry}in a package called   {@code java.util.Map}. Since we are operating as part of the compiler, our goal should be complete correctness, and the only way to achieve that is to operate on the real representations of types.
 * @param packageName The name of the package where the class containing these references isdefined. Other classes within the same package do not need to be imported.
 * @param referenced The complete set of declared types (classes and interfaces) that will bereferenced in the generated code.
 * @return a map where the keys are fully-qualified types and the corresponding values indicatewhether the type should be imported, and how the type should be spelled in the source code.
 */
private static Map<String,Spelling> findImports(Types typeUtil,String packageName,Set<TypeMirror> referenced){
  Map<String,Spelling> imports=new HashMap<String,Spelling>();
  Set<String> ambiguous=ambiguousNames(typeUtil,referenced);
  for (  TypeMirror type : referenced) {
    TypeElement typeElement=(TypeElement)typeUtil.asElement(type);
    String fullName=typeElement.getQualifiedName().toString();
    String simpleName=typeElement.getSimpleName().toString();
    String pkg=packageNameOf(typeElement);
    boolean importIt;
    String spelling;
    if (ambiguous.contains(simpleName)) {
      importIt=false;
      spelling=fullName;
    }
 else     if (pkg.equals(packageName) || pkg.equals("String_Node_Str")) {
      importIt=false;
      spelling=fullName.substring(pkg.isEmpty() ? 0 : pkg.length() + 1);
    }
 else {
      importIt=true;
      spelling=simpleName;
    }
    imports.put(fullName,new Spelling(spelling,importIt));
  }
  return imports;
}

Fixed version (ground truth):
/** 
 * Given a set of referenced types, works out which of them should be imported and what the resulting spelling of each one is. <p>This method operates on a   {@code Set<TypeMirror>} rather than just a {@code Set<String>}because it is not strictly possible to determine what part of a fully-qualified type name is the package and what part is the top-level class. For example,   {@code java.util.Map.Entry} isa class called  {@code Map.Entry} in a package called {@code java.util} assuming Javaconventions are being followed, but it could theoretically also be a class called  {@code Entry}in a package called   {@code java.util.Map}. Since we are operating as part of the compiler, our goal should be complete correctness, and the only way to achieve that is to operate on the real representations of types.
 * @param packageName The name of the package where the class containing these references isdefined. Other classes within the same package do not need to be imported.
 * @param referenced The complete set of declared types (classes and interfaces) that will bereferenced in the generated code.
 * @param defined The complete set of declared types (classes and interfaces) that are definedwithin the scope of the generated class (i.e. nested somewhere in its superclass chain, or in its interface set)
 * @return a map where the keys are fully-qualified types and the corresponding values indicatewhether the type should be imported, and how the type should be spelled in the source code.
 */
private static Map<String,Spelling> findImports(Types typeUtil,String packageName,Set<TypeMirror> referenced,Set<TypeMirror> defined){
  Map<String,Spelling> imports=new HashMap<String,Spelling>();
  Set<TypeMirror> typesInScope=new HashSet<TypeMirror>();
  typesInScope.addAll(referenced);
  typesInScope.addAll(defined);
  Set<String> ambiguous=ambiguousNames(typeUtil,typesInScope);
  for (  TypeMirror type : referenced) {
    TypeElement typeElement=(TypeElement)typeUtil.asElement(type);
    String fullName=typeElement.getQualifiedName().toString();
    String simpleName=typeElement.getSimpleName().toString();
    String pkg=packageNameOf(typeElement);
    boolean importIt;
    String spelling;
    if (ambiguous.contains(simpleName)) {
      importIt=false;
      spelling=fullName;
    }
 else     if (pkg.equals(packageName) || pkg.equals("String_Node_Str")) {
      importIt=false;
      spelling=fullName.substring(pkg.isEmpty() ? 0 : pkg.length() + 1);
    }
 else {
      importIt=true;
      spelling=simpleName;
    }
    imports.put(fullName,new Spelling(spelling,importIt));
  }
  return imports;
}