Package japa.parser.ast.expr

Examples of japa.parser.ast.expr.NameExpr


        return this;
    }

    public CompilationUnitBuilder buildImports(String[] imports) {
        List<ImportDeclaration> importList = new ArrayList<ImportDeclaration>();
        importList.add(new ImportDeclaration(new NameExpr("org.testng.annotations.Test"), false, false));
        cu.setImports(importList);
        return this;
    }
View Full Code Here


    public ClassTypeBuilder buildMethod(String methodName, MethodDeclaration method, String... params) {

        method.setName(methodName);
//        MarkerAnnotationExpr markerAnnotationExpr = new MarkerAnnotationExpr(new NameExpr("Test"));
        List<MemberValuePair> memberValuePairs = new ArrayList<MemberValuePair>();
        memberValuePairs.add(new MemberValuePair("description", new NameExpr("申艳超的测试用例")));
        NormalAnnotationExpr normalAnnotationExpr = new NormalAnnotationExpr(new NameExpr("Test"), null);
        List<AnnotationExpr> annotationExprList = new ArrayList<AnnotationExpr>();
//        annotationExprList.add(markerAnnotationExpr);
        annotationExprList.add(normalAnnotationExpr);
        method.getParameters().clear();
        method.setAnnotations(annotationExprList);
View Full Code Here

        //add a body to the method
        BlockStmt block = new BlockStmt();
        method.setBody(block);

        //add a statement do the method body
        NameExpr clazz = new NameExpr("System");
        FieldAccessExpr field = new FieldAccessExpr(clazz, "out");
        MethodCallExpr call = new MethodCallExpr(field, "println");
        ASTHelper.addArgument(call, new StringLiteralExpr("Hello World"));
        ASTHelper.addStmt(block, call);
View Full Code Here

    public static String getTestPackageNameFrom(PackageDeclaration packageDeclaration) {
        if (null == packageDeclaration) {
            return "test";
        } else {
            NameExpr packageNameExpr = packageDeclaration.getName();
            String packageName = packageNameExpr.toString();
            return packageName + ".test";
        }
    }
View Full Code Here

        Validate.notNull(compilationUnit.getImports(),
                "Compilation unit imports should be non-null when producing type '"
                        + cid.getName() + "'");
        for (final ImportMetadata importType : cid.getRegisteredImports()) {
            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());
                }
                compilationUnit.getImports().add(
                        new ImportDeclaration(typeToImportExpr, importType
                                .isStatic(), false));
            }
            else {
                compilationUnit.getImports().add(
                        new ImportDeclaration(new NameExpr(importType
                                .getImportPackage()
                                .getFullyQualifiedPackageName()), importType
                                .isStatic(), importType.isAsterisk()));
            }
        }

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

            // Resolve imports (ROO-1505)
            if (init instanceof ObjectCreationExpr) {
                final ObjectCreationExpr ocr = (ObjectCreationExpr) init;
                final JavaType typeToImport = JavaParserUtils.getJavaTypeNow(
                        compilationUnitServices, ocr.getType(), null);
                final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(
                        compilationUnitServices.getEnclosingTypeName(),
                        compilationUnitServices.getImports(), typeToImport);
                final ClassOrInterfaceType classOrInterfaceType = JavaParserUtils
                        .getClassOrInterfaceType(nameExpr);
                ocr.setType(classOrInterfaceType);
View Full Code Here

        // Create a holder for the annotation we're going to create
        boolean foundExisting = false;

        // Search for an existing annotation of this type
        for (final AnnotationExpr candidate : annotations) {
            NameExpr existingName = null;
            if (candidate instanceof NormalAnnotationExpr) {
                existingName = ((NormalAnnotationExpr) candidate).getName();
            }
            else if (candidate instanceof MarkerAnnotationExpr) {
                existingName = ((MarkerAnnotationExpr) candidate).getName();
            }
            else if (candidate instanceof SingleMemberAnnotationExpr) {
                existingName = ((SingleMemberAnnotationExpr) candidate)
                        .getName();
            }

            // Convert the candidate annotation type's into a JavaType
            final JavaType javaType = JavaParserUtils.getJavaType(
                    compilationUnitServices, existingName, null);
            if (annotation.getAnnotationType().equals(javaType)) {
                foundExisting = true;
                break;
            }
        }
        Validate.isTrue(
                !foundExisting,
                "Found an existing annotation for type '"
                        + annotation.getAnnotationType() + "'");

        // Import the annotation type, if needed
        final NameExpr nameToUse = JavaParserUtils.importTypeIfRequired(
                compilationUnitServices.getEnclosingTypeName(),
                compilationUnitServices.getImports(),
                annotation.getAnnotationType());

        // Create member-value pairs in accordance with Java Parser requirements
View Full Code Here

        }

        if (value instanceof ClassAttributeValue) {
            final JavaType castValue = ((ClassAttributeValue) value).getValue();
            // This doesn't preserve type parameters
            final NameExpr nameExpr = JavaParserUtils.getNameExpr(castValue
                    .getFullyQualifiedTypeName());
            final ClassExpr convertedValue = new ClassExpr(
                    JavaParserUtils.getReferenceType(nameExpr));
            return new MemberValuePair(value.getName().getSymbolName(),
                    convertedValue);
View Full Code Here

                "Compilation unit services required");

        // Obtain the annotation type name from the assorted types of
        // annotations we might have received (ie marker annotations, single
        // member annotations, normal annotations etc)
        final NameExpr nameToFind = JavaParserUtils.getNameExpr(annotationExpr);

        // Compute the actual annotation type, having regard to the compilation
        // unit package and imports
        annotationType = JavaParserUtils.getJavaType(compilationUnitServices,
                nameToFind, null);
