use of org.codehaus.groovy.ast.expr.VariableExpression in project groovy by apache.
the class AutoNewLineTransformer method createNewLine.
private Statement createNewLine(final ASTNode node) {
MethodCallExpression mce = new MethodCallExpression(new VariableExpression("this"), "newLine", ArgumentListExpression.EMPTY_ARGUMENTS);
mce.setImplicitThis(true);
mce.setSourcePosition(node);
ExpressionStatement stmt = new ExpressionStatement(mce);
stmt.setSourcePosition(node);
return stmt;
}
use of org.codehaus.groovy.ast.expr.VariableExpression in project gradle by gradle.
the class GradleResolveVisitor method lookupClassName.
private String lookupClassName(PropertyExpression pe) {
boolean doInitialClassTest = true;
String name = "";
// separated by "."
for (Expression it = pe; it != null; it = ((PropertyExpression) it).getObjectExpression()) {
if (it instanceof VariableExpression) {
VariableExpression ve = (VariableExpression) it;
// stop at super and this
if (ve.isSuperExpression() || ve.isThisExpression()) {
return null;
}
String varName = ve.getName();
if (doInitialClassTest) {
// field bar.
if (!testVanillaNameForClass(varName)) {
return null;
}
doInitialClassTest = false;
name = varName;
} else {
name = varName + "." + name;
}
break;
} else if (it.getClass() != PropertyExpression.class) {
// VariableExpressions will stop resolving
return null;
} else {
PropertyExpression current = (PropertyExpression) it;
String propertyPart = current.getPropertyAsString();
// the class property stops resolving, dynamic property names too
if (propertyPart == null || propertyPart.equals("class")) {
return null;
}
if (doInitialClassTest) {
// field bar.
if (!testVanillaNameForClass(propertyPart)) {
return null;
}
doInitialClassTest = false;
name = propertyPart;
} else {
name = propertyPart + "." + name;
}
}
}
if (name.length() == 0) {
return null;
}
return name;
}
use of org.codehaus.groovy.ast.expr.VariableExpression in project gradle by gradle.
the class RulesVisitor method extractModelPathFromMethodTarget.
// if the target was invalid
@Nullable
private String extractModelPathFromMethodTarget(MethodCallExpression call) {
Expression target = call.getMethod();
List<String> names = Lists.newLinkedList();
while (true) {
if (target instanceof ConstantExpression) {
if (target.getType().equals(ClassHelper.STRING_TYPE)) {
String name = target.getText();
names.add(0, name);
if (call.isImplicitThis()) {
break;
} else {
target = call.getObjectExpression();
continue;
}
}
} else if (target instanceof PropertyExpression) {
PropertyExpression propertyExpression = (PropertyExpression) target;
Expression property = propertyExpression.getProperty();
if (property instanceof ConstantExpression) {
ConstantExpression constantProperty = (ConstantExpression) property;
if (constantProperty.getType().equals(ClassHelper.STRING_TYPE)) {
String name = constantProperty.getText();
names.add(0, name);
target = propertyExpression.getObjectExpression();
continue;
}
}
} else if (target instanceof VariableExpression) {
// This will be the left most property
names.add(0, ((VariableExpression) target).getName());
break;
}
// Invalid paths fall through to here
restrict(call);
return null;
}
// TODO - validate that it's a valid model path
return ModelPath.pathString(Iterables.toArray(names, String.class));
}
use of org.codehaus.groovy.ast.expr.VariableExpression in project groovy-core by groovy.
the class InnerClassVisitor method visitConstructorCallExpression.
@Override
public void visitConstructorCallExpression(ConstructorCallExpression call) {
super.visitConstructorCallExpression(call);
if (!call.isUsingAnonymousInnerClass()) {
passThisReference(call);
return;
}
InnerClassNode innerClass = (InnerClassNode) call.getType();
ClassNode outerClass = innerClass.getOuterClass();
ClassNode superClass = innerClass.getSuperClass();
if (superClass instanceof InnerClassNode && !superClass.isInterface() && !(superClass.isStaticClass() || ((superClass.getModifiers() & ACC_STATIC) == ACC_STATIC))) {
insertThis0ToSuperCall(call, innerClass);
}
if (!innerClass.getDeclaredConstructors().isEmpty())
return;
if ((innerClass.getModifiers() & ACC_STATIC) != 0)
return;
VariableScope scope = innerClass.getVariableScope();
if (scope == null)
return;
// expressions = constructor call arguments
List<Expression> expressions = ((TupleExpression) call.getArguments()).getExpressions();
// block = init code for the constructor we produce
BlockStatement block = new BlockStatement();
// parameters = parameters of the constructor
final int additionalParamCount = 1 + scope.getReferencedLocalVariablesCount();
List<Parameter> parameters = new ArrayList<Parameter>(expressions.size() + additionalParamCount);
// superCallArguments = arguments for the super call == the constructor call arguments
List<Expression> superCallArguments = new ArrayList<Expression>(expressions.size());
// first we add a super() call for all expressions given in the
// constructor call expression
int pCount = additionalParamCount;
for (Expression expr : expressions) {
pCount++;
// add one parameter for each expression in the
// constructor call
Parameter param = new Parameter(ClassHelper.OBJECT_TYPE, "p" + pCount);
parameters.add(param);
// add to super call
superCallArguments.add(new VariableExpression(param));
}
// add the super call
ConstructorCallExpression cce = new ConstructorCallExpression(ClassNode.SUPER, new TupleExpression(superCallArguments));
block.addStatement(new ExpressionStatement(cce));
// we need to add "this" to access unknown methods/properties
// this is saved in a field named this$0
pCount = 0;
expressions.add(pCount, VariableExpression.THIS_EXPRESSION);
boolean isStatic = isStaticThis(innerClass, scope);
ClassNode outerClassType = getClassNode(outerClass, isStatic);
if (!isStatic && inClosure)
outerClassType = ClassHelper.CLOSURE_TYPE;
outerClassType = outerClassType.getPlainNodeReference();
Parameter thisParameter = new Parameter(outerClassType, "p" + pCount);
parameters.add(pCount, thisParameter);
thisField = innerClass.addField("this$0", PUBLIC_SYNTHETIC, outerClassType, null);
addFieldInit(thisParameter, thisField, block);
// for each shared variable we add a reference and save it as field
for (Iterator it = scope.getReferencedLocalVariablesIterator(); it.hasNext(); ) {
pCount++;
org.codehaus.groovy.ast.Variable var = (org.codehaus.groovy.ast.Variable) it.next();
VariableExpression ve = new VariableExpression(var);
ve.setClosureSharedVariable(true);
ve.setUseReferenceDirectly(true);
expressions.add(pCount, ve);
ClassNode rawReferenceType = ClassHelper.REFERENCE_TYPE.getPlainNodeReference();
Parameter p = new Parameter(rawReferenceType, "p" + pCount);
parameters.add(pCount, p);
p.setOriginType(var.getOriginType());
final VariableExpression initial = new VariableExpression(p);
initial.setSynthetic(true);
initial.setUseReferenceDirectly(true);
final FieldNode pField = innerClass.addFieldFirst(ve.getName(), PUBLIC_SYNTHETIC, rawReferenceType, initial);
pField.setHolder(true);
pField.setOriginType(ClassHelper.getWrapper(var.getOriginType()));
}
innerClass.addConstructor(ACC_SYNTHETIC, parameters.toArray(new Parameter[parameters.size()]), ClassNode.EMPTY_ARRAY, block);
}
use of org.codehaus.groovy.ast.expr.VariableExpression 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);
}
Aggregations