com.sun.source.tree.TypeParameterTree Java Examples

The following examples show how to use com.sun.source.tree.TypeParameterTree. 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: JerseyGenerationStrategy.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
MethodTree generateClose(TreeMaker maker, WorkingCopy copy){
    ModifiersTree methodModifier = maker.Modifiers(
            Collections.<Modifier>singleton(Modifier.PUBLIC));
    return maker.Method (
            methodModifier,
            "close", //NOI18N
            JavaSourceHelper.createTypeTree(copy, "void"), //NOI18N
            Collections.<TypeParameterTree>emptyList(),
            Collections.<VariableTree>emptyList(),
            Collections.<ExpressionTree>emptyList(),
            "{"+ //NOI18N
            "   client.destroy();"+ //NOI18N
            "}", //NOI18N
            null); 
}
 
Example #2
Source File: JUnit5TestGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 */
@Override
protected ClassTree composeNewTestClass(WorkingCopy workingCopy,
                                        String name,
                                        List<? extends Tree> members) {
    final TreeMaker maker = workingCopy.getTreeMaker();
    ModifiersTree modifiers = maker.Modifiers(
                                  Collections.<Modifier>singleton(PUBLIC));
    return maker.Class(
                modifiers,                                 //modifiers
                name,                                      //name
                Collections.<TypeParameterTree>emptyList(),//type params
                null,                                      //extends
                Collections.<ExpressionTree>emptyList(),   //implements
                members);                                  //members
}
 
Example #3
Source File: JUnit5TestGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 */
@Override
protected MethodTree composeNewTestMethod(String testMethodName,
                                          BlockTree testMethodBody,
                                          List<ExpressionTree> throwsList,
                                          WorkingCopy workingCopy) {
    TreeMaker maker = workingCopy.getTreeMaker();
    return maker.Method(
            createModifiersTree(ANN_TEST,
                                createModifierSet(PUBLIC),
                                workingCopy),
            testMethodName,
            maker.PrimitiveType(TypeKind.VOID),
            Collections.<TypeParameterTree>emptyList(),
            Collections.<VariableTree>emptyList(),
            throwsList,
            testMethodBody,
            null);          //default value - used by annotations
}
 
Example #4
Source File: ApplicationSubclassGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private ClassTree createMethodsOlderVersion(Collection<String> classNames,
        TreeMaker maker,ClassTree modified,
        CompilationController controller) throws IOException
{
    WildcardTree wildCard = maker.Wildcard(Tree.Kind.UNBOUNDED_WILDCARD,
            null);
    ParameterizedTypeTree wildClass = maker.ParameterizedType(
            maker.QualIdent(Class.class.getCanonicalName()),
            Collections.singletonList(wildCard));
    ParameterizedTypeTree wildSet = maker.ParameterizedType(
            maker.QualIdent(Set.class.getCanonicalName()),
            Collections.singletonList(wildClass));
    //StringBuilder builder = new StringBuilder();
    String methodBody = MiscPrivateUtilities.collectRestResources(classNames, restSupport, true);
    ModifiersTree modifiersTree = maker.Modifiers(EnumSet
            .of(Modifier.PRIVATE));
    MethodTree methodTree = maker.Method(modifiersTree,
            GET_REST_RESOURCE_CLASSES, wildSet,
            Collections.<TypeParameterTree> emptyList(),
            Collections.<VariableTree> emptyList(),
            Collections.<ExpressionTree> emptyList(), methodBody,
            null);
    modified = maker.addClassMember(modified, methodTree);
    return modified;
}
 
Example #5
Source File: IntroduceHint.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static void prepareTypeVars(TreePath method, CompilationInfo info, Map<TypeMirror, TreePathHandle> typeVar2Def, List<TreePathHandle> typeVars) throws IllegalArgumentException {
    if (method.getLeaf().getKind() == Kind.METHOD) {
        MethodTree mt = (MethodTree) method.getLeaf();

        for (TypeParameterTree tv : mt.getTypeParameters()) {
            TreePath def = new TreePath(method, tv);
            TypeMirror type = info.getTrees().getTypeMirror(def);

            if (type != null && type.getKind() == TypeKind.TYPEVAR) {
                TreePathHandle tph = TreePathHandle.create(def, info);

                typeVar2Def.put(type, tph);
                typeVars.add(tph);
            }
        }
    }
}
 
