Package com.github.antlrjavaparser.api

Examples of com.github.antlrjavaparser.api.ImportDeclaration


        Validate.notNull(
                compilationUnit.getImports(),
                "Compilation unit imports should be non-null when producing type '%s'",
                cid.getName());
        for (final ImportMetadata importType : cid.getRegisteredImports()) {
            ImportDeclaration importDeclaration;

            if (!importType.isAsterisk()) {
                NameExpr typeToImportExpr;
                if (importType.getImportType().getEnclosingType() == null) {
                    typeToImportExpr = new QualifiedNameExpr(new NameExpr(
                            importType.getImportType().getPackage()
                                    .getFullyQualifiedPackageName()),
                            importType.getImportType().getSimpleTypeName());
                }
                else {
                    typeToImportExpr = new QualifiedNameExpr(new NameExpr(
                            importType.getImportType().getEnclosingType()
                                    .getFullyQualifiedTypeName()), importType
                            .getImportType().getSimpleTypeName());
                }

                importDeclaration = new ImportDeclaration(typeToImportExpr,
                        importType.isStatic(), false);
            }
            else {
                importDeclaration = new ImportDeclaration(new NameExpr(
                        importType.getImportPackage()
                                .getFullyQualifiedPackageName()),
                        importType.isStatic(), importType.isAsterisk());
            }

            JavaParserCommentMetadataBuilder.updateCommentsToJavaParser(
                    importDeclaration, importType.getCommentStructure());

            compilationUnit.getImports().add(importDeclaration);
        }

        // Create a class or interface declaration to represent this actual type
        final int javaParserModifier = JavaParserUtils
                .getJavaParserModifier(cid.getModifier());
        TypeDeclaration typeDeclaration;
        ClassOrInterfaceDeclaration classOrInterfaceDeclaration;

        // Implements handling
        final List<ClassOrInterfaceType> implementsList = new ArrayList<ClassOrInterfaceType>();
        for (final JavaType current : cid.getImplementsTypes()) {
            implementsList.add(JavaParserUtils.getResolvedName(cid.getName(),
                    current, compilationUnit));
        }

        if (cid.getPhysicalTypeCategory() == PhysicalTypeCategory.INTERFACE
                || cid.getPhysicalTypeCategory() == PhysicalTypeCategory.CLASS) {
            final boolean isInterface = cid.getPhysicalTypeCategory() == PhysicalTypeCategory.INTERFACE;

            if (parent == null) {
                // Top level type
                typeDeclaration = new ClassOrInterfaceDeclaration(
                        javaParserModifier, isInterface, cid
                                .getName()
                                .getNameIncludingTypeParameters()
                                .replace(
                                        cid.getName().getPackage()
                                                .getFullyQualifiedPackageName()
                                                + ".", ""));
                classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;
            }
            else {
                // Inner type
                typeDeclaration = new ClassOrInterfaceDeclaration(
                        javaParserModifier, isInterface, cid.getName()
                                .getSimpleTypeName());
                classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) typeDeclaration;

                if (cid.getName().getParameters().size() > 0) {
                    classOrInterfaceDeclaration
                            .setTypeParameters(new ArrayList<TypeParameter>());

                    for (final JavaType param : cid.getName().getParameters()) {
                        NameExpr pNameExpr = JavaParserUtils
                                .importTypeIfRequired(cid.getName(),
                                        compilationUnit.getImports(), param);
                        final String tempName = StringUtils.replace(
                                pNameExpr.toString(), param.getArgName()
                                        + " extends ", "", 1);
                        pNameExpr = new NameExpr(tempName);
                        final ClassOrInterfaceType pResolvedName = JavaParserUtils
                                .getClassOrInterfaceType(pNameExpr);
                        classOrInterfaceDeclaration.getTypeParameters().add(
                                new TypeParameter(param.getArgName()
                                        .getSymbolName(), Collections
                                        .singletonList(pResolvedName)));
                    }
                }
            }

            // Superclass handling
            final List<ClassOrInterfaceType> extendsList = new ArrayList<ClassOrInterfaceType>();
            for (final JavaType current : cid.getExtendsTypes()) {
                if (!OBJECT.equals(current)) {
                    extendsList.add(JavaParserUtils.getResolvedName(
                            cid.getName(), current, compilationUnit));
                }
            }
            if (extendsList.size() > 0) {
                classOrInterfaceDeclaration.setExtends(extendsList);
            }

            // Implements handling
            if (implementsList.size() > 0) {
                classOrInterfaceDeclaration.setImplements(implementsList);
            }
        }
        else {
            typeDeclaration = new EnumDeclaration(javaParserModifier, cid
                    .getName().getSimpleTypeName());
        }
        typeDeclaration.setMembers(new ArrayList<BodyDeclaration>());

        Validate.notNull(typeDeclaration.getName(),
                "Missing type declaration name for '%s'", cid.getName());

        // If adding a new top-level type, must add it to the compilation unit
        // types
        Validate.notNull(
                compilationUnit.getTypes(),
                "Compilation unit types must not be null when attempting to add '%s'",
                cid.getName());

        if (parent == null) {
            // Top-level class
            compilationUnit.getTypes().add(typeDeclaration);
        }
        else {
            // Inner class
            parent.add(typeDeclaration);
        }

        // If the enclosing CompilationUnitServices was not provided a default
        // CompilationUnitServices needs to be created
        if (enclosingCompilationUnitServices == null) {
            // Create a compilation unit so that we can use JavaType*Metadata
            // static methods directly
            enclosingCompilationUnitServices = new CompilationUnitServices() {
                @Override
                public JavaPackage getCompilationUnitPackage() {
                    return cid.getName().getPackage();
                }

                @Override
                public JavaType getEnclosingTypeName() {
                    return cid.getName();
                }

                @Override
                public List<ImportDeclaration> getImports() {
                    return compilationUnit.getImports();
                }

                @Override
                public List<TypeDeclaration> getInnerTypes() {
                    return compilationUnit.getTypes();
                }

                @Override
                public PhysicalTypeCategory getPhysicalTypeCategory() {
                    return cid.getPhysicalTypeCategory();
                }
            };
        }

        final CompilationUnitServices finalCompilationUnitServices = enclosingCompilationUnitServices;
        // A hybrid CompilationUnitServices must be provided that references the
        // enclosing types imports and package
        final CompilationUnitServices compilationUnitServices = new CompilationUnitServices() {
            @Override
            public JavaPackage getCompilationUnitPackage() {
                return finalCompilationUnitServices.getCompilationUnitPackage();
            }

            @Override
            public JavaType getEnclosingTypeName() {
                return cid.getName();
            }

            @Override
            public List<ImportDeclaration> getImports() {
                return finalCompilationUnitServices.getImports();
            }

            @Override
            public List<TypeDeclaration> getInnerTypes() {
                return compilationUnit.getTypes();
            }

            @Override
            public PhysicalTypeCategory getPhysicalTypeCategory() {
                return cid.getPhysicalTypeCategory();
            }
        };

        // Add type annotations
        final List<AnnotationExpr> annotations = new ArrayList<AnnotationExpr>();
        typeDeclaration.setAnnotations(annotations);
        for (final AnnotationMetadata candidate : cid.getAnnotations()) {
            JavaParserAnnotationMetadataBuilder.addAnnotationToList(
                    compilationUnitServices, annotations, candidate);
        }

        // Add enum constants and interfaces
        if (typeDeclaration instanceof EnumDeclaration
                && cid.getEnumConstants().size() > 0) {
            final EnumDeclaration enumDeclaration = (EnumDeclaration) typeDeclaration;

            final List<EnumConstantDeclaration> constants = new ArrayList<EnumConstantDeclaration>();
            enumDeclaration.setEntries(constants);

            for (final JavaSymbolName constant : cid.getEnumConstants()) {
                addEnumConstant(constants, constant);
            }

            // Implements handling
            if (implementsList.size() > 0) {
                enumDeclaration.setImplements(implementsList);
            }
        }

        // Add fields
        for (final FieldMetadata candidate : cid.getDeclaredFields()) {
            JavaParserFieldMetadataBuilder.addField(compilationUnitServices,
                    typeDeclaration.getMembers(), candidate);
        }

        // Add constructors
        for (final ConstructorMetadata candidate : cid
                .getDeclaredConstructors()) {
            JavaParserConstructorMetadataBuilder.addConstructor(
                    compilationUnitServices, typeDeclaration.getMembers(),
                    candidate, null);
        }

        // Add methods
        for (final MethodMetadata candidate : cid.getDeclaredMethods()) {
            JavaParserMethodMetadataBuilder.addMethod(compilationUnitServices,
                    typeDeclaration.getMembers(), candidate, null);
        }

        // Add inner types
        for (final ClassOrInterfaceTypeDetails candidate : cid
                .getDeclaredInnerTypes()) {
            updateOutput(compilationUnit, compilationUnitServices, candidate,
                    typeDeclaration.getMembers());
        }

        final HashSet<String> imported = new HashSet<String>();
        final ArrayList<ImportDeclaration> imports = new ArrayList<ImportDeclaration>();
        for (final ImportDeclaration importDeclaration : compilationUnit
                .getImports()) {
            JavaPackage importPackage = null;
            JavaType importType = null;
            if (importDeclaration.isAsterisk()) {
                importPackage = new JavaPackage(importDeclaration.getName()
                        .toString());
            }
            else {
                importType = new JavaType(importDeclaration.getName()
                        .toString());
                importPackage = importType.getPackage();
            }

            if (importPackage.equals(cid.getName().getPackage())
                    && importDeclaration.isAsterisk()) {
                continue;
            }

            if (importPackage.equals(cid.getName().getPackage())
                    && importType != null
                    && importType.getEnclosingType() == null) {
                continue;
            }

            if (importType != null && importType.equals(cid.getName())) {
                continue;
            }

            if (!imported.contains(importDeclaration.getName().toString())) {
                imports.add(importDeclaration);
                imported.add(importDeclaration.getName().toString());
            }
        }

        Collections.sort(imports, new Comparator<ImportDeclaration>() {
            @Override
            public int compare(final ImportDeclaration importDeclaration,
                    final ImportDeclaration importDeclaration1) {
                return importDeclaration.getName().toString()
                        .compareTo(importDeclaration1.getName().toString());
            }
        });

        compilationUnit.setImports(imports);
View Full Code Here


            // 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);
        }

        return new JavaType(importDeclaration.getName().toString());
    }
View Full Code Here

            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;

                // If this is a complete match, it indicates we can use the
                // simple type name
                if (isEqual(existingImport.getName(), newImport.getName())) {
                    useSimpleTypeName = true;
                    break;
                }
            }
        }
View Full Code Here

            cidImports.addAll(cidCompilationUnit.getImports());
        }
        if (compilationUnit.getImports() != null) {
            for (final Iterator<ImportDeclaration> originalImportIter = compilationUnit
                    .getImports().iterator(); originalImportIter.hasNext();) {
                final ImportDeclaration originalImport = originalImportIter
                        .next();
                notFound = true;
                for (final Iterator<ImportDeclaration> newImportIter = cidImports
                        .iterator(); newImportIter.hasNext();) {
                    final ImportDeclaration newImport = newImportIter.next();
                    if (equals(originalImport, newImport)) {
                        // new Import found in original imports
                        // remove from newImports to check
                        newImportIter.remove();
View Full Code Here

TOP

Related Classes of com.github.antlrjavaparser.api.ImportDeclaration

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.