use of com.github.antlrjavaparser.api.expr.QualifiedNameExpr in project spring-roo by spring-projects.
the class JavaParserUtils method importTypeIfRequired.
/**
* Attempts to import the presented {@link JavaType}.
* <p>
* Whether imported or not, the method returns a {@link NameExpr} suitable
* for subsequent use when referring to that type.
* <p>
* If an attempt is made to import a java.lang type, it is ignored.
* <p>
* If an attempt is made to import a type without a package, it is ignored.
* <p>
* We import every type usage even if the type usage is within the same
* package and would theoretically not require an import. This is undertaken
* so that there is no requirement to separately parse every unqualified
* type usage within the compilation unit so as to refrain from importing
* subsequently conflicting types.
*
* @param targetType the compilation unit target type (required)
* @param imports the compilation unit's imports (required)
* @param typeToImport the type to be imported (required)
* @return the name expression to be used when referring to that type (never
* null)
*/
public static NameExpr importTypeIfRequired(final JavaType targetType, final List<ImportDeclaration> imports, final JavaType typeToImport) {
Validate.notNull(targetType, "Target type is required");
final JavaPackage compilationUnitPackage = targetType.getPackage();
Validate.notNull(imports, "Compilation unit imports required");
Validate.notNull(typeToImport, "Java type to import is required");
// If it's a primitive, it's really easy
if (typeToImport.isPrimitive()) {
return new NameExpr(typeToImport.getNameIncludingTypeParameters());
}
// Handle if the type doesn't have a package at all
if (typeToImport.isDefaultPackage()) {
return new NameExpr(typeToImport.getSimpleTypeName());
}
final JavaPackage typeToImportPackage = typeToImport.getPackage();
if (typeToImportPackage.equals(compilationUnitPackage)) {
return new NameExpr(typeToImport.getSimpleTypeName());
}
NameExpr typeToImportExpr;
if (typeToImport.getEnclosingType() == null) {
typeToImportExpr = new QualifiedNameExpr(new NameExpr(typeToImport.getPackage().getFullyQualifiedPackageName()), typeToImport.getSimpleTypeName());
} else {
typeToImportExpr = new QualifiedNameExpr(new NameExpr(typeToImport.getEnclosingType().getFullyQualifiedTypeName()), typeToImport.getSimpleTypeName());
}
final ImportDeclaration newImport = new ImportDeclaration(typeToImportExpr, false, false);
boolean addImport = true;
boolean useSimpleTypeName = false;
for (final ImportDeclaration existingImport : imports) {
if (existingImport.getName().getName().equals(newImport.getName().getName())) {
// Do not import, as there is already an import with the simple
// type name
addImport = false;
// simple type name
if (isEqual(existingImport.getName(), newImport.getName())) {
useSimpleTypeName = true;
break;
}
}
}
if (addImport && JdkJavaType.isPartOfJavaLang(typeToImport.getSimpleTypeName())) {
// This simple type name would be part of java.lang if left as the
// simple name. We want a fully-qualified name.
addImport = false;
useSimpleTypeName = false;
}
if (JdkJavaType.isPartOfJavaLang(typeToImport)) {
// So we would have imported, but we don't need to
addImport = false;
// The fact we could have imported means there was no other
// conflicting simple type names
useSimpleTypeName = true;
}
if (addImport && typeToImport.getPackage().equals(compilationUnitPackage)) {
// It is not theoretically necessary to add an import for something
// in the same package,
// but we elect to explicitly perform an import so future
// conflicting types are not imported
// addImport = true;
// useSimpleTypeName = false;
}
if (addImport && targetType.getSimpleTypeName().equals(typeToImport.getSimpleTypeName())) {
// So we would have imported it, but then it would conflict with the
// simple name of the type
addImport = false;
useSimpleTypeName = false;
}
if (addImport) {
imports.add(newImport);
useSimpleTypeName = true;
}
// (forget imports, though!)
if (typeToImport.getArgName() != null) {
return new NameExpr(typeToImport.toString());
}
if (useSimpleTypeName) {
return new NameExpr(typeToImport.getSimpleTypeName());
}
return new QualifiedNameExpr(new NameExpr(typeToImport.getPackage().getFullyQualifiedPackageName()), typeToImport.getSimpleTypeName());
}
use of com.github.antlrjavaparser.api.expr.QualifiedNameExpr in project spring-roo by spring-projects.
the class JavaParserUtils method getNameExpr.
/**
* Converts the presented class name into a name expression (either a
* {@link NameExpr} or {@link QualifiedNameExpr} depending on whether a
* package was presented).
*
* @param className to convert (required; can be fully qualified or simple
* name only)
* @return a compatible expression (never returns null)
*/
public static NameExpr getNameExpr(final String className) {
Validate.notBlank(className, "Class name required");
if (className.contains(".")) {
final int offset = className.lastIndexOf(".");
final String packageName = className.substring(0, offset);
final String typeName = className.substring(offset + 1);
return new QualifiedNameExpr(new NameExpr(packageName), typeName);
}
return new NameExpr(className);
}
Aggregations