Example #6
Source File: AbstractTestGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a public static {@code main(String[])} method
 * with the body taken from settings.
 *
 * @param  maker  {@code TreeMaker} to use for creating the method
 * @return  created {@code main(...)} method,
 *          or {@code null} if the method body would be empty
 */
private MethodTree createMainMethod(TreeMaker maker) {
    String initialMainMethodBody = getInitialMainMethodBody();
    if (initialMainMethodBody.length() == 0) {
        return null;
    }

    ModifiersTree modifiers = maker.Modifiers(
            createModifierSet(Modifier.PUBLIC, Modifier.STATIC));
    VariableTree param = maker.Variable(
                    maker.Modifiers(Collections.<Modifier>emptySet()),
                    "argList",                                      //NOI18N
                    maker.Identifier("String[]"),                   //NOI18N
                    null);            //initializer - not used in params
    MethodTree mainMethod = maker.Method(
          modifiers,                            //public static
          "main",                               //method name "main"//NOI18N
          maker.PrimitiveType(TypeKind.VOID),   //return type "void"
          Collections.<TypeParameterTree>emptyList(),     //type params
          Collections.<VariableTree>singletonList(param), //method param
          Collections.<ExpressionTree>emptyList(),        //throws-list
          '{' + initialMainMethodBody + '}',    //body text
          null);                                //only for annotations

    return mainMethod;
}
 
Example #7
Source File: JUnit4TestGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 */
@Override
protected MethodTree composeNewTestMethod(String testMethodName,
                                          BlockTree testMethodBody,
                                          List<ExpressionTree> throwsList,
                                          WorkingCopy workingCopy) {
    TreeMaker maker = workingCopy.getTreeMaker();
    return maker.Method(
            createModifiersTree(ANN_TEST,
                                createModifierSet(PUBLIC),
                                workingCopy),
            testMethodName,
            maker.PrimitiveType(TypeKind.VOID),
            Collections.<TypeParameterTree>emptyList(),
            Collections.<VariableTree>emptyList(),
            throwsList,
            testMethodBody,
            null);          //default value - used by annotations
}
 
Example #8
Source File: AbstractTestGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Makes a list of trees representing the given type parameter elements.
 */
private static List<TypeParameterTree> makeTypeParamsCopy(
                            List<? extends TypeParameterElement> typeParams,
                            TreeMaker maker) {
    if (typeParams.isEmpty()) {
        return Collections.emptyList();
    }

    int size = typeParams.size();
    if (size == 1) {
        return Collections.singletonList(makeCopy(typeParams.get(0), maker));
    }

    List<TypeParameterTree> result = new ArrayList<>(size);
    for (TypeParameterElement typeParam : typeParams) {
        result.add(makeCopy(typeParam, maker));
    }
    return result;
}
 
Example #9
Source File: TagHandlerGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private MethodTree addBodyEvaluatorCheck(boolean evaluateBody, TreeMaker make) throws IOException {        
    StringBuffer methodBody = new StringBuffer();
    methodBody.append("{"); //NOI18N
    methodBody.append("\n        // TODO: code that determines whether the body should be"); //NOI18N
    methodBody.append("\n        //       evaluated should be placed here."); //NOI18N
    methodBody.append("\n        //       Called from the doStartTag() method."); //NOI18N
    methodBody.append("\nreturn " + (evaluateBody ? "true;" : "false;")); //NOI18N
    methodBody.append("\n}"); //NOI18N
    
    MethodTree method = make.Method(
            make.Modifiers(Collections.<Modifier>singleton(Modifier.PRIVATE)),
            "theBodyShouldBeEvaluated", //NOI18N
            make.PrimitiveType(TypeKind.BOOLEAN),
            Collections.<TypeParameterTree>emptyList(),
            Collections.<VariableTree>emptyList(),
            Collections.<ExpressionTree>emptyList(),
            methodBody.toString(),
            null);
    
    //TODO: generate Javadoc
    
    return method;
}
 
