use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class PropertyTest method testProperties.
public void testProperties() throws Exception {
ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC + ACC_SUPER, ClassHelper.OBJECT_TYPE);
classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
Class fooClass = loadClass(classNode);
assertTrue("Loaded a new class", fooClass != null);
Object bean = fooClass.newInstance();
assertTrue("Managed to create bean", bean != null);
assertField(fooClass, "bar", 0, ClassHelper.STRING_TYPE);
assertGetProperty(bean, "bar", null);
assertSetProperty(bean, "bar", "newValue");
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class ClassCompletionVerifierTest method testDetectsIncorrectMemberVisibilityInInterface.
public void testDetectsIncorrectMemberVisibilityInInterface() throws Exception {
ClassNode node = new ClassNode("zzz", ACC_ABSTRACT | ACC_INTERFACE, ClassHelper.OBJECT_TYPE);
node.addMethod(new MethodNode("prim", ACC_PRIVATE, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null));
node.addMethod(new MethodNode("prom", ACC_PROTECTED, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null));
node.addField("prif", ACC_PRIVATE, ClassHelper.OBJECT_TYPE, null);
node.addField("prof", ACC_PROTECTED, ClassHelper.OBJECT_TYPE, null);
addDummyConstructor(node);
verifier.visitClass(node);
checkErrorCount(4);
checkErrorMessage(EXPECTED_PROTECTED_FIELD_ERROR_MESSAGE);
checkErrorMessage(EXPECTED_PRIVATE_FIELD_ERROR_MESSAGE);
checkErrorMessage(EXPECTED_PROTECTED_METHOD_ERROR_MESSAGE);
checkErrorMessage(EXPECTED_PRIVATE_METHOD_ERROR_MESSAGE);
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class ClassCompletionVerifierTest method testDetectsFinalAndStaticMethodsInInterface.
public void testDetectsFinalAndStaticMethodsInInterface() throws Exception {
ClassNode node = new ClassNode("zzz", ACC_ABSTRACT | ACC_INTERFACE, ClassHelper.OBJECT_TYPE);
node.addMethod(new MethodNode("xxx", ACC_PUBLIC | ACC_FINAL, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null));
node.addMethod(new MethodNode("yyy", ACC_PUBLIC | ACC_STATIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null));
addDummyConstructor(node);
verifier.visitClass(node);
checkErrorCount(2);
checkErrorMessage(EXPECTED_INTERFACE_FINAL_METHOD_ERROR_MESSAGE);
checkErrorMessage(EXPECTED_INTERFACE_STATIC_METHOD_ERROR_MESSAGE);
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class ClassCompletionVerifierTest method testDetectsIncorrectMethodModifiersInInterface.
public void testDetectsIncorrectMethodModifiersInInterface() throws Exception {
// can't check volatile here as it doubles up with bridge
ClassNode node = new ClassNode("zzz", ACC_ABSTRACT | ACC_INTERFACE, ClassHelper.OBJECT_TYPE);
node.addMethod(new MethodNode("st", ACC_STRICT, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null));
node.addMethod(new MethodNode("na", ACC_NATIVE, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null));
node.addMethod(new MethodNode("sy", ACC_SYNCHRONIZED, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null));
addDummyConstructor(node);
verifier.visitClass(node);
checkErrorCount(3);
checkErrorMessage(EXPECTED_STRICT_METHOD_ERROR_MESSAGE);
checkErrorMessage(EXPECTED_NATIVE_METHOD_ERROR_MESSAGE);
checkErrorMessage(EXPECTED_SYNCHRONIZED_METHOD_ERROR_MESSAGE);
}
use of org.codehaus.groovy.ast.ClassNode in project groovy by apache.
the class StaticImportVisitor method transformInlineConstants.
// resolve constant-looking expressions statically (do here as gets transformed away later)
private Expression transformInlineConstants(Expression exp) {
if (exp instanceof PropertyExpression) {
PropertyExpression pe = (PropertyExpression) exp;
if (pe.getObjectExpression() instanceof ClassExpression) {
ClassExpression ce = (ClassExpression) pe.getObjectExpression();
ClassNode type = ce.getType();
if (type.isEnum())
return exp;
Expression constant = findConstant(getField(type, pe.getPropertyAsString()));
if (constant != null)
return constant;
}
} else if (exp instanceof ListExpression) {
ListExpression le = (ListExpression) exp;
ListExpression result = new ListExpression();
for (Expression e : le.getExpressions()) {
result.addExpression(transformInlineConstants(e));
}
return result;
}
return exp;
}
Aggregations