本文整理汇总了Java中org.codehaus.groovy.ast.expr.Expression.visit方法的典型用法代码示例。如果您正苦于以下问题:Java Expression.visit方法的具体用法?Java Expression.visit怎么用?Java Expression.visit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.codehaus.groovy.ast.expr.Expression
的用法示例。
在下文中一共展示了Expression.visit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitCastExpression
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
public void visitCastExpression(CastExpression castExpression) {
ClassNode type = castExpression.getType();
Expression subExpression = castExpression.getExpression();
subExpression.visit(this);
if (ClassHelper.OBJECT_TYPE.equals(type)) return;
if (castExpression.isCoerce()) {
controller.getOperandStack().doAsType(type);
} else {
if (isNullConstant(subExpression) && !ClassHelper.isPrimitiveType(type)) {
controller.getOperandStack().replace(type);
} else {
ClassNode subExprType = controller.getTypeChooser().resolveType(subExpression, controller.getClassNode());
if (castExpression.isStrict() ||
(!ClassHelper.isPrimitiveType(type) && WideningCategories.implementsInterfaceOrSubclassOf(subExprType, type))) {
BytecodeHelper.doCast(controller.getMethodVisitor(), type);
controller.getOperandStack().replace(type);
} else {
controller.getOperandStack().doGroovyCast(type);
}
}
}
}
示例2: visitExpressionStatement
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
@Override
public void visitExpressionStatement(ExpressionStatement es) {
Expression exp = es.getExpression();
if (exp instanceof BinaryExpression) {
exp.visit(this);
}
super.visitExpressionStatement(es);
}
示例3: writeEnumerationBasedForEachLoop
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
private void writeEnumerationBasedForEachLoop(
CompileStack compileStack,
OperandStack operandStack,
MethodVisitor mv,
ForStatement loop,
Expression collectionExpression,
ClassNode collectionType,
Parameter loopVariable) {
// Declare the loop counter.
BytecodeVariable variable = compileStack.defineVariable(loopVariable, false);
collectionExpression.visit(controller.getAcg());
// Then get the iterator and generate the loop control
int enumIdx = compileStack.defineTemporaryVariable("$enum", ENUMERATION_CLASSNODE, true);
Label continueLabel = compileStack.getContinueLabel();
Label breakLabel = compileStack.getBreakLabel();
mv.visitLabel(continueLabel);
mv.visitVarInsn(ALOAD, enumIdx);
ENUMERATION_HASMORE_METHOD.call(mv);
// note: ifeq tests for ==0, a boolean is 0 if it is false
mv.visitJumpInsn(IFEQ, breakLabel);
mv.visitVarInsn(ALOAD, enumIdx);
ENUMERATION_NEXT_METHOD.call(mv);
operandStack.push(ClassHelper.OBJECT_TYPE);
operandStack.storeVar(variable);
// Generate the loop body
loop.getLoopBlock().visit(controller.getAcg());
mv.visitJumpInsn(GOTO, continueLabel);
mv.visitLabel(breakLabel);
}
示例4: visitPropertyExpression
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
public void visitPropertyExpression(final PropertyExpression expression) {
assertExpressionAuthorized(expression);
Expression receiver = expression.getObjectExpression();
final String typeName = receiver.getType().getName();
if (receiversWhiteList != null && !receiversWhiteList.contains(typeName)) {
throw new SecurityException("Property access not allowed on [" + typeName + "]");
} else if (receiversBlackList != null && receiversBlackList.contains(typeName)) {
throw new SecurityException("Property access not allowed on [" + typeName + "]");
}
receiver.visit(this);
final Expression property = expression.getProperty();
checkConstantTypeIfNotMethodNameOrProperty(property);
}
示例5: checkConstantTypeIfNotMethodNameOrProperty
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
private void checkConstantTypeIfNotMethodNameOrProperty(final Expression expr) {
if (expr instanceof ConstantExpression) {
if (!"java.lang.String".equals(expr.getType().getName())) {
expr.visit(this);
}
} else {
expr.visit(this);
}
}
示例6: execMethodAndStoreForSubscriptOperator
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
private void execMethodAndStoreForSubscriptOperator(int op, String method, Expression expression, VariableSlotLoader usesSubscript, Expression orig) {
final OperandStack operandStack = controller.getOperandStack();
writePostOrPrefixMethod(op,method,expression,orig);
// we need special code for arrays to store the result (like for a[1]++)
if (usesSubscript!=null) {
CompileStack compileStack = controller.getCompileStack();
BinaryExpression be = (BinaryExpression) expression;
ClassNode methodResultType = operandStack.getTopOperand();
final int resultIdx = compileStack.defineTemporaryVariable("postfix_" + method, methodResultType, true);
BytecodeExpression methodResultLoader = new VariableSlotLoader(methodResultType, resultIdx, operandStack);
// execute the assignment, this will leave the right side
// (here the method call result) on the stack
assignToArray(be, be.getLeftExpression(), usesSubscript, methodResultLoader, be.isSafe());
compileStack.removeVar(resultIdx);
}
// here we handle a.b++ and a++
else if (expression instanceof VariableExpression ||
expression instanceof FieldExpression ||
expression instanceof PropertyExpression)
{
operandStack.dup();
controller.getCompileStack().pushLHS(true);
expression.visit(controller.getAcg());
controller.getCompileStack().popLHS();
}
// other cases don't need storing, so nothing to be done for them
}
示例7: visitForLoop
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
@Override
public void visitForLoop(final ForStatement forLoop) {
// collect every variable expression used in the loop body
final Map<VariableExpression, ClassNode> varOrigType = new HashMap<VariableExpression, ClassNode>();
forLoop.getLoopBlock().visit(new VariableExpressionTypeMemoizer(varOrigType));
// visit body
Map<VariableExpression, List<ClassNode>> oldTracker = pushAssignmentTracking();
Expression collectionExpression = forLoop.getCollectionExpression();
if (collectionExpression instanceof ClosureListExpression) {
// for (int i=0; i<...; i++) style loop
super.visitForLoop(forLoop);
} else {
collectionExpression.visit(this);
final ClassNode collectionType = getType(collectionExpression);
ClassNode componentType = inferLoopElementType(collectionType);
ClassNode forLoopVariableType = forLoop.getVariableType();
if (ClassHelper.getUnwrapper(componentType) == forLoopVariableType) {
// prefer primitive type over boxed type
componentType = forLoopVariableType;
}
if (!checkCompatibleAssignmentTypes(forLoopVariableType, componentType)) {
addStaticTypeError("Cannot loop with element of type " + forLoopVariableType.toString(false) + " with collection of type " + collectionType.toString(false), forLoop);
}
if (forLoopVariableType != DYNAMIC_TYPE) {
// user has specified a type, prefer it over the inferred type
componentType = forLoopVariableType;
}
typeCheckingContext.controlStructureVariables.put(forLoop.getVariable(), componentType);
try {
super.visitForLoop(forLoop);
} finally {
typeCheckingContext.controlStructureVariables.remove(forLoop.getVariable());
}
}
boolean typeChanged = isSecondPassNeededForControlStructure(varOrigType, oldTracker);
if (typeChanged) visitForLoop(forLoop);
}
示例8: visitConstructor
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
@Override
public void visitConstructor(ConstructorNode node) {
super.visitConstructor(node);
Statement code = node.getCode();
Expression cce = getConstructorCall(code);
if (cce==null) return;
cce.visit(this);
}
示例9: writeArrayGet
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
private void writeArrayGet(final Expression receiver, final Expression arguments, final ClassNode rType, final ClassNode aType) {
OperandStack operandStack = controller.getOperandStack();
int m1 = operandStack.getStackLength();
// visit receiver
receiver.visit(controller.getAcg());
// visit arguments as array index
arguments.visit(controller.getAcg());
operandStack.doGroovyCast(int_TYPE);
int m2 = operandStack.getStackLength();
// array access
controller.getMethodVisitor().visitInsn(AALOAD);
operandStack.replace(rType.getComponentType(), m2-m1);
}
示例10: writeReturn
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
public void writeReturn(ReturnStatement statement) {
controller.getAcg().onLineNumber(statement, "visitReturnStatement");
writeStatementLabel(statement);
MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();
ClassNode returnType = controller.getReturnType();
if (returnType == ClassHelper.VOID_TYPE) {
if (!(statement.isReturningNullOrVoid())) {
//TODO: move to Verifier
controller.getAcg().throwException("Cannot use return statement with an expression on a method that returns void");
}
controller.getCompileStack().applyBlockRecorder();
mv.visitInsn(RETURN);
return;
}
Expression expression = statement.getExpression();
expression.visit(controller.getAcg());
operandStack.doGroovyCast(returnType);
if (controller.getCompileStack().hasBlockRecorder()) {
ClassNode type = operandStack.getTopOperand();
int returnValueIdx = controller.getCompileStack().defineTemporaryVariable("returnValue", returnType, true);
controller.getCompileStack().applyBlockRecorder();
operandStack.load(type, returnValueIdx);
controller.getCompileStack().removeVar(returnValueIdx);
}
BytecodeHelper.doReturn(mv, returnType);
operandStack.remove(1);
}
示例11: visitListOfExpressions
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
protected void visitListOfExpressions(List<? extends Expression> list) {
if (list == null) return;
for (Expression expression : list) {
if (expression instanceof SpreadExpression) {
Expression spread = ((SpreadExpression) expression).getExpression();
spread.visit(this);
} else {
expression.visit(this);
}
}
}
示例12: evaluateCompareExpression
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
protected void evaluateCompareExpression(MethodCaller compareMethod, BinaryExpression expression) {
Expression leftExp = expression.getLeftExpression();
TypeChooser typeChooser = controller.getTypeChooser();
ClassNode cn = controller.getClassNode();
ClassNode leftType = typeChooser.resolveType(leftExp,cn);
Expression rightExp = expression.getRightExpression();
ClassNode rightType = typeChooser.resolveType(rightExp,cn);
boolean done = false;
if ( ClassHelper.isPrimitiveType(leftType) &&
ClassHelper.isPrimitiveType(rightType))
{
BinaryExpressionMultiTypeDispatcher helper = new BinaryExpressionMultiTypeDispatcher(getController());
done = helper.doPrimitiveCompare(leftType, rightType, expression);
}
if (!done) {
AsmClassGenerator acg = controller.getAcg();
OperandStack operandStack = controller.getOperandStack();
leftExp.visit(acg);
operandStack.box();
rightExp.visit(acg);
operandStack.box();
compareMethod.call(controller.getMethodVisitor());
ClassNode resType = ClassHelper.boolean_TYPE;
if (compareMethod==findRegexMethod) {
resType = ClassHelper.OBJECT_TYPE;
}
operandStack.replace(resType,2);
}
}
示例13: visitProperty
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
public void visitProperty(PropertyNode node) {
visitAnnotations(node);
Statement statement = node.getGetterBlock();
visitClassCodeContainer(statement);
statement = node.getSetterBlock();
visitClassCodeContainer(statement);
Expression init = node.getInitialExpression();
if (init != null) init.visit(this);
}
示例14: writeAICCall
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
protected boolean writeAICCall(ConstructorCallExpression call) {
if (!call.isUsingAnonymousInnerClass()) return false;
ConstructorNode cn = call.getType().getDeclaredConstructors().get(0);
OperandStack os = controller.getOperandStack();
String ownerDescriptor = prepareConstructorCall(cn);
List<Expression> args = makeArgumentList(call.getArguments()).getExpressions();
Parameter[] params = cn.getParameters();
// if a this appears as parameter here, then it should be
// not static, unless we are in a static method. But since
// ACG#visitVariableExpression does the opposite for this case, we
// push here an explicit this. This should not have any negative effect
// sine visiting a method call or property with implicit this will push
// a new value for this again.
controller.getCompileStack().pushImplicitThis(true);
for (int i=0; i<params.length; i++) {
Parameter p = params[i];
Expression arg = args.get(i);
if (arg instanceof VariableExpression) {
VariableExpression var = (VariableExpression) arg;
loadVariableWithReference(var);
} else {
arg.visit(controller.getAcg());
}
os.doGroovyCast(p.getType());
}
controller.getCompileStack().popImplicitThis();
finnishConstructorCall(cn, ownerDescriptor, args.size());
return true;
}
示例15: setField
import org.codehaus.groovy.ast.expr.Expression; //导入方法依赖的package包/类
private boolean setField(PropertyExpression expression, Expression objectExpression, ClassNode rType, String name) {
if (expression.isSafe()) return false;
FieldNode fn = AsmClassGenerator.getDeclaredFieldOfCurrentClassOrAccessibleFieldOfSuper(controller.getClassNode(), rType, name, false);
if (fn==null) return false;
OperandStack stack = controller.getOperandStack();
stack.doGroovyCast(fn.getType());
MethodVisitor mv = controller.getMethodVisitor();
String ownerName = BytecodeHelper.getClassInternalName(fn.getOwner());
if (!fn.isStatic()) {
controller.getCompileStack().pushLHS(false);
objectExpression.visit(controller.getAcg());
controller.getCompileStack().popLHS();
if (!rType.equals(stack.getTopOperand())) {
BytecodeHelper.doCast(mv, rType);
stack.replace(rType);
}
stack.swap();
mv.visitFieldInsn(PUTFIELD, ownerName, name, BytecodeHelper.getTypeDescription(fn.getType()));
stack.remove(1);
} else {
mv.visitFieldInsn(PUTSTATIC, ownerName, name, BytecodeHelper.getTypeDescription(fn.getType()));
}
//mv.visitInsn(ACONST_NULL);
//stack.replace(OBJECT_TYPE);
return true;
}