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

The following examples show how to use org.openide.util.Parameters#javaIdentifierOrNull() . 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 5 votes vote down vote up
/**
 * Creates a new annotation argument whose value is a literal.
 *
 * @param  argumentName the argument name; cannot be null.
 * @param  argumentValue the argument value; cannot be null. The semantics
 *         of this parameter is the same as of the parameters of
 *         {@link TreeMaker#Literal(Object)}.
 * @return the new annotation argument; never null.
 */
public ExpressionTree createAnnotationArgument(String argumentName, Object argumentValue) {
    Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N
    Parameters.notNull("argumentValue", argumentValue); // NOI18N

    TreeMaker make = getTreeMaker();
    ExpressionTree argumentValueTree = make.Literal(argumentValue);
    if (argumentName == null) {
        return argumentValueTree;
    } else {
        return make.Assignment(make.Identifier(argumentName), argumentValueTree);
    }
}
 
Example 2
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 an array.
 *
 * @param argumentName the argument name; cannot be null.
 * @param argumentValues the argument values to initialize the array with; cannot be null.
 * @return the new annotation argument; never null.
 */
public ExpressionTree createAnnotationArgument(String argumentName, List<? extends ExpressionTree> argumentValues) {
    Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N
    Parameters.notNull("argumentValues", argumentValues); // NOI18N

    TreeMaker make = getTreeMaker();
    ExpressionTree argumentValuesTree = make.NewArray(null, Collections.<ExpressionTree>emptyList(), argumentValues);
    if (argumentName == null) {
        return argumentValuesTree;
    } else {
        return make.Assignment(make.Identifier(argumentName), argumentValuesTree);
    }
}
 
Example 3
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 4
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void removeAnnotationArgument(WorkingCopy workingCopy, 
        Element element, Element annotationElement, 
        String argumentName) throws IOException 
{
    Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N
    ModifiersTree oldTree = null;
    if (element instanceof TypeElement) {
        oldTree = workingCopy.getTrees().getTree((TypeElement) element).
            getModifiers();
    } else if (element instanceof ExecutableElement) {
        oldTree = workingCopy.getTrees().getTree((ExecutableElement) element).
            getModifiers();
    } else if (element instanceof VariableElement) {
        oldTree = ((VariableTree) workingCopy.getTrees().getTree(element)).
            getModifiers();
    }
    if (oldTree == null) {
        return;
    }
    
    AnnotationMirror annMirror = null;
    for( AnnotationMirror annotationMirror : element.getAnnotationMirrors() ){
        if ( annotationElement.equals(annotationMirror.getAnnotationType().
                asElement()))
        {
            annMirror = annotationMirror;
        }
    }
    if ( annMirror == null ){
        return;
    }
    
    AnnotationTree annotation = (AnnotationTree) workingCopy.getTrees().
        getTree(element, annMirror);
    TreeMaker make = workingCopy.getTreeMaker();
    ExpressionTree e = getAnnotationArgumentTree(annotation, argumentName);
    AnnotationTree modifiedAnnotation = make.removeAnnotationAttrValue(annotation, e);
    workingCopy.rewrite(annotation, modifiedAnnotation);
}
 
