final NameExpr nameToFind, final Set<JavaSymbolName> typeParameters) {
Validate.notNull(compilationUnitServices,
"Compilation unit services required");
Validate.notNull(nameToFind, "Name to find is required");
final JavaPackage compilationUnitPackage = compilationUnitServices
.getCompilationUnitPackage();
if (nameToFind instanceof QualifiedNameExpr) {
final QualifiedNameExpr qne = (QualifiedNameExpr) nameToFind;
// Handle qualified name expressions that are related to inner types
// (eg Foo.Bar)
final NameExpr qneQualifier = qne.getQualifier();
final NameExpr enclosedBy = getNameExpr(compilationUnitServices
.getEnclosingTypeName().getSimpleTypeName());
if (isEqual(qneQualifier, enclosedBy)) {
// This qualified name expression is simply an inner type
// reference
final String name = compilationUnitServices
.getEnclosingTypeName().getFullyQualifiedTypeName()
+ "." + nameToFind.getName();
return new JavaType(name,
compilationUnitServices.getEnclosingTypeName());
}
// Refers to a different enclosing type, so calculate the package
// name based on convention of an uppercase letter denotes same
// package (ROO-1210)
if (qne.toString().length() > 1
&& Character.isUpperCase(qne.toString().charAt(0))) {
// First letter is uppercase, so this likely requires prepending
// of some package name
final ImportDeclaration importDeclaration = getImportDeclarationFor(
compilationUnitServices, qne.getQualifier());
if (importDeclaration == null) {
if (!compilationUnitPackage.getFullyQualifiedPackageName()
.equals("")) {
// It was not imported, so let's assume it's in the same
// package
return new JavaType(compilationUnitServices
.getCompilationUnitPackage()
.getFullyQualifiedPackageName()
+ "." + qne.toString());
}
}
else {
return new JavaType(importDeclaration.getName() + "."
+ qne.getName());
}
// This name expression (which contains a dot) had its qualifier
// imported, so let's use the import
}
else {
// First letter is lowercase, so the reference already includes
// a package
return new JavaType(qne.toString());
}
}
if ("?".equals(nameToFind.getName())) {
return new JavaType(OBJECT.getFullyQualifiedTypeName(), 0,
DataType.TYPE, JavaType.WILDCARD_NEITHER, null);
}
// Unqualified name detected, so check if it's in the type parameter
// list
if (typeParameters != null
&& typeParameters.contains(new JavaSymbolName(nameToFind
.getName()))) {
return new JavaType(nameToFind.getName(), 0, DataType.VARIABLE,
null, null);
}
// Check if we are looking for the enclosingType itself
final NameExpr enclosingTypeName = getNameExpr(compilationUnitServices
.getEnclosingTypeName().getSimpleTypeName());
if (isEqual(enclosingTypeName, nameToFind)) {
return compilationUnitServices.getEnclosingTypeName();
}
// We are searching for a non-qualified name expression (nameToFind), so
// check if the compilation unit itself declares that type
for (final TypeDeclaration internalType : compilationUnitServices
.getInnerTypes()) {
final NameExpr nameExpr = getNameExpr(internalType.getName());
if (isEqual(nameExpr, nameToFind)) {
// Found, so now we need to convert the internalType to a proper
// JavaType
final String name = compilationUnitServices
.getEnclosingTypeName().getFullyQualifiedTypeName()
+ "." + nameToFind.getName();
return new JavaType(name);
}
}
final ImportDeclaration importDeclaration = getImportDeclarationFor(
compilationUnitServices, nameToFind);
if (importDeclaration == null) {
if (JdkJavaType.isPartOfJavaLang(nameToFind.getName())) {
return new JavaType("java.lang." + nameToFind.getName());
}
final String name = compilationUnitPackage
.getFullyQualifiedPackageName().equals("") ? nameToFind
.getName() : compilationUnitPackage
.getFullyQualifiedPackageName()
+ "."
+ nameToFind.getName();
return new JavaType(name);
}