Java Code Examples for org.openide.util.Parameters#javaIdentifier()

The following examples show how to use org.openide.util.Parameters#javaIdentifier() . 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: 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 2
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new public property getter 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 createPropertyGetterMethod(ModifiersTree modifiersTree, String propertyName, Tree propertyType) {
    Parameters.notNull("modifiersTree", modifiersTree); // NOI18N
    Parameters.javaIdentifier("propertyName", propertyName); // NOI18N
    Parameters.notNull("propertyType", propertyType); // NOI18N

    return getTreeMaker().Method(
            modifiersTree,
            createPropertyAccessorName(propertyName, true),
            propertyType,
            Collections.<TypeParameterTree>emptyList(),
            Collections.<VariableTree>emptyList(),
            Collections.<ExpressionTree>emptyList(),
            "{ return " + propertyName + "; }", // NOI18N
            null);
}
 
Example 3
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, it's not used inside except for assertion since 2007 (or before, TODO: remove?)
 * @param  parameters the constructor parameters; cannot be null.
 * @return the new constructor; never null.
 */
public MethodTree createAssignmentConstructor(ModifiersTree modifiersTree, String constructorName, List<VariableTree> 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 (VariableTree parameter : parameters) {
        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 4
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new public property getter 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 createPropertyGetterMethod(ModifiersTree modifiersTree, String propertyName, Tree propertyType) throws IOException {
    Parameters.notNull( MODIFIERS_TREE,modifiersTree); // NOI18N
    Parameters.javaIdentifier( PROPERTY_NAME,propertyName); // NOI18N
    Parameters.notNull( PROPERTY_TYPE,propertyType); // NOI18N
    getWorkingCopy().toPhase(Phase.RESOLVED);

    return getTreeMaker().Method(
            modifiersTree,
            createPropertyAccessorName(propertyName, true),
            propertyType,
            Collections.<TypeParameterTree>emptyList(),
            Collections.<VariableTree>emptyList(),
            Collections.<ExpressionTree>emptyList(),
            "{ return " + propertyName + "; }", // NOI18N
            null);
}
 
Example 5
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new Java class based on the provided template.
 *
 * @param  template the template to base the new class on.
 * @param  targetFolder the folder the new class should be created in;
 *         cannot be null.
 * @param  className the name of the new class (a valid Java identifier);
 *         cannot be null.
 * @param  javadoc the new class's Javadoc; can be null.
 * @param  parameters map of named objects that are going to be used when creating the new object
 * @return the FileObject for the new Java class; never null.
 * @throws IOException if an error occurred while creating the class.
 */
public static FileObject createClass(String template, FileObject targetFolder, String className, final String javadoc, Map parameters) throws IOException {
    Parameters.notNull("template", template); // NOI18N
    Parameters.notNull("targetFolder", targetFolder); // NOI18N
    Parameters.javaIdentifier("className", className); // NOI18N

    FileObject classFO = createDataObjectFromTemplate(template, targetFolder, className, parameters).getPrimaryFile();
    // JavaSource javaSource = JavaSource.forFileObject(classFO);
    // final boolean[] commit = { false };
    // ModificationResult modification = javaSource.runModificationTask(new AbstractTask<WorkingCopy>() {
    //     public void run(WorkingCopy copy) throws IOException {
    //         GenerationUtils genUtils = GenerationUtils.newInstance(copy);
    //         if (javadoc != null) {
    //             genUtils.setJavadoc(copy, mainType, javadoc);
    //         }
    //     }
    // });
    // if (commit[0]) {
    //     modification.commit();
    // }

    return classFO;
}
 
Example 6
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new public property getter method.
 *
 * @param  modifiersTree the method modifiers; cannot be null.
 * @param  propertyType the fully-qualified name of the property type; cannot be null.
 * @param  propertyName the property name; cannot be null.
 * @return the new method; never null.
 */
public MethodTree createPropertyGetterMethod(ModifiersTree modifiersTree, String propertyName, String propertyType) throws IOException {
    Parameters.notNull( MODIFIERS_TREE,modifiersTree); // NOI18N
    Parameters.javaIdentifier( PROPERTY_NAME,propertyName); // NOI18N
    Parameters.notNull( PROPERTY_TYPE,propertyType); // NOI18N
    getWorkingCopy().toPhase(Phase.RESOLVED);

    return createPropertyGetterMethod(modifiersTree, propertyName, createType(propertyType));
}
 
Example 7
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new variable (a <code>VariableTree</code> with no
 * modifiers nor initializer).
 *
 * @param  variableType the variable type; cannot be null.
 * @param  variableName the variable name; cannot be null.
 * @return the new variable; never null.
 */
public VariableTree createVariable(String variableName, Tree variableType) {
    Parameters.javaIdentifier("variableName", variableName); // NOI18N
    Parameters.notNull("variableType", variableType); // NOI18N

    return getTreeMaker().Variable(
            createEmptyModifiers(),
            variableName,
            variableType,
            null);
}
 
Example 8
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new variable (a <code>VariableTree</code> with no
 * modifiers nor initializer).
 *
 * @param  variableType the fully-qualified name of the variable type; cannot be null.
 * @param  variableName the variable name; cannot be null.
 * @return the new variable; never null.
 */
public VariableTree createVariable(String variableName, String variableType) {
    Parameters.javaIdentifier("variableName", variableName); // NOI18N
    Parameters.notNull("variableType", variableType); // NOI18N

    return createField(
            createEmptyModifiers(),
            variableName,
            variableType);
}
 
Example 9
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new variable (a <code>VariableTree</code> with no
 * modifiers nor initializer).
 *
 * @param  variableType the variable type; cannot be null.
 * @param  variableName the variable name; cannot be null.
 * @return the new variable; never null.
 */
public VariableTree createVariable(String variableName, Tree variableType) {
    Parameters.javaIdentifier("variableName", variableName); // NOI18N
    Parameters.notNull("variableType", variableType); // NOI18N

    return getTreeMaker().Variable(
            createEmptyModifiers(),
            variableName,
            variableType,
            null);
}
 
Example 10
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new field.
 *
 * @param  scope the scope in which to create the field (will be e.g. used
 *         to parse <code>fieldType</code>).
 * @param  modifiersTree the field modifiers; cannot be null.
 * @param  fieldType the fully-qualified name of the field type; cannot be null.
 * @param  fieldName the field name; cannot be null.
 * @param  expressionTree expression to initialize the field; can be null.
 * @return the new field; never null.
 */
public VariableTree createField(TypeElement scope, ModifiersTree modifiersTree, String fieldName, String fieldType, ExpressionTree expressionTree) {
    Parameters.notNull("modifiersTree", modifiersTree); // NOI18N
    Parameters.javaIdentifier("fieldName", fieldName); // NOI18N
    Parameters.notNull("fieldType", fieldType); // NOI18N

    return getTreeMaker().Variable(
            modifiersTree,
            fieldName,
            createType(fieldType, scope),
            expressionTree);
}
 
Example 11
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new variable (a <code>VariableTree</code> with no
 * modifiers nor initializer).
 *
 * @param  scope the scope in which to create the variable (will be e.g. used
 *         to parse <code>variableType</code>).
 * @param  variableType the fully-qualified name of the variable type; cannot be null.
 * @param  variableName the variable name; cannot be null.
 * @return the new variable; never null.
 */
public VariableTree createVariable(TypeElement scope, String variableName, String variableType) {
    Parameters.javaIdentifier("variableName", variableName); // NOI18N
    Parameters.notNull("variableType", variableType); // NOI18N

    return createField(
            scope,
            createEmptyModifiers(),
            variableName,
            variableType,
            null);
}
 
Example 12
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new field.
 *
 * @param  modifiersTree the field modifiers; cannot be null.
 * @param  fieldType the fully-qualified name of the field type; cannot be null.
 * @param  fieldName the field name; cannot be null.
 * @return the new field; never null.
 */
public VariableTree createField(ModifiersTree modifiersTree, String fieldName, String fieldType) {
    Parameters.notNull( MODIFIERS_TREE,modifiersTree); // NOI18N
    Parameters.javaIdentifier("fieldName", fieldName); // NOI18N
    Parameters.notNull("fieldType", fieldType); // NOI18N

    return getTreeMaker().Variable(
            modifiersTree,
            fieldName,
            createType(fieldType),
            null);
}
 
Example 13
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new public property setter method.
 *
 * @param  propertyType the fully-qualified name of 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, String propertyType) throws IOException {
    Parameters.notNull( MODIFIERS_TREE,modifiersTree); // NOI18N
    Parameters.javaIdentifier( PROPERTY_NAME,propertyName); // NOI18N
    Parameters.notNull( PROPERTY_TYPE,propertyType); // NOI18N
    getWorkingCopy().toPhase(Phase.RESOLVED);

    return createPropertySetterMethod(modifiersTree, propertyName, createType(propertyType));
}
 
Example 14
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new annotation argument whose value is a member of a type. For
 * example it can be used to generate <code>@Target(ElementType.CONSTRUCTOR)</code>.
 *
 * @param  argumentName the argument name; cannot be null.
 * @param  argumentType the fully-qualified name of the type whose member is to be invoked
 *         (e.g. <code>java.lang.annotations.ElementType</code> in the previous
 *         example); cannot be null.
 * @param  argumentTypeField a field of <code>argumentType</code>
 *         (e.g. <code>CONSTRUCTOR</code> in the previous example);
 *         cannot be null.
 * @return the new annotation argument; never null.
 */
public ExpressionTree createAnnotationArgument(String argumentName, String argumentType, String argumentTypeField) {
    Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N
    Parameters.notNull("argumentType", argumentType); // NOI18N
    Parameters.javaIdentifier("argumentTypeField", argumentTypeField); // NOI18N

    TreeMaker make = getTreeMaker();
    MemberSelectTree argumentValueTree = make.MemberSelect(createQualIdent(argumentType), argumentTypeField);
    if (argumentName == null) {
        return argumentValueTree;
    } else {
        return make.Assignment(make.Identifier(argumentName), argumentValueTree);
    }
}
 
Example 15
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new variable (a <code>VariableTree</code> with no
 * modifiers nor initializer).
 *
 * @param  scope the scope in which to create the variable (will be e.g. used
 *         to parse <code>variableType</code>).
 * @param  variableType the fully-qualified name of the variable type; cannot be null.
 * @param  variableName the variable name; cannot be null.
 * @return the new variable; never null.
 */
public VariableTree createVariable(TypeElement scope, String variableName, String variableType) {
    Parameters.javaIdentifier("variableName", variableName); // NOI18N
    Parameters.notNull("variableType", variableType); // NOI18N

    return createField(
            scope,
            createEmptyModifiers(),
            variableName,
            variableType,
            null);
}
 
Example 16
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new Java class based on the provided template.
 *
 * @param  targetFolder the folder the new class should be created in;
 *         cannot be null.
 * @param  targetName the name of the new interface (a valid Java identifier);
 *         cannot be null.
 * @param  parameters map of named objects that are going to be used when creating the new object
 * @return the FileObject for the new Java class; never null.
 */
public static FileObject createClass(String template, FileObject targetFolder, String className, final String javadoc, 
        Map<String,? extends Object> parameters) throws IOException {
    Parameters.notNull("template", template); // NOI18N
    Parameters.notNull("targetFolder", targetFolder); // NOI18N
    Parameters.javaIdentifier("className", className); // NOI18N

    FileObject classFO = createDataObjectFromTemplate(template, targetFolder, className, parameters).getPrimaryFile();
    return classFO;
}
 
Example 17
Source File: GenerationUtils.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new public property setter method.
 *
 * @param  scope the scope in which to create the method (will be e.g. used
 *         to parse <code>propertyType</code>).
 * @param  modifiersTree the method modifiers; cannot be null.
 * @param  propertyName the property name; cannot be null.
 * @param  propertyType the fully-qualified name of the property type; cannot be null.
 * @return the new method; never null.
 */
public MethodTree createPropertySetterMethod(TypeElement scope, ModifiersTree modifiersTree, String propertyName, String propertyType) {
    Parameters.notNull("modifiersTree", modifiersTree); // NOI18N
    Parameters.javaIdentifier("propertyName", propertyName); // NOI18N
    Parameters.notNull("propertyType", propertyType); // NOI18N

    return createPropertySetterMethod(modifiersTree, propertyName, createType(propertyType, scope));
}
 
Example 18
Source File: MethodModel.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/**
 * Creates new instance of method model. None of the parameters can be null.
 * 
 * @param name name of the method, must be valid Java identifier
 * @param returnType name of return type as written in source code,
 * for non-primitive types fully-qualfied name must be used,
 * must contain at least one non-whitespace character
 * @param body string representation of body, can be null
 * @param parameters list of method parameters, can be empty
 * @param exceptions list of exceptions represented by fully-qualified names of exceptions, can be empty
 * @param modifiers list of modifiers of method, can be empty
 * @throws NullPointerException if any of the parameters is <code>null</code>.
 * @throws IllegalArgumentException if the paramter returnType does not contain at least one non-whitespace character
 * or the parameter name is not a valid Java identifier
 * @return immutable model of method
 */
public static MethodModel create(String name, String returnType, String body, List<Variable> parameters, List<String> exceptions, Set<Modifier> modifiers) {
    Parameters.javaIdentifier("name", name);
    Parameters.notWhitespace("returnType", returnType);
    Parameters.notNull("parameters", parameters);
    Parameters.notNull("exceptions", exceptions);
    Parameters.notNull("modifiers", modifiers);
    return new MethodModel(name, returnType, body, parameters, exceptions, modifiers, Collections.<Annotation>emptyList());
}
 
Example 19
Source File: MethodModel.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/**
 * Creates new instance of method model. None of the parameters can be null.
 *
 * @param name name of the method, must be valid Java identifier
 * @param returnType name of return type as written in source code,
 * for non-primitive types fully-qualfied name must be used,
 * must contain at least one non-whitespace character
 * @param body string representation of body, can be null
 * @param parameters list of method parameters, can be empty
 * @param exceptions list of exceptions represented by fully-qualified names of exceptions, can be empty
 * @param modifiers list of modifiers of method, can be empty
 * @param annotations list of {@code Annotations} represented by fully-qualified names of annotations, can be empty
 * @throws NullPointerException if any of the parameters is <code>null</code>.
 * @throws IllegalArgumentException if the paramter returnType does not contain at least one non-whitespace character
 * or the parameter name is not a valid Java identifier
 * @return immutable model of method
 */
public static MethodModel create(String name, String returnType, String body, List<Variable> parameters, List<String> exceptions, Set<Modifier> modifiers,  List<Annotation> annotations) {
    Parameters.javaIdentifier("name", name);
    Parameters.notWhitespace("returnType", returnType);
    Parameters.notNull("parameters", parameters);
    Parameters.notNull("exceptions", exceptions);
    Parameters.notNull("modifiers", modifiers);
    Parameters.notNull("annotations", annotations);
    return new MethodModel(name, returnType, body, parameters, exceptions, modifiers, annotations);
}
 
Example 20
Source File: MethodModel.java    From netbeans with Apache License 2.0 2 votes vote down vote up
/**
 * Creates new instance of a model of class variable or method parameter
 * without final modifier. Same as calling {@link #create(String, String, boolean)}
 * with 3rd argument set to false
 * 
 * @param type name of type as written in source code
 * for non-primitive types fully-qualfied name must be used,
 * must contain at least one non-whitespace character
 * @param name name of the paramter or variable, must be valid Java identifier
 * @throws NullPointerException if any of the parameters is <code>null</code>.
 * @throws IllegalArgumentException if the paramter type does not contain at least one non-whitespace character
 * or the parameter name is not a valid Java identifier
 * @return immutable model of variable or method parameter
 */
public static Variable create(String type, String name) {
    Parameters.notWhitespace("type", type);
    Parameters.javaIdentifier("name", name);
    return new MethodModel.Variable(type, name, false);
}