Package japa.parser.ast.expr

Examples of japa.parser.ast.expr.NameExpr


        final List<ImportDeclaration> imports = compilationUnitServices
                .getImports();

        for (final ImportDeclaration candidate : imports) {
            final NameExpr candidateNameExpr = candidate.getName();
            if (!candidate.toString().contains("*")) {
                Validate.isInstanceOf(QualifiedNameExpr.class,
                        candidateNameExpr, "Expected import '" + candidate
                                + "' to use a fully-qualified type name");
            }
            if (nameExpr instanceof QualifiedNameExpr) {
                // User is asking for a fully-qualified name; let's see if there
                // is a full match
                if (isEqual(nameExpr, candidateNameExpr)) {
                    return candidate;
                }
            }
            else {
                // User is not asking for a fully-qualified name, so let's do a
                // simple name comparison that discards the import's
                // qualified-name package
                if (candidateNameExpr.getName().equals(nameExpr.getName())) {
                    return candidate;
                }
            }
        }
        return null;
View Full Code Here


        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()
View Full Code Here

        Validate.notNull(compilationUnitServices,
                "Compilation unit services required");
        Validate.notNull(typeDeclaration, "Type declaration required");

        // Convert the ClassOrInterfaceDeclaration name into a JavaType
        final NameExpr nameExpr = getNameExpr(typeDeclaration.getName());
        final JavaType effectiveType = getJavaType(compilationUnitServices,
                nameExpr, null);

        final List<JavaType> parameterTypes = new ArrayList<JavaType>();
        if (typeDeclaration instanceof ClassOrInterfaceDeclaration) {
View Full Code Here

        ClassOrInterfaceType scope = cit.getScope();
        while (scope != null) {
            typeName = scope.getName() + "." + typeName;
            scope = scope.getScope();
        }
        final NameExpr nameExpr = getNameExpr(typeName);

        final JavaType effectiveType = getJavaType(compilationUnitServices,
                nameExpr, typeParameters);

        // Handle any type arguments
View Full Code Here

     */
    public static NameExpr getNameExpr(final AnnotationExpr annotationExpr) {
        Validate.notNull(annotationExpr, "Annotation expression required");
        if (annotationExpr instanceof MarkerAnnotationExpr) {
            final MarkerAnnotationExpr a = (MarkerAnnotationExpr) annotationExpr;
            final NameExpr nameToFind = a.getName();
            Validate.notNull(nameToFind,
                    "Unable to determine annotation name from '"
                            + annotationExpr + "'");
            return nameToFind;
        }
        else if (annotationExpr instanceof SingleMemberAnnotationExpr) {
            final SingleMemberAnnotationExpr a = (SingleMemberAnnotationExpr) annotationExpr;
            final NameExpr nameToFind = a.getName();
            Validate.notNull(nameToFind,
                    "Unable to determine annotation name from '"
                            + annotationExpr + "'");
            return nameToFind;
        }
        else if (annotationExpr instanceof NormalAnnotationExpr) {
            final NormalAnnotationExpr a = (NormalAnnotationExpr) annotationExpr;
            final NameExpr nameToFind = a.getName();
            Validate.notNull(nameToFind,
                    "Unable to determine annotation name from '"
                            + annotationExpr + "'");
            return nameToFind;
        }
View Full Code Here

        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);
    }
View Full Code Here

        return new ReferenceType(getClassOrInterfaceType(nameExpr));
    }

    public static ClassOrInterfaceType getResolvedName(final JavaType target,
            final JavaType current, final CompilationUnit compilationUnit) {
        final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(target,
                compilationUnit.getImports(), current);
        final ClassOrInterfaceType resolvedName = JavaParserUtils
                .getClassOrInterfaceType(nameExpr);
        if (current.getParameters() != null
                && current.getParameters().size() > 0) {
View Full Code Here

    }

    public static Type getResolvedName(final JavaType target,
            final JavaType current,
            final CompilationUnitServices compilationUnit) {
        final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(target,
                compilationUnit.getImports(), current);
        final ClassOrInterfaceType resolvedName = JavaParserUtils
                .getClassOrInterfaceType(nameExpr);
        if (current.getParameters() != null
                && current.getParameters().size() > 0) {
View Full Code Here

                        .getQualifier().getName();
                final String simpleName = ((QualifiedNameExpr) scope).getName();
                final String fullyQualifiedName = packageName + "."
                        + simpleName;
                final JavaType javaType = new JavaType(fullyQualifiedName);
                final NameExpr nameToUse = importTypeIfRequired(targetType,
                        imports, javaType);
                if (!(nameToUse instanceof QualifiedNameExpr)) {
                    return new FieldAccessExpr(nameToUse, field);
                }
            }
        }
        else if (value instanceof ClassExpr) {
            final Type type = ((ClassExpr) value).getType();
            if (type instanceof ClassOrInterfaceType) {
                final JavaType javaType = new JavaType(
                        ((ClassOrInterfaceType) type).getName());
                final NameExpr nameToUse = importTypeIfRequired(targetType,
                        imports, javaType);
                if (!(nameToUse instanceof QualifiedNameExpr)) {
                    return new ClassExpr(new ClassOrInterfaceType(
                            javaType.getSimpleTypeName()));
                }
            }
            else if (type instanceof ReferenceType
                    && ((ReferenceType) type).getType() instanceof ClassOrInterfaceType) {
                final ClassOrInterfaceType cit = (ClassOrInterfaceType) ((ReferenceType) type)
                        .getType();
                final JavaType javaType = new JavaType(cit.getName());
                final NameExpr nameToUse = importTypeIfRequired(targetType,
                        imports, javaType);
                if (!(nameToUse instanceof QualifiedNameExpr)) {
                    return new ClassExpr(new ClassOrInterfaceType(
                            javaType.getSimpleTypeName()));
                }
View Full Code Here

        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;

                // If this is a complete match, it indicates we can use the
                // 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;
        }

        // This is pretty crude, but at least it emits source code for people
        // (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());
    }
View Full Code Here

TOP

Related Classes of japa.parser.ast.expr.NameExpr

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.