Examples of NameExpr


Examples of japa.parser.ast.expr.NameExpr

        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

Examples of japa.parser.ast.expr.NameExpr

     */
    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

Examples of japa.parser.ast.expr.NameExpr

        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

Examples of japa.parser.ast.expr.NameExpr

        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

Examples of japa.parser.ast.expr.NameExpr

    }

    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

Examples of japa.parser.ast.expr.NameExpr

                        .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

Examples of japa.parser.ast.expr.NameExpr

        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

Examples of japa.parser.ast.expr.NameExpr

            final FieldAccessExpr field = (FieldAccessExpr) expression;
            final String fieldName = field.getField();

            // Determine the type
            final Expression scope = field.getScope();
            NameExpr nameToFind = null;
            if (scope instanceof FieldAccessExpr) {
                final FieldAccessExpr fScope = (FieldAccessExpr) scope;
                nameToFind = JavaParserUtils.getNameExpr(fScope.toString());
            }
            else if (scope instanceof NameExpr) {
                nameToFind = (NameExpr) scope;
            }
            else {
                throw new UnsupportedOperationException(
                        "A FieldAccessExpr for '"
                                + field.getScope()
                                + "' should return a NameExpr or FieldAccessExpr (was "
                                + field.getScope().getClass().getName() + ")");
            }
            final JavaType fieldType = JavaParserUtils.getJavaType(
                    compilationUnitServices, nameToFind, null);

            final EnumDetails enumDetails = new EnumDetails(fieldType,
                    new JavaSymbolName(fieldName));
            return new EnumAttributeValue(annotationName, enumDetails);
        }

        if (expression instanceof NameExpr) {
            final NameExpr field = (NameExpr) expression;
            final String name = field.getName();
            // As we have no way of finding out the real type
            final JavaType fieldType = new JavaType("unknown.Object");
            final EnumDetails enumDetails = new EnumDetails(fieldType,
                    new JavaSymbolName(name));
            return new EnumAttributeValue(annotationName, enumDetails);
View Full Code Here

Examples of japa.parser.ast.expr.NameExpr

      }

      if (n.getThrows() != null) {
        printer.print(" throws ");
        for (Iterator<NameExpr> i = n.getThrows().iterator(); i.hasNext();) {
          NameExpr name = i.next();
          name.accept(this, arg);
          if (i.hasNext()) {
            printer.print(", ");
          }
        }
      }
View Full Code Here

Examples of japa.parser.ast.expr.NameExpr

     *            qualified name
     * @return instanceof {@link NameExpr}
     */
    public static NameExpr createNameExpr(String qualifiedName) {
        String[] split = qualifiedName.split("\\.");
        NameExpr ret = new NameExpr(split[0]);
        for (int i = 1; i < split.length; i++) {
            ret = new QualifiedNameExpr(ret, split[i]);
        }
        return ret;
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.