Example 5
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 literal.
 *
 * @param  argumentName the argument name; cannot be null.
 * @param  argumentValue the argument value; cannot be null. The semantics
 *         of this parameter is the same as of the parameters of
 *         {@link TreeMaker#Literal(Object)}.
 * @return the new annotation argument; never null.
 */
public ExpressionTree createAnnotationArgument(String argumentName, Object argumentValue) {
    Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N
    Parameters.notNull("argumentValue", argumentValue); // NOI18N

    TreeMaker make = getTreeMaker();
    ExpressionTree argumentValueTree = make.Literal(argumentValue);
    if (argumentName == null) {
        return argumentValueTree;
    } else {
        return make.Assignment(make.Identifier(argumentName), argumentValueTree);
    }
}
 
Example 6
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 an array.
 *
 * @param argumentName the argument name; cannot be null.
 * @param argumentValues the argument values to initialize the array with; cannot be null.
 * @return the new annotation argument; never null.
 */
public ExpressionTree createAnnotationArgument(String argumentName, List<? extends ExpressionTree> argumentValues) {
    Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N
    Parameters.notNull("argumentValues", argumentValues); // NOI18N

    TreeMaker make = getTreeMaker();
    ExpressionTree argumentValuesTree = make.NewArray(null, Collections.<ExpressionTree>emptyList(), argumentValues);
    if (argumentName == null) {
        return argumentValuesTree;
    } else {
        return make.Assignment(make.Identifier(argumentName), argumentValuesTree);
    }
}
 
Example 7
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 8
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 literal.
 *
 * @param  argumentName the argument name; cannot be null.
 * @param  argumentValue the argument value; cannot be null. The semantics
 *         of this parameter is the same as of the parameters of
 *         {@link TreeMaker#Literal(Object)}.
 * @return the new annotation argument; never null.
 */
public ExpressionTree createAnnotationArgument(String argumentName, Object argumentValue) {
    Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N
    Parameters.notNull("argumentValue", argumentValue); // NOI18N

    TreeMaker make = getTreeMaker();
    ExpressionTree argumentValueTree = make.Literal(argumentValue);
    if (argumentName == null) {
        return argumentValueTree;
    } else {
        return make.Assignment(make.Identifier(argumentName), argumentValueTree);
    }
}
 
Example 9
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 an array.
 *
 * @param argumentName the argument name; cannot be null.
 * @param argumentValue the argument value; cannot be null.
 * @return the new annotation argument; never null.
 */
public ExpressionTree createAnnotationArgument(String argumentName, List<? extends ExpressionTree> argumentValues) {
    Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N
    Parameters.notNull("argumentValues", argumentValues); // NOI18N

    TreeMaker make = getTreeMaker();
    ExpressionTree argumentValuesTree = make.NewArray(null, Collections.<ExpressionTree>emptyList(), argumentValues);
    if (argumentName == null) {
        return argumentValuesTree;
    } else {
        return make.Assignment(make.Identifier(argumentName), argumentValuesTree);
    }
}
 
Example 10
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 11
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static void addAnnotationArgument(WorkingCopy workingCopy, 
        Element element, Element annotationElement, 
        String argumentName, Object argumentValue) throws IOException 
{
    Parameters.javaIdentifierOrNull("argumentName", argumentName); // NOI18N
    Parameters.notNull("argumentValue", argumentValue); // NOI18N
    ModifiersTree oldTree = null;
    if (element instanceof TypeElement) {
        oldTree = workingCopy.getTrees().getTree((TypeElement) element).
            getModifiers();
    } else if (element instanceof ExecutableElement) {
        oldTree = workingCopy.getTrees().getTree((ExecutableElement) element).
            getModifiers();
    } else if (element instanceof VariableElement) {
        oldTree = ((VariableTree) workingCopy.getTrees().getTree(element)).
            getModifiers();
    }
    if (oldTree == null) {
        return;
    }
    
    AnnotationMirror annMirror = null;
    for( AnnotationMirror annotationMirror : element.getAnnotationMirrors() ){
        if ( annotationElement.equals(annotationMirror.getAnnotationType().
                asElement()))
        {
            annMirror = annotationMirror;
        }
    }
    if ( annMirror == null ){
        return;
    }
    
    AnnotationTree annotation = (AnnotationTree) workingCopy.getTrees().
        getTree(element, annMirror);
    TreeMaker make = workingCopy.getTreeMaker();
    ExpressionTree oldArgTree = getAnnotationArgumentTree(annotation, argumentName);
    if(oldArgTree!=null)
        annotation = make.removeAnnotationAttrValue(annotation, oldArgTree);
    ExpressionTree argumentValueTree = null;
    if(argumentValue instanceof Enum) {
        argumentValueTree =  make.MemberSelect(make.QualIdent(
                argumentValue.getClass().getCanonicalName()),
                ((Enum)argumentValue).name());
    } else {
        try {
        argumentValueTree = make.Literal(argumentValue);
        } catch (IllegalArgumentException iae) {
            // dont do anything for now
            return ;
        }
    }
    if (argumentName != null) {
        argumentValueTree =  make.Assignment(make.Identifier(argumentName), 
                argumentValueTree);
    }
    AnnotationTree modifiedAnnotation = make.addAnnotationAttrValue(annotation, 
            argumentValueTree);
    workingCopy.rewrite(annotation, modifiedAnnotation);
}