Пример #1
0
  @Override
  public String resolveType(final String type) {
    String original = type;
    String result = type;

    // Strip away any characters that might hinder the type matching process
    if (Types.isArray(result)) {
      original = Types.stripArray(result);
      result = Types.stripArray(result);
    }

    if (Types.isGeneric(result)) {
      original = Types.stripGenerics(result);
      result = Types.stripGenerics(result);
    }

    if (Types.isPrimitive(result)) {
      return result;
    }

    // Check for direct import matches first since they are the fastest and least work-intensive
    if (Types.isSimpleName(result)) {
      if (!hasImport(type) && Types.isJavaLang(type)) {
        result = "java.lang." + result;
      }

      if (result.equals(original)) {
        for (Import imprt : getImports()) {
          if (Types.areEquivalent(result, imprt.getQualifiedName())) {
            result = imprt.getQualifiedName();
            break;
          }
        }
      }
    }

    // If we didn't match any imports directly, we might have a wild-card/on-demand import.
    if (Types.isSimpleName(result)) {
      for (Import imprt : getImports()) {
        if (imprt.isWildcard()) {
          // TODO warn if no wild-card resolvers are configured
          // TODO Test wild-card/on-demand import resolving
          for (WildcardImportResolver r : getImportResolvers()) {
            result = r.resolve(this, result);
            if (Types.isQualified(result)) break;
          }
        }
      }
    }

    // No import matches and no wild-card/on-demand import matches means this class is in the same
    // package.
    if (Types.isSimpleName(result)) {
      if (getPackage() != null) result = getPackage() + "." + result;
    }

    return result;
  }
Пример #2
0
 @Command("list-imports")
 @RequiresResource(JavaResource.class)
 public void listImports(final PipeOut out) throws FileNotFoundException {
   List<Import> imports = resource.getJavaSource().getImports();
   for (Import i : imports) {
     String str = "import " + (i.isStatic() ? "static " : "") + i.getQualifiedName() + ";";
     str = JavaColorizer.format(out, str);
     out.println(str);
   }
 }
Пример #3
0
 @Override
 public O removeImport(final String name) {
   for (Import i : getImports()) {
     if (i.getQualifiedName().equals(name)) {
       removeImport(i);
       break;
     }
   }
   return (O) this;
 }
Пример #4
0
 @Override
 public Import getImport(final String className) {
   List<Import> imports = getImports();
   for (Import imprt : imports) {
     if (imprt.getQualifiedName().equals(className) || imprt.getSimpleName().equals(className)) {
       return imprt;
     }
   }
   return null;
 }
Пример #5
0
 @Override
 public List<String> getInterfaces() {
   List<String> result = new ArrayList<String>();
   List<Type> superTypes = JDTHelper.getInterfaces(getBodyDeclaration());
   for (Type type : superTypes) {
     String name = JDTHelper.getTypeName(type);
     if (Types.isSimpleName(name) && this.hasImport(name)) {
       Import imprt = this.getImport(name);
       String pkg = imprt.getPackage();
       if (!Strings.isNullOrEmpty(pkg)) {
         name = pkg + "." + name;
       }
     }
     result.add(name);
   }
   return result;
 }
Пример #6
0
 @Override
 public O removeImport(final Import imprt) {
   Object internal = imprt.getInternal();
   if (unit.imports().contains(internal)) {
     unit.imports().remove(internal);
   }
   return (O) this;
 }
Пример #7
0
  @Override
  public Import addImport(final String className) {
    String strippedClassName = Types.stripGenerics(Types.stripArray(className));
    Import imprt;
    if (Types.isSimpleName(strippedClassName) && !hasImport(strippedClassName)) {
      throw new IllegalArgumentException(
          "Cannot import class without a package [" + strippedClassName + "]");
    }

    if (!hasImport(strippedClassName) && validImport(strippedClassName)) {
      imprt = new ImportImpl(this).setName(strippedClassName);
      unit.imports().add(imprt.getInternal());
    } else if (hasImport(strippedClassName)) {
      imprt = getImport(strippedClassName);
    } else {
      throw new IllegalArgumentException(
          "Attempted to import the illegal type [" + strippedClassName + "]");
    }
    return imprt;
  }
Пример #8
0
 @Override
 public boolean hasImport(final Import imprt) {
   return hasImport(imprt.getQualifiedName());
 }
Пример #9
0
 @Override
 public Import getImport(final Import imprt) {
   return getImport(imprt.getQualifiedName());
 }
Пример #10
0
 @Override
 public Import addImport(final Import imprt) {
   return addImport(imprt.getQualifiedName()).setStatic(imprt.isStatic());
 }