use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy-core by groovy.
the class Verifier method visitConstructor.
public void visitConstructor(ConstructorNode node) {
CodeVisitorSupport checkSuper = new CodeVisitorSupport() {
boolean firstMethodCall = true;
String type = null;
public void visitMethodCallExpression(MethodCallExpression call) {
if (!firstMethodCall)
return;
firstMethodCall = false;
String name = call.getMethodAsString();
// the name might be null if the method name is a GString for example
if (name == null)
return;
if (!name.equals("super") && !name.equals("this"))
return;
type = name;
call.getArguments().visit(this);
type = null;
}
public void visitConstructorCallExpression(ConstructorCallExpression call) {
if (!call.isSpecialCall())
return;
type = call.getText();
call.getArguments().visit(this);
type = null;
}
public void visitVariableExpression(VariableExpression expression) {
if (type == null)
return;
String name = expression.getName();
if (!name.equals("this") && !name.equals("super"))
return;
throw new RuntimeParserException("cannot reference " + name + " inside of " + type + "(....) before supertype constructor has been called", expression);
}
};
Statement s = node.getCode();
if (s == null) {
return;
} else {
s.visit(new VerifierCodeVisitor(this));
}
s.visit(checkSuper);
}
use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project groovy-core by groovy.
the class Verifier method getFirstIfSpecialConstructorCall.
private ConstructorCallExpression getFirstIfSpecialConstructorCall(Statement code) {
if (code == null || !(code instanceof ExpressionStatement))
return null;
Expression expression = ((ExpressionStatement) code).getExpression();
if (!(expression instanceof ConstructorCallExpression))
return null;
ConstructorCallExpression cce = (ConstructorCallExpression) expression;
if (cce.isSpecialCall())
return cce;
return null;
}
use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project grails-core by grails.
the class TestMixinTransformation method addLegacyMixinFieldIfNonExistent.
protected static FieldNode addLegacyMixinFieldIfNonExistent(ClassNode classNode, ClassNode fieldType, String fieldName) {
ClassNode targetAwareInterface = GrailsASTUtils.findInterface(fieldType, new ClassNode(TestMixinTargetAware.class).getPlainNodeReference());
if (classNode != null && classNode.getField(fieldName) == null) {
Expression constructorArgument = new ArgumentListExpression();
if (targetAwareInterface != null) {
MapExpression namedArguments = new MapExpression();
namedArguments.addMapEntryExpression(new MapEntryExpression(new ConstantExpression("target"), new VariableExpression("this")));
constructorArgument = namedArguments;
}
return classNode.addField(fieldName, Modifier.PRIVATE, fieldType, new ConstructorCallExpression(fieldType, constructorArgument));
}
return null;
}
use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project grails-core by grails.
the class TestMixinTransformation method addJunitRuleFields.
protected void addJunitRuleFields(ClassNode classNode) {
if (classNode.getField(JUNIT_ADAPTER_FIELD_NAME) != null) {
return;
}
ClassNode junitAdapterType = ClassHelper.make(TestRuntimeJunitAdapter.class);
FieldNode junitAdapterFieldNode = classNode.addField(JUNIT_ADAPTER_FIELD_NAME, Modifier.STATIC, junitAdapterType, new ConstructorCallExpression(junitAdapterType, MethodCallExpression.NO_ARGUMENTS));
boolean spockTest = isSpockTest(classNode);
FieldNode staticRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "StaticClassRule", Modifier.PRIVATE | Modifier.STATIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newClassRule", new ClassExpression(classNode)));
AnnotationNode classRuleAnnotation = new AnnotationNode(ClassHelper.make(ClassRule.class));
if (spockTest) {
// @ClassRule must be added to @Shared field in spock
FieldNode spockSharedRuleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "SharedClassRule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new FieldExpression(staticRuleFieldNode));
spockSharedRuleFieldNode.addAnnotation(classRuleAnnotation);
spockSharedRuleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Shared.class)));
addSpockFieldMetadata(spockSharedRuleFieldNode, 0);
} else {
staticRuleFieldNode.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
staticRuleFieldNode.addAnnotation(classRuleAnnotation);
}
FieldNode ruleFieldNode = classNode.addField(RULE_FIELD_NAME_BASE + "Rule", Modifier.PUBLIC, ClassHelper.make(TestRule.class), new MethodCallExpression(new FieldExpression(junitAdapterFieldNode), "newRule", new VariableExpression("this")));
ruleFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(Rule.class)));
if (spockTest) {
addSpockFieldMetadata(ruleFieldNode, 0);
}
}
use of org.codehaus.groovy.ast.expr.ConstructorCallExpression in project grails-core by grails.
the class TestMixinTransformation method addTestRuntimeAwareMixinFieldIfNonExistent.
protected FieldNode addTestRuntimeAwareMixinFieldIfNonExistent(ClassNode classNode, ClassNode fieldType, String fieldName) {
if (classNode == null || classNode.getField(fieldName) != null) {
return null;
}
MapExpression constructorArguments = new MapExpression();
constructorArguments.addMapEntryExpression(new MapEntryExpression(new ConstantExpression("testClass"), new ClassExpression(classNode)));
FieldNode mixinInstanceFieldNode = classNode.addField(fieldName, Modifier.STATIC, fieldType, new ConstructorCallExpression(fieldType, constructorArguments));
mixinInstanceFieldNode.addAnnotation(new AnnotationNode(ClassHelper.make(MixinInstance.class)));
addJunitRuleFields(classNode);
return mixinInstanceFieldNode;
}
Aggregations