Package japa.parser.ast.type

Examples of japa.parser.ast.type.Type


      @Override
      public void visit(MethodDeclaration n, Object arg) {
        if (n.getName().equals(name)) {
          int modifiers2 = n.getModifiers();
          if (modifiers2 == modis) {
            Type type = n.getType();
            if (type.toString().equals(returnType)) {
              List<Parameter> ps = n.getParameters();
              if (ps == null)
                ps = new ArrayList<Parameter>();
              if (paramsMatch(params, ps)) {
                sb.append(1);
View Full Code Here


      @Override
      public void visit(MethodDeclaration n, Object arg) {
        if (n.getName().equals(name)) {
          int modifiers2 = n.getModifiers();
          if (modifiers2 == modis) {
            Type type = n.getType();
            if (type.toString().equals(returnType)) {
              List<Parameter> ps = n.getParameters();
              if (ps == null)
                ps = new ArrayList<Parameter>();
              if (paramsMatch(params, ps)) {
                sb.append(1);
View Full Code Here

            return JavaType.VOID_PRIMITIVE;
        }

        int array = 0;

        Type internalType = type;
        if (internalType instanceof ReferenceType) {
            array = ((ReferenceType) internalType).getArrayCount();
            if (array > 0) {
                internalType = ((ReferenceType) internalType).getType();
            }
        }

        if (internalType instanceof PrimitiveType) {
            final PrimitiveType pt = (PrimitiveType) internalType;
            if (pt.getType().equals(Primitive.Boolean)) {
                return new JavaType(Boolean.class.getName(), array,
                        DataType.PRIMITIVE, null, null);
            }
            if (pt.getType().equals(Primitive.Char)) {
                return new JavaType(Character.class.getName(), array,
                        DataType.PRIMITIVE, null, null);
            }
            if (pt.getType().equals(Primitive.Byte)) {
                return new JavaType(Byte.class.getName(), array,
                        DataType.PRIMITIVE, null, null);
            }
            if (pt.getType().equals(Primitive.Short)) {
                return new JavaType(Short.class.getName(), array,
                        DataType.PRIMITIVE, null, null);
            }
            if (pt.getType().equals(Primitive.Int)) {
                return new JavaType(Integer.class.getName(), array,
                        DataType.PRIMITIVE, null, null);
            }
            if (pt.getType().equals(Primitive.Long)) {
                return new JavaType(Long.class.getName(), array,
                        DataType.PRIMITIVE, null, null);
            }
            if (pt.getType().equals(Primitive.Float)) {
                return new JavaType(Float.class.getName(), array,
                        DataType.PRIMITIVE, null, null);
            }
            if (pt.getType().equals(Primitive.Double)) {
                return new JavaType(Double.class.getName(), array,
                        DataType.PRIMITIVE, null, null);
            }
            throw new IllegalStateException("Unsupported primitive '"
                    + pt.getType() + "'");
        }

        if (internalType instanceof WildcardType) {
            // We only provide very primitive support for wildcard types; Roo
            // only needs metadata at the end of the day,
            // not complete binding support from an AST
            final WildcardType wt = (WildcardType) internalType;
            if (wt.getSuper() != null) {
                final ReferenceType rt = wt.getSuper();
                final ClassOrInterfaceType cit = (ClassOrInterfaceType) rt
                        .getType();
                final JavaType effectiveType = getJavaTypeNow(
                        compilationUnitServices, cit, typeParameters);
                return new JavaType(effectiveType.getFullyQualifiedTypeName(),
                        rt.getArrayCount(), effectiveType.getDataType(),
                        JavaType.WILDCARD_SUPER, effectiveType.getParameters());
            }
            else if (wt.getExtends() != null) {
                final ReferenceType rt = wt.getExtends();
                final ClassOrInterfaceType cit = (ClassOrInterfaceType) rt
                        .getType();
                final JavaType effectiveType = getJavaTypeNow(
                        compilationUnitServices, cit, typeParameters);
                return new JavaType(effectiveType.getFullyQualifiedTypeName(),
                        rt.getArrayCount(), effectiveType.getDataType(),
                        JavaType.WILDCARD_EXTENDS,
                        effectiveType.getParameters());
            }
            else {
                return new JavaType(OBJECT.getFullyQualifiedTypeName(), 0,
                        DataType.TYPE, JavaType.WILDCARD_NEITHER, null);
            }
        }

        ClassOrInterfaceType cit;
        if (internalType instanceof ClassOrInterfaceType) {
            cit = (ClassOrInterfaceType) internalType;
        }
        else if (internalType instanceof ReferenceType) {
            cit = (ClassOrInterfaceType) ((ReferenceType) type).getType();
        }
        else {
            throw new IllegalStateException("The presented type '"
                    + internalType.getClass() + "' with value '" + internalType
                    + "' is unsupported by JavaParserUtils");
        }

        final JavaType effectiveType = getJavaTypeNow(compilationUnitServices,
                cit, typeParameters);
View Full Code Here

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

     * @param initType
     * @return the final {@link ClassOrInterfaceType} or null if no {@link ClassOrInterfaceType} found
     *
     */
    public static ClassOrInterfaceType getClassOrInterfaceType(Type type) {
        Type tmp = type;
        while (tmp instanceof ReferenceType) {
               tmp = ((ReferenceType) tmp).getType();
        };
        if (tmp instanceof ClassOrInterfaceType){
          return (ClassOrInterfaceType) tmp;
View Full Code Here

        Validate.notNull(field, "Field required");

        JavaParserUtils.importTypeIfRequired(
                compilationUnitServices.getEnclosingTypeName(),
                compilationUnitServices.getImports(), field.getFieldType());
        final Type initType = JavaParserUtils.getResolvedName(
                compilationUnitServices.getEnclosingTypeName(),
                field.getFieldType(), compilationUnitServices);
        ClassOrInterfaceType finalType = JavaParserUtils
                .getClassOrInterfaceType(initType);
View Full Code Here

        modifier = JavaParserUtils.getJdkModifier(fieldDeclaration
                .getModifiers());

        this.declaredByMetadataId = declaredByMetadataId;

        final Type type = fieldDeclaration.getType();
        fieldType = JavaParserUtils.getJavaType(compilationUnitServices, type,
                typeParameters);

        // Convert into an array if this variable ID uses array notation
        if (var.getId().getArrayCount() > 0) {
View Full Code Here

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

            // Compute the parameter type
            Type parameterType = null;
            if (constructorParameter.getJavaType().isPrimitive()) {
                parameterType = JavaParserUtils.getType(constructorParameter
                        .getJavaType());
            }
            else {
                final Type finalType = JavaParserUtils.getResolvedName(
                        constructorParameter.getJavaType(),
                        constructorParameter.getJavaType(),
                        compilationUnitServices);
                final ClassOrInterfaceType cit = JavaParserUtils
                        .getClassOrInterfaceType(finalType);
View Full Code Here

        body = body.substring(0, body.lastIndexOf("}"));

        // Lookup the parameters and their names
        if (constructorDeclaration.getParameters() != null) {
            for (final Parameter p : constructorDeclaration.getParameters()) {
                final Type pt = p.getType();
                final JavaType parameterType = JavaParserUtils.getJavaType(
                        compilationUnitServices, pt, fullTypeParameters);

                final List<AnnotationExpr> annotationsList = p.getAnnotations();
                final List<AnnotationMetadata> annotations = new ArrayList<AnnotationMetadata>();
View Full Code Here

        if (typeParameters == null) {
            typeParameters = new HashSet<JavaSymbolName>();
        }

        // Create the return type we should use
        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);
            }
            d.setThrows(throwsTypes);
        }

        // Set the body
        if (StringUtils.isBlank(method.getBody())) {
            // Never set the body if an abstract method
            if (!Modifier.isAbstract(method.getModifier())
                    && !PhysicalTypeCategory.INTERFACE
                            .equals(compilationUnitServices
                                    .getPhysicalTypeCategory())) {
                d.setBody(new BlockStmt());
            }
        }
        else {
            // There is a body.
            // We need to make a fake method that we can have JavaParser parse.
            // Easiest way to do that is to build a simple source class
            // containing the required method and re-parse it.
            final StringBuilder sb = new StringBuilder();
            sb.append("class TemporaryClass {\n");
            sb.append("  public void temporaryMethod() {\n");
            sb.append(method.getBody());
            sb.append("\n");
            sb.append("  }\n");
            sb.append("}\n");
            final ByteArrayInputStream bais = new ByteArrayInputStream(sb
                    .toString().getBytes());
            CompilationUnit ci;
            try {
                ci = JavaParser.parse(bais);
            }
            catch (final ParseException pe) {
                throw new IllegalStateException(
                        "Illegal state: JavaParser did not parse correctly", pe);
            }
            final List<TypeDeclaration> types = ci.getTypes();
            if (types == null || types.size() != 1) {
                throw new IllegalArgumentException("Method body invalid");
            }
            final TypeDeclaration td = types.get(0);
            final List<BodyDeclaration> bodyDeclarations = td.getMembers();
            if (bodyDeclarations == null || bodyDeclarations.size() != 1) {
                throw new IllegalStateException(
                        "Illegal state: JavaParser did not return body declarations correctly");
            }
            final BodyDeclaration bd = bodyDeclarations.get(0);
            if (!(bd instanceof MethodDeclaration)) {
                throw new IllegalStateException(
                        "Illegal state: JavaParser did not return a method declaration correctly");
            }
            final MethodDeclaration md = (MethodDeclaration) bd;
            d.setBody(md.getBody());
        }

        // Locate where to add this method; also verify if this method already
        // exists
        for (final BodyDeclaration bd : members) {
            if (bd instanceof MethodDeclaration) {
                // Next method should appear after this current method
                final MethodDeclaration md = (MethodDeclaration) bd;
                if (md.getName().equals(d.getName())) {
                    if ((md.getParameters() == null || md.getParameters()
                            .isEmpty())
                            && (d.getParameters() == null || d.getParameters()
                                    .isEmpty())) {
                        throw new IllegalStateException("Method '"
                                + method.getMethodName().getSymbolName()
                                + "' already exists");
                    }
                    else if (md.getParameters() != null
                            && md.getParameters().size() == d.getParameters()
                                    .size()) {
                        // Possible match, we need to consider parameter types
                        // as well now
                        final MethodMetadata methodMetadata = JavaParserMethodMetadataBuilder
                                .getInstance(method.getDeclaredByMetadataId(),
                                        md, compilationUnitServices,
                                        typeParameters).build();
                        boolean matchesFully = true;
                        index = -1;
                        for (final AnnotatedJavaType existingParameter : methodMetadata
                                .getParameterTypes()) {
                            index++;
                            final AnnotatedJavaType parameterType = method
                                    .getParameterTypes().get(index);
                            if (!existingParameter.getJavaType().equals(
                                    parameterType.getJavaType())) {
                                matchesFully = false;
                                break;
                            }
                        }
                        if (matchesFully) {
View Full Code Here

TOP

Related Classes of japa.parser.ast.type.Type

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.