Example #10
Source File: EntityMethodGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public MethodTree createToStringMethod(String fqn, List<VariableTree> fields) {
    StringBuilder body = new StringBuilder(30 + fields.size() * 30);
    body.append("{"); // NOI18N
    body.append("return \"" + fqn + "[ "); // NOI18N
    for (Iterator<VariableTree> i = fields.iterator(); i.hasNext();) {
        String fieldName = i.next().getName().toString();
        body.append(fieldName + "=\" + " + fieldName + " + \""); //NOI18N
        body.append(i.hasNext() ? ", " : " ]\";"); //NOI18N
    }
    body.append("}"); // NOI18N
    TreeMaker make = copy.getTreeMaker();
    // XXX Javadoc
    return make.Method(make.Modifiers(EnumSet.of(Modifier.PUBLIC),
            Collections.singletonList(genUtils.createAnnotation("java.lang.Override"))), "toString",
            genUtils.createType("java.lang.String", scope), Collections.<TypeParameterTree>emptyList(),
            Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), body.toString(), null);
}
 
Example #11
Source File: AbstractTestGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Makes a list of trees representing the given type parameter elements.
 */
private static List<TypeParameterTree> makeTypeParamsCopy(
                            List<? extends TypeParameterElement> typeParams,
                            TreeMaker maker) {
    if (typeParams.isEmpty()) {
        return Collections.emptyList();
    }

    int size = typeParams.size();
    if (size == 1) {
        return Collections.singletonList(makeCopy(typeParams.get(0), maker));
    }

    List<TypeParameterTree> result = new ArrayList(size);
    for (TypeParameterElement typeParam : typeParams) {
        result.add(makeCopy(typeParam, maker));
    }
    return result;
}
 
Example #12
Source File: EntityMethodGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public MethodTree createHashCodeMethod(List<VariableTree> fields) {
    StringBuilder body = new StringBuilder(20 + fields.size() * 30);
    body.append("{"); // NOI18N
    body.append("int hash = 0;"); // NOI18N
    for (VariableTree field : fields) {
        body.append(createHashCodeLineForField(field));
    }
    body.append("return hash;"); // NOI18N
    body.append("}"); // NOI18N
    TreeMaker make = copy.getTreeMaker();
    // XXX Javadoc
    return make.Method(make.Modifiers(EnumSet.of(Modifier.PUBLIC),
            Collections.singletonList(genUtils.createAnnotation("java.lang.Override"))), "hashCode",
            make.PrimitiveType(TypeKind.INT), Collections.<TypeParameterTree>emptyList(),
            Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), body.toString(), null);
}
 
Example #13
Source File: AbstractTestGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a public static {@literal main(String[])} method
 * with the body taken from settings.
 *
 * @param  maker  {@literal TreeMaker} to use for creating the method
 * @return  created {@literal main(...)} method,
 *          or {@literal null} if the method body would be empty
 */
private MethodTree createMainMethod(TreeMaker maker) {
    String initialMainMethodBody = getInitialMainMethodBody();
    if (initialMainMethodBody.length() == 0) {
        return null;
    }

    ModifiersTree modifiers = maker.Modifiers(
            createModifierSet(Modifier.PUBLIC, Modifier.STATIC));
    VariableTree param = maker.Variable(
                    maker.Modifiers(Collections.<Modifier>emptySet()),
                    "argList",                                      //NOI18N
                    maker.Identifier("String[]"),                   //NOI18N
                    null);            //initializer - not used in params
    MethodTree mainMethod = maker.Method(
          modifiers,                            //public static
          "main",                               //method name "main"//NOI18N
          maker.PrimitiveType(TypeKind.VOID),   //return type "void"
          Collections.<TypeParameterTree>emptyList(),     //type params
          Collections.<VariableTree>singletonList(param), //method param
          Collections.<ExpressionTree>emptyList(),        //throws-list
          '{' + initialMainMethodBody + '}',    //body text
          null);                                //only for annotations

    return mainMethod;
}
 
Example #14
Source File: JavaFixUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Number visitMethod(MethodTree node, Void p) {
    String name = node.getName().toString();
    String newName = name;

    if (name.startsWith("$")) {
        if (parameterNames.containsKey(name)) {
            newName = parameterNames.get(name);
        }
    }

    List<? extends TypeParameterTree> typeParams = resolveMultiParameters(node.getTypeParameters());
    List<? extends VariableTree> params = resolveMultiParameters(node.getParameters());
    List<? extends ExpressionTree> thrown = resolveMultiParameters(node.getThrows());
    
    MethodTree nue = make.Method(node.getModifiers(), newName, node.getReturnType(), typeParams, params, thrown, node.getBody(), (ExpressionTree) node.getDefaultValue());
    
    rewrite(node, nue);
    
    return super.visitMethod(nue, p);
}
 
