Java Code Examples for org.codehaus.groovy.ast.PropertyNode#getDeclaringClass()

The following examples show how to use org.codehaus.groovy.ast.PropertyNode#getDeclaringClass() . 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: ClassCompletionVerifier.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void checkDuplicateProperties(PropertyNode node) {
    ClassNode cn = node.getDeclaringClass();
    String name = node.getName();
    String getterName = "get" + capitalize(name);
    if (Character.isUpperCase(name.charAt(0))) {
        for (PropertyNode propNode : cn.getProperties()) {
            String otherName = propNode.getField().getName();
            String otherGetterName = "get" + capitalize(otherName);
            if (node != propNode && getterName.equals(otherGetterName)) {
                String msg = "The field " + name + " and " + otherName + " on the class " +
                        cn.getName() + " will result in duplicate JavaBean properties, which is not allowed";
                addError(msg, node);
            }
        }
    }
}
 
Example 2
Source File: DelegateASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void addGetterIfNeeded(DelegateDescription delegate, PropertyNode prop, String name, boolean allNames) {
    boolean isPrimBool = prop.getOriginType().equals(ClassHelper.boolean_TYPE);
    // do a little bit of pre-work since Groovy compiler hasn't added property accessors yet
    boolean willHaveGetAccessor = true;
    boolean willHaveIsAccessor = isPrimBool;
    String suffix = Verifier.capitalize(name);
    if (isPrimBool) {
        ClassNode cNode = prop.getDeclaringClass();
        if (cNode.getGetterMethod("is" + suffix) != null && cNode.getGetterMethod("get" + suffix) == null)
            willHaveGetAccessor = false;
        if (cNode.getGetterMethod("get" + suffix) != null && cNode.getGetterMethod("is" + suffix) == null)
            willHaveIsAccessor = false;
    }
    Reference<Boolean> ownerWillHaveGetAccessor = new Reference<Boolean>();
    Reference<Boolean> ownerWillHaveIsAccessor = new Reference<Boolean>();
    extractAccessorInfo(delegate.owner, name, ownerWillHaveGetAccessor, ownerWillHaveIsAccessor);

    for (String prefix : new String[]{"get", "is"}) {
        String getterName = prefix + suffix;
        if ((prefix.equals("get") && willHaveGetAccessor && !ownerWillHaveGetAccessor.get()
                || prefix.equals("is") && willHaveIsAccessor && !ownerWillHaveIsAccessor.get())
                && !shouldSkipPropertyMethod(name, getterName, delegate.excludes, delegate.includes, allNames)) {
            addGeneratedMethod(delegate.owner, getterName,
                    ACC_PUBLIC,
                    GenericsUtils.nonGeneric(prop.getType()),
                    Parameter.EMPTY_ARRAY,
                    null,
                    returnS(propX(delegate.getOp, name)));
        }
    }
}
 
Example 3
Source File: GeneralUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static BooleanExpression hasSamePropertyX(final PropertyNode pNode, final Expression other) {
    ClassNode cNode = pNode.getDeclaringClass();
    return sameX(getterThisX(cNode, pNode), getterX(cNode, other, pNode));
}
 
Example 4
Source File: GeneralUtils.java    From groovy with Apache License 2.0 3 votes vote down vote up
/**
 * This method is similar to {@link #propX(Expression, Expression)} but will make sure that if the property
 * being accessed is defined inside the classnode provided as a parameter, then a getter call is generated
 * instead of a field access.
 * @param annotatedNode the class node where the property node is accessed from
 * @param pNode the property being accessed
 * @return a method call expression or a property expression
 */
public static Expression getterThisX(final ClassNode annotatedNode, final PropertyNode pNode) {
    ClassNode owner = pNode.getDeclaringClass();
    if (annotatedNode.equals(owner)) {
        return callThisX(getterName(annotatedNode, pNode));
    }
    return propX(varX("this"), pNode.getName());
}
 
Example 5
Source File: GeneralUtils.java    From groovy with Apache License 2.0 3 votes vote down vote up
/**
 * This method is similar to {@link #propX(Expression, Expression)} but will make sure that if the property
 * being accessed is defined inside the classnode provided as a parameter, then a getter call is generated
 * instead of a field access.
 * @param annotatedNode the class node where the property node is accessed from
 * @param receiver the object having the property
 * @param pNode the property being accessed
 * @return a method call expression or a property expression
 */
public static Expression getterX(final ClassNode annotatedNode, final Expression receiver, final PropertyNode pNode) {
    ClassNode owner = pNode.getDeclaringClass();
    if (annotatedNode.equals(owner)) {
        return callX(receiver, getterName(annotatedNode, pNode));
    }
    return propX(receiver, pNode.getName());
}