Search in sources :

Example 1 with HasAccessSpecifier

use of com.github.javaparser.resolution.declarations.HasAccessSpecifier in project javaparser by javaparser.

the class JavaParserTypeDeclarationAdapter method checkAncestorsForType.

/**
 * Recursively checks the ancestors of the {@param declaration} if an internal type is declared with a name equal
 * to {@param name}.
 * TODO: Edit to remove return of null (favouring a return of optional)
 * @return A ResolvedTypeDeclaration matching the {@param name}, null otherwise
 */
private ResolvedTypeDeclaration checkAncestorsForType(String name, ResolvedReferenceTypeDeclaration declaration) {
    for (ResolvedReferenceType ancestor : declaration.getAncestors(true)) {
        try {
            // TODO: Figure out if it is appropriate to remove the orElseThrow() -- if so, how...
            ResolvedReferenceTypeDeclaration ancestorReferenceTypeDeclaration = ancestor.getTypeDeclaration().orElseThrow(() -> new RuntimeException("TypeDeclaration unexpectedly empty."));
            for (ResolvedTypeDeclaration internalTypeDeclaration : ancestorReferenceTypeDeclaration.internalTypes()) {
                boolean visible = true;
                if (internalTypeDeclaration instanceof ResolvedReferenceTypeDeclaration) {
                    ResolvedReferenceTypeDeclaration resolvedReferenceTypeDeclaration = internalTypeDeclaration.asReferenceType();
                    if (resolvedReferenceTypeDeclaration instanceof HasAccessSpecifier) {
                        visible = ((HasAccessSpecifier) resolvedReferenceTypeDeclaration).accessSpecifier() != AccessSpecifier.PRIVATE;
                    }
                }
                if (internalTypeDeclaration.getName().equals(name)) {
                    if (visible) {
                        return internalTypeDeclaration;
                    } else {
                        // FIXME -- Avoid returning null.
                        return null;
                    }
                }
            }
            // check recursively the ancestors of this ancestor
            ResolvedTypeDeclaration ancestorTypeDeclaration = checkAncestorsForType(name, ancestorReferenceTypeDeclaration);
            if (ancestorTypeDeclaration != null) {
                return ancestorTypeDeclaration;
            }
        } catch (UnsupportedOperationException e) {
        // just continue using the next ancestor
        }
    }
    // FIXME -- Avoid returning null.
    return null;
}
Also used : ResolvedReferenceTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration) HasAccessSpecifier(com.github.javaparser.resolution.declarations.HasAccessSpecifier) ResolvedReferenceType(com.github.javaparser.resolution.types.ResolvedReferenceType) ResolvedTypeDeclaration(com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)

Aggregations

HasAccessSpecifier (com.github.javaparser.resolution.declarations.HasAccessSpecifier)1 ResolvedReferenceTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration)1 ResolvedTypeDeclaration (com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration)1 ResolvedReferenceType (com.github.javaparser.resolution.types.ResolvedReferenceType)1