Example #15
Source File: JaxRsGenerationStrategy.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
MethodTree generateClose( TreeMaker maker, WorkingCopy copy ) {
    ModifiersTree methodModifier = maker.Modifiers(
            Collections.<Modifier>singleton(Modifier.PUBLIC));
    return maker.Method (
            methodModifier,
            "close", //NOI18N
            JavaSourceHelper.createTypeTree(copy, "void"), //NOI18N
            Collections.<TypeParameterTree>emptyList(),
            Collections.<VariableTree>emptyList(),
            Collections.<ExpressionTree>emptyList(),
            "{"+ //NOI18N
            "   client.close();"+ //NOI18N
            "}", //NOI18N
            null); 
}
 
Example #16
Source File: EqualsHashCodeGenerator.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static MethodTree createHashCodeMethod(WorkingCopy wc, Iterable<? extends VariableElement> hashCodeFields, Scope scope) {
    TreeMaker make = wc.getTreeMaker();
    Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC);        

    int startNumber = generatePrimeNumber(2, 10);
    int multiplyNumber = generatePrimeNumber(10, 100);
    List<StatementTree> statements = new ArrayList<>();
    //int hash = <startNumber>;
    statements.add(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "hash", make.PrimitiveType(TypeKind.INT), make.Literal(startNumber))); //NOI18N        
    for (VariableElement ve : hashCodeFields) {
        TypeMirror tm = ve.asType();
        ExpressionTree variableRead = prepareExpression(wc, HASH_CODE_PATTERNS, tm, ve, scope);
        statements.add(make.ExpressionStatement(make.Assignment(make.Identifier("hash"), make.Binary(Tree.Kind.PLUS, make.Binary(Tree.Kind.MULTIPLY, make.Literal(multiplyNumber), make.Identifier("hash")), variableRead)))); //NOI18N
    }
    statements.add(make.Return(make.Identifier("hash"))); //NOI18N        
    BlockTree body = make.Block(statements, false);
    ModifiersTree modifiers = prepareModifiers(wc, mods,make);
    
    return make.Method(modifiers, "hashCode", make.PrimitiveType(TypeKind.INT), Collections.<TypeParameterTree> emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), body, null); //NOI18N
}
 
Example #17
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new public property setter method.
 *
 * @param  modifiersTree the method modifiers; cannot be null.
 * @param  propertyType the property type; cannot be null.
 * @param  propertyName the property name; cannot be null.
 * @return the new method; never null.
 */
public MethodTree createPropertySetterMethod(ModifiersTree modifiersTree, String propertyName, Tree propertyType) {
    Parameters.notNull("modifiersTree", modifiersTree); // NOI18N
    Parameters.javaIdentifier("propertyName", propertyName); // NOI18N
    Parameters.notNull("propertyType", propertyType); // NOI18N

    TreeMaker make = getTreeMaker();
    return make.Method(
            modifiersTree,
            createPropertyAccessorName(propertyName, false),
            make.PrimitiveType(TypeKind.VOID),
            Collections.<TypeParameterTree>emptyList(),
            Collections.singletonList(createVariable(propertyName, propertyType)),
            Collections.<ExpressionTree>emptyList(),
            "{ this." + propertyName + " = " + propertyName + "; }", // NOI18N
            null);
}
 
Example #18
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a constructor which assigns its parameters to fields with the
 * same names. For example it can be used to generate:
 *
 * <pre>
 * public void Constructor(String field1, Object field2) {
 *     this.field1 = field1;
 *     this.field2 = field2;
 * }
 * </pre>
 *
 * @param  modifiersTree the constructor modifiers.
 * @param  constructorName the constructor name; cannot be null.
 * @param  parameters the constructor parameters; cannot be null.
 * @return the new constructor; never null.
 */
public MethodTree createAssignmentConstructor(ModifiersTree modifiersTree, String constructorName, List parameters) {
    Parameters.notNull("modifiersTree", modifiersTree);
    Parameters.javaIdentifier("constructorName", constructorName); // NOI18N
    Parameters.notNull("parameters", parameters); // NOI18N

    StringBuilder body = new StringBuilder(parameters.size() * 30);
    body.append("{"); // NOI18N
    for(int i=0; i < parameters.size();i++ ) {
        VariableTree parameter =  (VariableTree)parameters.get(i);
        String parameterName = parameter.getName().toString();
        body.append("this." + parameterName + " = " + parameterName + ";"); // NOI18N
    }
    body.append("}"); // NOI18N

    TreeMaker make = getTreeMaker();
    return make.Constructor(
            modifiersTree,
            Collections.<TypeParameterTree>emptyList(),
            parameters,
            Collections.<ExpressionTree>emptyList(),
            body.toString());
}
 
