"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/main/java/org/codehaus/groovy/transform/trait/TraitReceiverTransformer.java" between
apache-groovy-src-3.0.8.zip and apache-groovy-src-3.0.9.zip

About: Groovy is an agile dynamic (scripting) language for the Java Platform. It has features similar to those of Python, Ruby, Perl, and Smalltalk. Source release.

TraitReceiverTransformer.java  (apache-groovy-src-3.0.8):TraitReceiverTransformer.java  (apache-groovy-src-3.0.9)
skipping to change at line 33 skipping to change at line 33
import org.codehaus.groovy.ast.ClassHelper; import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode; import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.DynamicVariable; import org.codehaus.groovy.ast.DynamicVariable;
import org.codehaus.groovy.ast.FieldNode; import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.GenericsType; import org.codehaus.groovy.ast.GenericsType;
import org.codehaus.groovy.ast.MethodNode; import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.PropertyNode; import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.Variable; import org.codehaus.groovy.ast.Variable;
import org.codehaus.groovy.ast.expr.ArgumentListExpression; import org.codehaus.groovy.ast.expr.ArgumentListExpression;
import org.codehaus.groovy.ast.expr.BinaryExpression; import org.codehaus.groovy.ast.expr.BinaryExpression;
import org.codehaus.groovy.ast.expr.ClassExpression;
import org.codehaus.groovy.ast.expr.ClosureExpression; import org.codehaus.groovy.ast.expr.ClosureExpression;
import org.codehaus.groovy.ast.expr.ConstantExpression; import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.expr.DeclarationExpression; import org.codehaus.groovy.ast.expr.DeclarationExpression;
import org.codehaus.groovy.ast.expr.Expression; import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.FieldExpression; import org.codehaus.groovy.ast.expr.FieldExpression;
import org.codehaus.groovy.ast.expr.MethodCallExpression; import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.expr.PropertyExpression; import org.codehaus.groovy.ast.expr.PropertyExpression;
import org.codehaus.groovy.ast.expr.StaticMethodCallExpression; import org.codehaus.groovy.ast.expr.StaticMethodCallExpression;
import org.codehaus.groovy.ast.expr.TupleExpression; import org.codehaus.groovy.ast.expr.TupleExpression;
import org.codehaus.groovy.ast.expr.VariableExpression; import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.control.SourceUnit; import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.syntax.SyntaxException; import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.syntax.Token; import org.codehaus.groovy.syntax.Token;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import static org.codehaus.groovy.ast.tools.GeneralUtils.INSTANCEOF;
import static org.codehaus.groovy.ast.tools.GeneralUtils.args; import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
import static org.codehaus.groovy.ast.tools.GeneralUtils.binX; import static org.codehaus.groovy.ast.tools.GeneralUtils.binX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callX; import static org.codehaus.groovy.ast.tools.GeneralUtils.callX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.castX; import static org.codehaus.groovy.ast.tools.GeneralUtils.castX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.classX; import static org.codehaus.groovy.ast.tools.GeneralUtils.classX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.isInstanceOfX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.propX; import static org.codehaus.groovy.ast.tools.GeneralUtils.propX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.ternaryX; import static org.codehaus.groovy.ast.tools.GeneralUtils.ternaryX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX; import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
/** /**
* This expression transformer is used internally by the {@link org.codehaus.gro ovy.transform.trait.TraitASTTransformation * This expression transformer is used internally by the {@link org.codehaus.gro ovy.transform.trait.TraitASTTransformation
* trait} AST transformation to change the receiver of a message on "this" into a static method call on the trait helper * trait} AST transformation to change the receiver of a message on "this" into a static method call on the trait helper
* class. * class.
* <p> * <p>
* In a nutshell, code like the following method definition in a trait: <code>vo id foo() { this.bar() }</code> is * In a nutshell, code like the following method definition in a trait: <code>vo id foo() { this.bar() }</code> is
skipping to change at line 242 skipping to change at line 240
Expression ret = exp instanceof DeclarationExpression ? Expression ret = exp instanceof DeclarationExpression ?
new DeclarationExpression(leftTransform, operation, rightTransfo rm) : binX(leftTransform, operation, rightTransform); new DeclarationExpression(leftTransform, operation, rightTransfo rm) : binX(leftTransform, operation, rightTransform);
ret.setSourcePosition(exp); ret.setSourcePosition(exp);
ret.copyNodeMetaData(exp); ret.copyNodeMetaData(exp);
return ret; return ret;
} }
private Expression transformFieldReference(final Expression exp, final Field Node fn, final boolean isStatic) { private Expression transformFieldReference(final Expression exp, final Field Node fn, final boolean isStatic) {
Expression receiver = createFieldHelperReceiver(); Expression receiver = createFieldHelperReceiver();
if (isStatic) { if (isStatic) {
Expression isClass = binX(receiver, INSTANCEOF, classX(ClassHelper.C receiver = asClass(receiver);
LASS_Type));
receiver = ternaryX(isClass, receiver, callX(receiver, "getClass"));
} }
MethodCallExpression mce = callX(receiver, Traits.helperGetterName(fn)); MethodCallExpression mce = callX(receiver, Traits.helperGetterName(fn));
mce.setImplicitThis(false); mce.setImplicitThis(false);
mce.setSourcePosition(exp); mce.setSourcePosition(exp);
markDynamicCall(mce, fn, isStatic); markDynamicCall(mce, fn, isStatic);
return mce; return mce;
} }
private static void markDynamicCall(final MethodCallExpression mce, final Fi eldNode fn, final boolean isStatic) { private static void markDynamicCall(final MethodCallExpression mce, final Fi eldNode fn, final boolean isStatic) {
skipping to change at line 291 skipping to change at line 288
Expression arguments = transform(call.getArguments()); Expression arguments = transform(call.getArguments());
ArgumentListExpression superCallArgs = new ArgumentListExpression(); ArgumentListExpression superCallArgs = new ArgumentListExpression();
if (arguments instanceof ArgumentListExpression) { if (arguments instanceof ArgumentListExpression) {
ArgumentListExpression list = (ArgumentListExpression) arguments; ArgumentListExpression list = (ArgumentListExpression) arguments;
for (Expression expression : list) { for (Expression expression : list) {
superCallArgs.addExpression(expression); superCallArgs.addExpression(expression);
} }
} else { } else {
superCallArgs.addExpression(arguments); superCallArgs.addExpression(arguments);
} }
MethodCallExpression transformed = new MethodCallExpression( MethodCallExpression newCall = callX(
weaved, weaved,
Traits.getSuperTraitMethodName(traitClass, method), Traits.getSuperTraitMethodName(traitClass, method),
superCallArgs superCallArgs
); );
transformed.setSourcePosition(call); newCall.setSourcePosition(call);
transformed.setSafe(call.isSafe()); newCall.setSafe(call.isSafe());
transformed.setSpreadSafe(call.isSpreadSafe()); newCall.setSpreadSafe(call.isSpreadSafe());
transformed.setImplicitThis(false); newCall.setImplicitThis(false);
return transformed; return newCall;
} }
private Expression transformMethodCallOnThis(final MethodCallExpression call ) { private Expression transformMethodCallOnThis(final MethodCallExpression call ) {
Expression method = call.getMethod(); Expression method = call.getMethod();
Expression arguments = call.getArguments(); Expression arguments = call.getArguments();
Expression objectExpr = call.getObjectExpression();
if (method instanceof ConstantExpression) { if (method instanceof ConstantExpression) {
String methodName = method.getText(); String methodName = call.getMethodAsString();
List<MethodNode> methods = traitClass.getMethods(methodName); for (MethodNode methodNode : traitClass.getMethods(methodName)) {
for (MethodNode methodNode : methods) { if (methodName.equals(methodNode.getName()) && (methodNode.isSta
if (methodName.equals(methodNode.getName()) && methodNode.isPriv tic() || methodNode.isPrivate())) {
ate()) { ArgumentListExpression newArgs = createArgumentList(methodNo
if (inClosure) { de.isStatic() ? asClass(objectExpr) : weaved, arguments);
return transformPrivateMethodCallOnThisInClosure(call, a MethodCallExpression newCall = callX(inClosure ? classX(trai
rguments, methodName); tHelperClass) : varX("this"), methodName, newArgs);
} newCall.setImplicitThis(true);
return transformPrivateMethodCallOnThis(call, arguments, met newCall.setSafe(call.isSafe());
hodName); newCall.setSourcePosition(call);
newCall.setSpreadSafe(call.isSpreadSafe());
return newCall;
} }
} }
} }
if (inClosure) {
return transformMethodCallOnThisInClosure(call);
}
return transformMethodCallOnThisFallBack(call, method, arguments);
}
private Expression transformMethodCallOnThisFallBack(final MethodCallExpress
ion call,
final Expression method
, final Expression arguments) {
MethodCallExpression transformed = new MethodCallExpression(
weaved,
method,
transform(arguments)
);
transformed.setSourcePosition(call);
transformed.setSafe(call.isSafe());
transformed.setSpreadSafe(call.isSpreadSafe());
transformed.setImplicitThis(false);
return transformed;
}
private Expression transformMethodCallOnThisInClosure(final MethodCallExpres
sion call) {
MethodCallExpression transformed = new MethodCallExpression(
(Expression) call.getReceiver(),
call.getMethod(),
transform(call.getArguments())
);
transformed.setSourcePosition(call);
transformed.setSafe(call.isSafe());
transformed.setSpreadSafe(call.isSpreadSafe());
transformed.setImplicitThis(call.isImplicitThis());
return transformed;
}
private Expression transformPrivateMethodCallOnThis(final MethodCallExpressi
on call,
final Expression argumen
ts, final String methodName) {
ArgumentListExpression newArgs = createArgumentList(arguments);
MethodCallExpression transformed = new MethodCallExpression(
new VariableExpression("this"),
methodName,
newArgs
);
transformed.setSourcePosition(call);
transformed.setSafe(call.isSafe());
transformed.setSpreadSafe(call.isSpreadSafe());
transformed.setImplicitThis(true);
return transformed;
}
private Expression transformPrivateMethodCallOnThisInClosure(final MethodCal MethodCallExpression newCall = callX(inClosure ? objectExpr : weaved, me
lExpression call, thod, transform(arguments));
final Expressio newCall.setImplicitThis(inClosure ? call.isImplicitThis() : false);
n arguments, final String methodName) { newCall.setSafe(call.isSafe());
ArgumentListExpression newArgs = createArgumentList(arguments); newCall.setSourcePosition(call);
MethodCallExpression transformed = new MethodCallExpression( newCall.setSpreadSafe(call.isSpreadSafe());
new ClassExpression(traitHelperClass), return newCall;
methodName,
newArgs
);
transformed.setSourcePosition(call);
transformed.setSafe(call.isSafe());
transformed.setSpreadSafe(call.isSpreadSafe());
transformed.setImplicitThis(true);
return transformed;
} }
private ArgumentListExpression createArgumentList(final Expression origCallA rgs) { private ArgumentListExpression createArgumentList(final Expression self, fin al Expression arguments) {
ArgumentListExpression newArgs = new ArgumentListExpression(); ArgumentListExpression newArgs = new ArgumentListExpression();
newArgs.addExpression(new VariableExpression(weaved)); newArgs.addExpression(self);
if (origCallArgs instanceof TupleExpression) { if (arguments instanceof TupleExpression) {
List<Expression> expressions = ((TupleExpression) origCallArgs).getE for (Expression argument : (TupleExpression) arguments) {
xpressions(); newArgs.addExpression(transform(argument));
for (Expression expression : expressions) {
newArgs.addExpression(transform(expression));
} }
} else { } else {
newArgs.addExpression(origCallArgs); newArgs.addExpression(transform(arguments));
} }
return newArgs; return newArgs;
} }
private Expression createFieldHelperReceiver() { private Expression createFieldHelperReceiver() {
return weaved.getOriginType().equals(ClassHelper.CLASS_Type) ? weaved : castX(fieldHelper, weaved); return weaved.getOriginType().equals(ClassHelper.CLASS_Type) ? weaved : castX(fieldHelper, weaved);
} }
private Expression asClass(final Expression e) {
ClassNode rawClass = ClassHelper.CLASS_Type.getPlainNodeReference();
return ternaryX(isInstanceOfX(e, rawClass), e, callX(e, "getClass"));
}
} }
 End of changes. 15 change blocks. 
98 lines changed or deleted 41 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)