View Full Code Here

        Type returnType = null;
        if (method.getReturnType().isPrimitive()) {
            returnType = JavaParserUtils.getType(method.getReturnType());
        }
        else {
            final NameExpr importedType = JavaParserUtils.importTypeIfRequired(
                    compilationUnitServices.getEnclosingTypeName(),
                    compilationUnitServices.getImports(),
                    method.getReturnType());
            final ClassOrInterfaceType cit = JavaParserUtils
                    .getClassOrInterfaceType(importedType);

            // Add any type arguments presented for the return type
            if (method.getReturnType().getParameters().size() > 0) {
                final List<Type> typeArgs = new ArrayList<Type>();
                cit.setTypeArgs(typeArgs);
                for (final JavaType parameter : method.getReturnType()
                        .getParameters()) {
                    typeArgs.add(JavaParserUtils.importParametersForType(
                            compilationUnitServices.getEnclosingTypeName(),
                            compilationUnitServices.getImports(), parameter));
                }
            }

            // Handle arrays
            if (method.getReturnType().isArray()) {
                final ReferenceType rt = new ReferenceType();
                rt.setArrayCount(method.getReturnType().getArray());
                rt.setType(cit);
                returnType = rt;
            }
            else {
                returnType = cit;
            }
        }

        // Start with the basic method
        final MethodDeclaration d = new MethodDeclaration();
        d.setModifiers(JavaParserUtils.getJavaParserModifier(method
                .getModifier()));
        d.setName(method.getMethodName().getSymbolName());
        d.setType(returnType);

        // Add any method-level annotations (not parameter annotations)
        final List<AnnotationExpr> annotations = new ArrayList<AnnotationExpr>();
        d.setAnnotations(annotations);
        for (final AnnotationMetadata annotation : method.getAnnotations()) {
            JavaParserAnnotationMetadataBuilder.addAnnotationToList(
                    compilationUnitServices, annotations, annotation);
        }

        // Add any method parameters, including their individual annotations and
        // type parameters
        final List<Parameter> parameters = new ArrayList<Parameter>();
        d.setParameters(parameters);

        int index = -1;
        for (final AnnotatedJavaType methodParameter : method
                .getParameterTypes()) {
            index++;

            // Add the parameter annotations applicable for this parameter type
            final List<AnnotationExpr> parameterAnnotations = new ArrayList<AnnotationExpr>();

            for (final AnnotationMetadata parameterAnnotation : methodParameter
                    .getAnnotations()) {
                JavaParserAnnotationMetadataBuilder.addAnnotationToList(
                        compilationUnitServices, parameterAnnotations,
                        parameterAnnotation);
            }

            // Compute the parameter name
            final String parameterName = method.getParameterNames().get(index)
                    .getSymbolName();

            // Compute the parameter type
            Type parameterType = null;
            if (methodParameter.getJavaType().isPrimitive()) {
                parameterType = JavaParserUtils.getType(methodParameter
                        .getJavaType());
            }
            else {
                final NameExpr type = JavaParserUtils.importTypeIfRequired(
                        compilationUnitServices.getEnclosingTypeName(),
                        compilationUnitServices.getImports(),
                        methodParameter.getJavaType());
                final ClassOrInterfaceType cit = JavaParserUtils
                        .getClassOrInterfaceType(type);

                // Add any type arguments presented for the return type
                if (methodParameter.getJavaType().getParameters().size() > 0) {
                    final List<Type> typeArgs = new ArrayList<Type>();
                    cit.setTypeArgs(typeArgs);
                    for (final JavaType parameter : methodParameter
                            .getJavaType().getParameters()) {
                        typeArgs.add(JavaParserUtils.importParametersForType(
                                compilationUnitServices.getEnclosingTypeName(),
                                compilationUnitServices.getImports(), parameter));
                    }
                }

                // Handle arrays
                if (methodParameter.getJavaType().isArray()) {
                    final ReferenceType rt = new ReferenceType();
                    rt.setArrayCount(methodParameter.getJavaType().getArray());
                    rt.setType(cit);
                    parameterType = rt;
                }
                else {
                    parameterType = cit;
                }
            }

            // Create a Java Parser method parameter and add it to the list of
            // parameters
            final Parameter p = new Parameter(parameterType,
                    new VariableDeclaratorId(parameterName));
            p.setVarArgs(methodParameter.isVarArgs());
            p.setAnnotations(parameterAnnotations);
            parameters.add(p);
        }

        // Add exceptions which the method my throw
        if (method.getThrowsTypes().size() > 0) {
            final List<NameExpr> throwsTypes = new ArrayList<NameExpr>();
            for (final JavaType javaType : method.getThrowsTypes()) {
                final NameExpr importedType = JavaParserUtils
                        .importTypeIfRequired(
                                compilationUnitServices.getEnclosingTypeName(),
                                compilationUnitServices.getImports(), javaType);
                throwsTypes.add(importedType);
            }
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.