Example #19
Source File: JavaInputAstVisitor.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Does not omit the leading '<', which should be associated with the type name.
 */
private void typeParametersRest(
        List<? extends TypeParameterTree> typeParameters, Indent plusIndent) {
    builder.open(plusIndent);
    builder.breakOp();
    builder.open(ZERO);
    boolean first = true;
    for (TypeParameterTree typeParameter : typeParameters) {
        if (!first) {
            token(",");
            builder.breakOp(" ");
        }
        scan(typeParameter, null);
        first = false;
    }
    token(">");
    builder.close();
    builder.close();
}
 
Example #20
Source File: JaxRsGenerationStrategy.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
MethodTree generateOptionalFormMethod( TreeMaker maker, WorkingCopy copy ) {
    String mvMapClass = "javax.ws.rs.core.MultivaluedMap"; //NOI18N
    TypeElement mvMapEl = copy.getElements().getTypeElement(mvMapClass);
    String mvType = mvMapEl == null ? mvMapClass : "MultivaluedMap"; //NOI18N

    String body =
    "{"+ //NOI18N
        mvType+"<String,String> qParams = new javax.ws.rs.core.MultivaluedHashMap<String,String>();"+ //NOI18N
       "for (String qParam : optionalParams) {" + //NOI18N
        "    String[] qPar = qParam.split(\"=\");"+ //NOI18N
        "    if (qPar.length > 1) qParams.add(qPar[0], qPar[1])"+ //NOI18N
        "}"+ //NOI18N
        "return qParams;"+ //NOI18N
    "}"; //NOI18N
    ModifiersTree methodModifier = maker.Modifiers(Collections.<Modifier>singleton(Modifier.PRIVATE));
    ExpressionTree returnTree =
            mvMapEl == null ?
                copy.getTreeMaker().Identifier(mvMapClass) :
                copy.getTreeMaker().QualIdent(mvMapEl);
    ModifiersTree paramModifier = maker.Modifiers(Collections.<Modifier>emptySet());
    VariableTree param = maker.Variable(paramModifier, "optionalParams", maker.Identifier("String..."), null); //NOI18N
    return maker.Method (
            methodModifier,
            "getQParams", //NOI18N
            returnTree,
            Collections.<TypeParameterTree>emptyList(),
            Collections.<VariableTree>singletonList(param),
            Collections.<ExpressionTree>emptyList(),
            body,
            null); //NOI18N
}
 
Example #21
Source File: LabelsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public Void visitTypeParameter(TypeParameterTree node, Object p) {
    System.err.println("visitTypeParameter: " + node.getName());
    super.visitTypeParameter(node, p);
    TypeParameterTree copy = make.setLabel(node, node.getName() + "0");
    this.copy.rewrite(node, copy);
    return null;
}
 
Example #22
Source File: DispatchClientMethodGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void run( WorkingCopy copy ) throws Exception {
    copy.toPhase(Phase.RESOLVED);
    ClassTree javaClass = SourceUtils.getPublicTopLevelTree(copy);
    if (javaClass != null) {
        
        identifyConext(javaClass, copy);
        if ( isMethodBody() ){
            return;
        }

        TreeMaker maker = copy.getTreeMaker();
        Set<Modifier> methodModifs = new HashSet<Modifier>();
        methodModifs.add(Modifier.PRIVATE);
        Tree returnTypeTree = maker.PrimitiveType(TypeKind.VOID);  
        MethodTree methodTree = maker.Method (
                maker.Modifiers(methodModifs),
                suggestMethodName(javaClass),
                returnTypeTree,
                Collections.<TypeParameterTree>emptyList(),
                Collections.<VariableTree>emptyList(),
                Collections.<ExpressionTree>emptyList(),
                "{"+body+"}",
                null); //NOI18N

        ClassTree modifiedClass = maker.addClassMember(javaClass, methodTree);
        copy.rewrite(javaClass, modifiedClass);
    }        
}
 
