Java Code Examples for com.sun.source.tree.Tree.Kind#PRIMITIVE_TYPE

The following examples show how to use com.sun.source.tree.Tree.Kind#PRIMITIVE_TYPE . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AddWsOperationHelper.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private String getMethodBody(Tree returnType) {
    String body = null;
    if (Kind.PRIMITIVE_TYPE == returnType.getKind()) {
        TypeKind type = ((PrimitiveTypeTree)returnType).getPrimitiveTypeKind();
        if (TypeKind.VOID == type) body = ""; //NOI18N
        else if (TypeKind.BOOLEAN == type) body = "return false;"; // NOI18N
        else if (TypeKind.INT == type) body = "return 0;"; // NOI18N
        else if (TypeKind.LONG == type) body = "return 0;"; // NOI18N
        else if (TypeKind.FLOAT == type) body = "return 0.0;"; // NOI18N
        else if (TypeKind.DOUBLE == type) body = "return 0.0;"; // NOI18N
        else if (TypeKind.BYTE == type) body = "return 0;"; // NOI18N
        else if (TypeKind.SHORT == type) body = "return 0;"; // NOI18N
        else if (TypeKind.CHAR == type) body = "return ' ';"; // NOI18N
        else body = "return null"; //NOI18N
    } else
        body = "return null"; //NOI18N
    return "{\n\t\t"+NbBundle.getMessage(AddWsOperationHelper.class, "TXT_TodoComment")+"\n"+body+"\n}";
}
 
Example 2
Source File: AddWsOperationHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private String getMethodBody(Tree returnType) {
    String body = null;
    if (Kind.PRIMITIVE_TYPE == returnType.getKind()) {
        TypeKind type = ((PrimitiveTypeTree)returnType).getPrimitiveTypeKind();
        if (TypeKind.VOID == type) {
            body = ""; //NOI18N
        }
        else if (TypeKind.BOOLEAN == type) {
            body = "return false;"; // NOI18N
        }
        else if (TypeKind.INT == type) {
            body = "return 0;"; // NOI18N
        }
        else if (TypeKind.LONG == type) {
            body = "return 0;"; // NOI18N
        }
        else if (TypeKind.FLOAT == type) {
            body = "return 0.0;"; // NOI18N
        }
        else if (TypeKind.DOUBLE == type) {
            body = "return 0.0;"; // NOI18N
        }
        else if (TypeKind.BYTE == type) {
            body = "return 0;"; // NOI18N
        }
        else if (TypeKind.SHORT == type) {
            body = "return 0;"; // NOI18N
        }
        else if (TypeKind.CHAR == type) {
            body = "return ' ';"; // NOI18N
        }
        else {
            body = "return null"; //NOI18N
        }
    } else
        body = "return null"; //NOI18N
    return "{\n\t\t"+NbBundle.getMessage(AddWsOperationHelper.class, "TXT_TodoComment")+"\n"+body+"\n}";
}
 
Example 3
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private String simpleName(Tree t) {
    if (t == null) {
        return Bundle.DisplayName_Unknown();
    }
    if (t.getKind() == Kind.IDENTIFIER) {
        return ((IdentifierTree) t).getName().toString();
    }

    if (t.getKind() == Kind.MEMBER_SELECT) {
        return ((MemberSelectTree) t).getIdentifier().toString();
    }

    if (t.getKind() == Kind.METHOD_INVOCATION) {
        return scan(t, null);
    }

    if (t.getKind() == Kind.PARAMETERIZED_TYPE) {
        return simpleName(((ParameterizedTypeTree) t).getType()) + "<...>"; // NOI18N
    }

    if (t.getKind() == Kind.ARRAY_ACCESS) {
        return simpleName(((ArrayAccessTree) t).getExpression()) + "[]"; //NOI18N
    }

    if (t.getKind() == Kind.PARENTHESIZED) {
        return "(" + simpleName(((ParenthesizedTree)t).getExpression()) + ")"; //NOI18N
    }

    if (t.getKind() == Kind.TYPE_CAST) {
        return simpleName(((TypeCastTree)t).getType());
    }

    if (t.getKind() == Kind.ARRAY_TYPE) {
        return simpleName(((ArrayTypeTree)t).getType());
    }

    if (t.getKind() == Kind.PRIMITIVE_TYPE) {
        return ((PrimitiveTypeTree) t).getPrimitiveTypeKind().name().toLowerCase();
    }
    
    throw new IllegalStateException("Currently unsupported kind of tree: " + t.getKind()); // NOI18N
}
 
Example 4
Source File: JaxWsClassesCookieImpl.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@org.netbeans.api.annotations.common.SuppressWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
@Override
public void addOperation(final MethodTree method) {
    JavaSource targetSource = JavaSource.forFileObject(implClassFO);
    CancellableTask<WorkingCopy> modificationTask = 
        new CancellableTask<WorkingCopy>() 
        {
        @Override
        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);            
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree javaClass = SourceUtils.
                getPublicTopLevelTree(workingCopy);
            if (javaClass!=null) {
                GenerationUtils genUtils = GenerationUtils.newInstance(
                            workingCopy);
                
                AnnotationTree webMethodAnnotation = make.Annotation(
                    make.QualIdent("javax.jws.WebMethod"),      //NOI18N
                    Collections.<ExpressionTree>emptyList()
                );
                // add @WebMethod annotation
                ModifiersTree modifiersTree = make.addModifiersAnnotation(
                        method.getModifiers(), webMethodAnnotation);
                
                // add @Oneway annotation
                if (Kind.PRIMITIVE_TYPE == method.getReturnType().getKind()) {
                    PrimitiveTypeTree primitiveType = 
                        (PrimitiveTypeTree)method.getReturnType();
                    if (TypeKind.VOID == primitiveType.getPrimitiveTypeKind()) {
                        AnnotationTree oneWayAnnotation = make.Annotation(
                            make.QualIdent("javax.jws.Oneway"),         // NOI18N
                            Collections.<ExpressionTree>emptyList()
                        );
                        modifiersTree = make.addModifiersAnnotation(
                                modifiersTree, oneWayAnnotation);
                    }
                }
                
                // add @WebParam annotations 
                List<? extends VariableTree> parameters = method.getParameters();
                List<VariableTree> newParameters = new ArrayList<VariableTree>();
                for (VariableTree param:parameters) {
                    AnnotationTree paramAnnotation = make.Annotation(
                        make.QualIdent("javax.jws.WebParam"),           //NOI18N
                        Collections.<ExpressionTree>singletonList(
                            make.Assignment(make.Identifier("name"),    //NOI18N
                                    make.Literal(param.getName().toString()))) 
                    );
                    newParameters.add(genUtils.addAnnotation(param, paramAnnotation));
                }
                // create new (annotated) method
                MethodTree  annotatedMethod = make.Method(
                            modifiersTree,
                            method.getName(),
                            method.getReturnType(),
                            method.getTypeParameters(),
                            newParameters,
                            method.getThrows(),
                            method.getBody(),
                            (ExpressionTree)method.getDefaultValue());
                Comment comment = Comment.create(NbBundle.getMessage(
                        JaxWsClassesCookieImpl.class, "TXT_WSOperation"));      //NOI18N                 
                make.addComment(annotatedMethod, comment, true);
                
                ClassTree modifiedClass = make.addClassMember(javaClass,
                        annotatedMethod);
                workingCopy.rewrite(javaClass, modifiedClass);
            }
        }
        @Override
        public void cancel() {
            
        }
    };
    try {
        targetSource.runModificationTask(modificationTask).commit();
    } catch (IOException ex) {
        ErrorManager.getDefault().notify(ex);
    }
}