Java Code Examples for org.codehaus.groovy.ast.FieldNode#isPublic()

The following examples show how to use org.codehaus.groovy.ast.FieldNode#isPublic() . 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: AsmClassGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static boolean isValidFieldNodeForByteCodeAccess(final FieldNode fn, final ClassNode accessingNode) {
    if (fn == null) return false;
    ClassNode declaringClass = fn.getDeclaringClass();
    // same class is always allowed access
    if (fn.isPublic() || declaringClass.equals(accessingNode)) return true;
    boolean samePackages = Objects.equals(declaringClass.getPackageName(), accessingNode.getPackageName());
    // protected means same class or same package, or subclass
    if (fn.isProtected() && (samePackages || accessingNode.isDerivedFrom(declaringClass))) {
        return true;
    }
    if (!fn.isPrivate()) {
        // package private is the only modifier left. It means  same package is allowed, subclass not, same class is
        return samePackages;
    }
    return false;
}
 
Example 2
Source File: StaticTypesCallSiteWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * Direct access is allowed from the declaring class of the field and sometimes from inner and peer types.
 *
 * @return {@code true} if GETFIELD or GETSTATIC is safe for given field and receiver
 */
private static boolean isDirectAccessAllowed(final FieldNode field, final ClassNode receiver) {
    // first, direct access from anywhere for public fields
    if (field.isPublic()) return true;

    ClassNode declaringType = field.getDeclaringClass().redirect(), receiverType = receiver.redirect();

    // next, direct access from within the declaring class
    if (receiverType.equals(declaringType)) return true;

    if (field.isPrivate()) return false;

    // next, direct access from within the declaring package
    if (Objects.equals(receiver.getPackageName(), declaringType.getPackageName())) return true;

    // last, inner class access to outer class fields
    receiverType = receiverType.getOuterClass();
    while (receiverType != null) {
        if (receiverType.equals(declaringType)) {
            return true;
        }
        receiverType = receiverType.getOuterClass();
    }
    return false;
}
 
Example 3
Source File: GroovyNodeToStringUtils.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
public static String variableToString(Variable variable, ASTNodeVisitor ast) {
	StringBuilder builder = new StringBuilder();
	if (variable instanceof FieldNode) {
		FieldNode fieldNode = (FieldNode) variable;
		if (fieldNode.isPublic()) {
			builder.append("public ");
		}
		if (fieldNode.isProtected()) {
			builder.append("protected ");
		}
		if (fieldNode.isPrivate()) {
			builder.append("private ");
		}

		if (fieldNode.isFinal()) {
			builder.append("final ");
		}

		if (fieldNode.isStatic()) {
			builder.append("static ");
		}
	}
	ClassNode varType = null;
	if (variable instanceof ASTNode) {
		varType = GroovyASTUtils.getTypeOfNode((ASTNode) variable, ast);
	} else {
		varType = variable.getType();
	}
	builder.append(varType.getNameWithoutPackage());
	builder.append(" ");
	builder.append(variable.getName());
	return builder.toString();
}
 
Example 4
Source File: ImmutableASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void ensureNotPublic(AbstractASTTransformation xform, String cNode, FieldNode fNode) {
    String fName = fNode.getName();
    // TODO: do we need to lock down things like: $ownClass
    if (fNode.isPublic() && !fName.contains("$") && !(fNode.isStatic() && fNode.isFinal())) {
        xform.addError("Public field '" + fName + "' not allowed for " + MY_TYPE_NAME + " class '" + cNode + "'.", fNode);
    }
}
 
Example 5
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean checkStaticOuterField(final PropertyExpression pexp, final String propertyName) {
    for (final ClassNode outer : controller.getClassNode().getOuterClasses()) {
        FieldNode field = outer.getDeclaredField(propertyName);
        if (field != null) {
            if (!field.isStatic()) break;

            Expression outerClass = classX(outer);
            outerClass.setNodeMetaData(PROPERTY_OWNER, outer);
            outerClass.setSourcePosition(pexp.getObjectExpression());

            Expression outerField = attrX(outerClass, pexp.getProperty());
            outerField.setSourcePosition(pexp);
            outerField.visit(this);
            return true;
        } else {
            field = outer.getField(propertyName); // checks supers
            if (field != null && !field.isPrivate() && (field.isPublic() || field.isProtected()
                    || Objects.equals(field.getDeclaringClass().getPackageName(), outer.getPackageName()))) {
                if (!field.isStatic()) break;

                Expression upperClass = classX(field.getDeclaringClass());
                upperClass.setNodeMetaData(PROPERTY_OWNER, field.getDeclaringClass());
                upperClass.setSourcePosition(pexp.getObjectExpression());

                Expression upperField = propX(upperClass, pexp.getProperty());
                upperField.setSourcePosition(pexp);
                upperField.visit(this);
                return true;
            }
        }
    }
    return false;
}
 
Example 6
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 4 votes vote down vote up
private boolean tryPropertyOfSuperClass(final PropertyExpression pexp, final String propertyName) {
    ClassNode classNode = controller.getClassNode();

    if (!controller.getCompileStack().isLHS()) {
        String methodName = "get" + capitalize(propertyName); // TODO: "is"
        callX(pexp.getObjectExpression(), methodName).visit(this);
        return true;
    }

    FieldNode fieldNode = classNode.getSuperClass().getField(propertyName);

    if (fieldNode == null) {
        throw new RuntimeParserException("Failed to find field[" + propertyName + "] of " + classNode.getName() + "'s super class", pexp);
    }
    if (fieldNode.isFinal()) {
        throw new RuntimeParserException("Cannot modify final field[" + propertyName + "] of " + classNode.getName() + "'s super class", pexp);
    }

    MethodNode setter = classNode.getSuperClass().getSetterMethod(getSetterName(propertyName));
    MethodNode getter = classNode.getSuperClass().getGetterMethod("get" + capitalize(propertyName));

    if (fieldNode.isPrivate() && (setter == null || getter == null || !setter.getDeclaringClass().equals(getter.getDeclaringClass()))) {
        throw new RuntimeParserException("Cannot access private field[" + propertyName + "] of " + classNode.getName() + "'s super class", pexp);
    }

    OperandStack operandStack = controller.getOperandStack();
    operandStack.doAsType(fieldNode.getType());

    MethodVisitor mv = controller.getMethodVisitor();
    mv.visitVarInsn(ALOAD, 0);
    operandStack.push(classNode);

    operandStack.swap();

    String owner = BytecodeHelper.getClassInternalName(classNode.getSuperClass().getName());
    String desc = BytecodeHelper.getTypeDescription(fieldNode.getType());
    if (fieldNode.isPublic() || fieldNode.isProtected()) {
        mv.visitFieldInsn(PUTFIELD, owner, propertyName, desc);
    } else {
        mv.visitMethodInsn(INVOKESPECIAL, owner, setter.getName(), BytecodeHelper.getMethodDescriptor(setter), false);
    }
    return true;
}