Example #23
Source File: TreeNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitTypeParameter(TypeParameterTree tree, List<Node> d) {
    List<Node> below = new ArrayList<Node>();
    
    addCorrespondingElement(below);
    addCorrespondingType(below);
    addCorrespondingComments(below);
    
    super.visitTypeParameter(tree, below);
    
    d.add(new TreeNode(info, getCurrentPath(), below));
    return null;
}
 
Example #24
Source File: SAXGeneratorSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Stub's method. It is SAX 2.0 method
 */
private MethodTree genStartPrefixMappingMethod(TreeMaker make)  {
    MethodTree method = null;
    if (sax == 2) {
        //first create the body
         StringBuffer code = new StringBuffer("{\n");
         if (model.isPropagateSAX())
            code.append ("handler." + M_START_PREFIX_MAPPING + "(prefix, uri);\n"); // NOI18N
         code.append("}\n");
         
         //method params
         Tree stree = make.Identifier("java.lang.String");
         VariableTree par1 = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "prefix", stree, null);
         VariableTree par2 = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "uri", stree, null);
         List parList = new ArrayList(2);
         parList.add(par1);
         parList.add(par2);
         ModifiersTree mods = make.Modifiers(EnumSet.of(Modifier.PUBLIC,Modifier.FINAL));
    
          method = make.Method(
                        mods,
                        M_START_PREFIX_MAPPING,
                        make.PrimitiveType(TypeKind.VOID),
                        Collections.<TypeParameterTree>emptyList(),
                        parList,
                        Collections.singletonList(make.Identifier(SAX_EXCEPTION)),
                        code.toString(),
                        null
                    );
    }

    return method;
}
 
Example #25
Source File: SAXGeneratorSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Stub's method. It is SAX 2.0 method
 */
private MethodTree genSkippedEntityMethod(TreeMaker make)   {
    MethodTree method = null;
    if (sax == 2) {
        //first create the body
         StringBuffer code = new StringBuffer("{\n");
         if (model.isPropagateSAX())
            code.append("\nhandler." + M_SKIPPED_ENTITY + "(name);\n"); // NOI18N
         code.append("}\n");
         
         //method params
         Tree stree = make.Identifier("java.lang.String");
         VariableTree par1 = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "name", stree, null);
         List parList = new ArrayList();
         parList.add(par1);
         ModifiersTree mods = make.Modifiers(EnumSet.of(Modifier.PUBLIC,Modifier.FINAL));
    
          method = make.Method(
                        mods,
                        M_SKIPPED_ENTITY,
                        make.PrimitiveType(TypeKind.VOID),
                        Collections.<TypeParameterTree>emptyList(),
                        parList,
                        Collections.singletonList(make.Identifier(SAX_EXCEPTION)),
                        code.toString(),
                        null
                    );
    
    }
    return method;
}
 
Example #26
Source File: SAXGeneratorSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Stub's method. It is SAX 2.0 method
 */
private MethodTree genEndPrefixMappingMethod(TreeMaker make)   {
    MethodTree method = null;
    if (sax == 2) {
        //first create the body
         StringBuffer code = new StringBuffer("{\n");
         if (model.isPropagateSAX())
            code.append ("\nhandler." + M_END_PREFIX_MAPPING + "(prefix);\n"); // NOI18N
         code.append("}\n");
         
         //method params
         Tree stree = make.Identifier("java.lang.String");
         VariableTree par1 = make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), "prefix", stree, null);
         List parList = new ArrayList();
         parList.add(par1);
         ModifiersTree mods = make.Modifiers(EnumSet.of(Modifier.PUBLIC,Modifier.FINAL));
    
          method = make.Method(
                        mods,
                        M_END_PREFIX_MAPPING,
                        make.PrimitiveType(TypeKind.VOID),
                        Collections.<TypeParameterTree>emptyList(),
                        parList,
                        Collections.singletonList(make.Identifier(SAX_EXCEPTION)),
                        code.toString(),
                        null
                    );
    
    }
    return method;
}
 
Example #27
Source File: JavaSourceUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static MethodTree getMarshalMethod(TreeMaker make, String arg1, String arg2) throws IOException {        
    MethodTree method = make.Method(
            make.Modifiers(Collections.<Modifier>singleton(Modifier.PRIVATE)),
            // XXX TODO get unique method name.
            "marshal", //NO I18N
            make.PrimitiveType(TypeKind.BOOLEAN),
            Collections.<TypeParameterTree>emptyList(),
            Collections.<VariableTree>emptyList(),
            Collections.<ExpressionTree>emptyList(),
            getMarshalMethodBody(arg1, arg2),
            null);
    return method;
}
 
