/** * Returns the dimension of the array. * * It simply counts the "[" from the string. * * @param name an array type, e.g.: byte[] or [Ljava.lang.Boolean; * @return the array dimension. 0 if the type is not a valid array */ public static int getArrayDimension(String name) { int count = 0; if (name != null) { if (isGeneric(name)) { name = stripGenerics(name); } for (char c : name.toCharArray()) { if (c == '[') { count++; } } } return count; }
public static boolean areEquivalent(String left, String right) { if ((left == null) && (right == null)) return true; if ((left == null) || (right == null)) return false; if (left.equals(right)) return true; left = stripGenerics(left); right = stripGenerics(right); String l = toSimpleName(left); String r = toSimpleName(right); String lp = getPackage(left); String rp = getPackage(right); if (l.equals(r)) { return !(!lp.isEmpty() && !rp.isEmpty()); } return false; }
public static boolean areEquivalent(String left, String right) { if ((left == null) && (right == null)) return true; if ((left == null) || (right == null)) return false; if (left.equals(right)) return true; left = stripGenerics(left); right = stripGenerics(right); String l = toSimpleName(left); String r = toSimpleName(right); String lp = getPackage(left); String rp = getPackage(right); if (l.equals(r)) { return !(!lp.isEmpty() && !rp.isEmpty()); } return false; }
public static boolean areEquivalent(String left, String right) { if ((left == null) && (right == null)) return true; if ((left == null) || (right == null)) return false; if (left.equals(right)) return true; left = stripGenerics(left); right = stripGenerics(right); String l = toSimpleName(left); String r = toSimpleName(right); String lp = getPackage(left); String rp = getPackage(right); if (l.equals(r)) { if (!lp.isEmpty() && !rp.isEmpty()) { return false; } return true; } return false; }
@Override public boolean hasImport(final String type) { String resultType = type; if (Types.isArray(type)) { resultType = Types.stripArray(type); } if (Types.isGeneric(type)) { resultType = Types.stripGenerics(type); } return getImport(resultType) != null; }
@Override public boolean hasImport(final String type) { String resultType = type; if (Types.isArray(type)) { resultType = Types.stripArray(type); } if (Types.isGeneric(type)) { resultType = Types.stripGenerics(type); } return getImport(resultType) != null; }
@Override public boolean hasImport(final String type) { String resultType = type; if (Types.isArray(type)) { resultType = Types.stripArray(type); } if (Types.isGeneric(type)) { resultType = Types.stripGenerics(type); } return getImport(resultType) != null; }
@Override public boolean hasImport(final String type) { String resultType = type; if (Types.isArray(type)) { resultType = Types.stripArray(type); } if (Types.isGeneric(type)) { resultType = Types.stripGenerics(type); } return getImport(resultType) != null; }
@Override public boolean hasImport(final String type) { String resultType = type; if (Types.isArray(type)) { resultType = Types.stripArray(type); } if (Types.isGeneric(type)) { resultType = Types.stripGenerics(type); } return getImport(resultType) != null; }
@Override public boolean requiresImport(final String type) { String resultType = type; if (Types.isArray(resultType)) { resultType = Types.stripArray(type); } if (Types.isGeneric(resultType)) { resultType = Types.stripGenerics(resultType); } return !(!validImport(resultType) || hasImport(resultType) || Types.isJavaLang(resultType)); }
@Override public List<String> getInterfaces() { List<String> result = new ArrayList<>(); List<Type> superTypes = JDTHelper.getInterfaces(getDeclaration()); for (Type type : superTypes) { String name = JDTHelper.getTypeName(type); String rawName = Types.stripGenerics(name); if (Types.isSimpleName(rawName)) { String pkg; if (this.hasImport(rawName)) { Import imprt = this.getImport(rawName); pkg = imprt.getPackage(); } else { pkg = this.getPackage(); } if (!Strings.isNullOrEmpty(pkg)) { name = pkg + "." + name; } } result.add(name); } return result; }
@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; }
@Override public String getName() { String result = type.toString(); if (isParameterized()) { // strip type parameters after stripping array dimensions if (isArray()) { result = Types.stripArray(result); } result = Types.stripGenerics(result); // restore array dimensions for (int i = 0, dim = getArrayDimensions(); i < dim; i++) { result += "[]"; } return result; } for (int i = 0, dim = getExtraDimensions(); i < dim; i++) { result += "[]"; } return result; }
@Override public boolean requiresImport(final String type) { String resultType = type; if (Types.isArray(resultType)) { resultType = Types.stripArray(type); } if (Types.isGeneric(resultType)) { resultType = Types.stripGenerics(resultType); } if (!validImport(resultType) || hasImport(resultType) || Types.isJavaLang(resultType)) { return false; } return true; }
@Override public String getName() { String result = type.toString(); if (isParameterized()) { // strip type parameters after stripping array dimensions if (isArray()) { result = Types.stripArray(result); } result = Types.stripGenerics(result); // restore array dimensions for (int i = 0, dim = getArrayDimensions(); i < dim; i++) { result += "[]"; } return result; } for (int i = 0, dim = getExtraDimensions(); i < dim; i++) { result += "[]"; } return result; }
@Override public String getName() { String result = type.toString(); if (isParameterized()) { // strip type parameters after stripping array dimensions if (isArray()) { result = Types.stripArray(result); } result = Types.stripGenerics(result); // restore array dimensions for (int i = 0, dim = getArrayDimensions(); i < dim; i++) { result += "[]"; } return result; } for (int i = 0, dim = getExtraDimensions(); i < dim; i++) { result += "[]"; } return result; }
@Override public boolean requiresImport(final String type) { String resultType = type; if (Types.isArray(resultType)) { resultType = Types.stripArray(type); } if (Types.isGeneric(resultType)) { resultType = Types.stripGenerics(resultType); } if (!validImport(resultType) || hasImport(resultType) || Types.isJavaLang(resultType) || Strings.areEqual(getPackage(), Types.getPackage(resultType))) { return false; } return true; }
@SuppressWarnings("unchecked") @Override public Import addImport(final String className) { String strippedClassName = Types.stripGenerics(Types.stripArray(className)); if (Types.isGeneric(className)) { for (String genericPart : Types.splitGenerics(className)) { if (Types.isQualified(genericPart)) addImport(genericPart); } } Import imprt; if (!hasImport(strippedClassName) && validImport(strippedClassName)) { imprt = new ImportImpl(this).setName(strippedClassName); unit.imports().add(imprt.getInternal()); } else if (hasImport(strippedClassName)) { imprt = getImport(strippedClassName); } else { imprt = null; } return imprt; }
@Override public boolean requiresImport(final String type) { boolean requiresImport = false; String resultType = type; if (Types.isArray(resultType)) { resultType = Types.stripArray(resultType); } if (Types.isGeneric(resultType)) { for (String genericPart : Types.splitGenerics(resultType)) { requiresImport |= requiresImport(genericPart); } resultType = Types.stripGenerics(resultType); } requiresImport |= !(!validImport(resultType) || hasImport(resultType) || Types.isJavaLang(resultType) || Strings.areEqual(getPackage(), Types.getPackage(resultType))); return requiresImport; }
@Override public boolean requiresImport(final String type) { boolean requiresImport = false; String resultType = type; if (Types.isArray(resultType)) { resultType = Types.stripArray(resultType); } if (Types.isGeneric(resultType)) { for (String genericPart : Types.splitGenerics(resultType)) { requiresImport |= requiresImport(genericPart); } resultType = Types.stripGenerics(resultType); } requiresImport |= !(!validImport(resultType) || hasImport(resultType) || Types.isJavaLang(resultType) || Strings.areEqual(getPackage(), Types.getPackage(resultType))); return requiresImport; }