private static Map < String , Spelling > findImports ( Elements elementUtils , Types typeUtils , String codePackageName , Set < TypeMirror > referenced , Set < TypeMirror > defined ) { Map < String , Spelling > imports = new HashMap <> ( ) ; Set < TypeMirror > typesInScope = new TypeMirrorSet ( ) ; typesInScope . addAll ( referenced ) ; typesInScope . addAll ( defined ) ; Set < String > ambiguous = ambiguousNames ( typeUtils , typesInScope ) ; for ( TypeMirror type : referenced ) { TypeElement typeElement = ( TypeElement ) typeUtils . asElement ( type ) ; String fullName = typeElement . getQualifiedName ( ) . toString ( ) ; String simpleName = typeElement . getSimpleName ( ) . toString ( ) ; String pkg = packageNameOf ( typeElement ) ; boolean importIt ; String spelling ; if ( ambiguous . contains ( simpleName ) ) { importIt = false ; spelling = fullName ; } else if ( pkg . equals ( "java.lang" ) ) { importIt = false ; spelling = javaLangSpelling ( elementUtils , codePackageName , typeElement ) ; } else if ( pkg . equals ( codePackageName ) ) { importIt = false ; spelling = fullName . substring ( pkg . isEmpty ( ) ? 0 : pkg . length ( ) + 1 ) ; } else { importIt = true ; spelling = simpleName ; } imports . put ( fullName , new Spelling ( spelling , importIt ) ) ; } return imports ; }
Given a set of referenced types works out which of them should be imported and what the resulting spelling of each one is .