Example #28
Source File: IntroduceMethodFix.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private MethodTree createMethodDefinition(boolean mustStatic) {
    // if all the statements are contained within a Block, get just the block, it will be processed recursively
    List<VariableTree> formalArguments = IntroduceHint.createVariables(copy, parameters, pathToClass, 
            statementPaths.subList(from, to + 1));
    if (formalArguments == null) {
        return null; //XXX
    }
    List<ExpressionTree> thrown = IntroduceHint.typeHandleToTree(copy, thrownTypes);
    if (thrown == null) {
        return null; //XXX
    }
    List<TypeParameterTree> typeVars = new LinkedList<TypeParameterTree>();
    for (TreePathHandle tph : IntroduceMethodFix.this.typeVars) {
        typeVars.add((TypeParameterTree) tph.resolve(copy).getLeaf());
    }
    List<StatementTree> methodStatements = new ArrayList<StatementTree>();
    generateMethodContents(methodStatements);
    makeReturnsFromExtractedMethod(methodStatements);
    
    Set<Modifier> modifiers = EnumSet.noneOf(Modifier.class);
    modifiers.addAll(access);
    
    if (target.iface) {
        modifiers.add(Modifier.DEFAULT);
    } else if (mustStatic) {
        modifiers.add(Modifier.STATIC);
    }
    ModifiersTree mods = make.Modifiers(modifiers);
    MethodTree method = make.Method(mods, name, returnTypeTree, typeVars, formalArguments, thrown, make.Block(methodStatements, false), null);
    
    return method;
}
 
Example #29
Source File: ClientJavaSourceHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static ClassTree addJerseyClientClass (
        WorkingCopy copy,
        ClassTree classTree,
        String className,
        String resourceURI,
        RestServiceDescription restServiceDesc,
        WadlSaasResource saasResource,
        PathFormat pf,
        Security security, ClientGenerationStrategy strategy ) {

    TreeMaker maker = copy.getTreeMaker();
    ModifiersTree modifs = maker.Modifiers(Collections.<Modifier>singleton(Modifier.STATIC));
    ClassTree innerClass = maker.Class(
            modifs,
            className,
            Collections.<TypeParameterTree>emptyList(),
            null,
            Collections.<Tree>emptyList(),
            Collections.<Tree>emptyList());

    ClassTree modifiedInnerClass =
            generateClassArtifacts(copy, innerClass, resourceURI, 
                    restServiceDesc, saasResource, pf, security, 
                    classTree.getSimpleName().toString(), strategy );

    return maker.addClassMember(classTree, modifiedInnerClass);
}
 
Example #30
Source File: MultipleRewritesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testRewriteRewrittenTree() throws Exception {
    testFile = new File(getWorkDir(), "Test.java");
    TestUtilities.copyStringToFile(testFile,
            "package hierbas.del.litoral;\n\n" +
            "public class Test {\n" +
            "}\n"
            );
    String golden =
            "package hierbas.del.litoral;\n\n" +
            "public class Test {\n" +
            "\n" +
            "    @Deprecated\n" +
            "    public void taragui() {\n" +
            "    }\n" +
            "}\n";
    JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile));
    Task<WorkingCopy> task = new Task<WorkingCopy>() {
        
        public void run(WorkingCopy workingCopy) throws java.io.IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            TreeMaker make = workingCopy.getTreeMaker();
            
            ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0);
            ModifiersTree mods = make.Modifiers(EnumSet.of(Modifier.PUBLIC));
            MethodTree method = make.Method(mods, "taragui", make.Type(workingCopy.getTypes().getNoType(TypeKind.VOID)), Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), "{}", null);
            
            workingCopy.rewrite(clazz, make.addClassMember(clazz, method));
            
            ModifiersTree nueMods = make.Modifiers(EnumSet.of(Modifier.PUBLIC), Collections.singletonList(make.Annotation(make.QualIdent(workingCopy.getElements().getTypeElement("java.lang.Deprecated")), Collections.<ExpressionTree>emptyList())));
            
            workingCopy.rewrite(mods, nueMods);
        }
        
    };
    testSource.runModificationTask(task).commit();
    String res = TestUtilities.copyFileToString(testFile);
    //System.err.println(res);
    assertEquals(golden, res);
}