org.codehaus.groovy.ast.expr.FieldExpression Java Examples
The following examples show how to use
org.codehaus.groovy.ast.expr.FieldExpression.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BinaryExpressionHelper.java From groovy with Apache License 2.0 | 6 votes |
private void execMethodAndStoreForSubscriptOperator(final int op, String method, final Expression expression, final VariableSlotLoader usesSubscript, final Expression orig) { writePostOrPrefixMethod(op, method, expression, orig); // we need special code for arrays to store the result (like for a[1]++) if (usesSubscript != null) { BinaryExpression be = (BinaryExpression) expression; CompileStack compileStack = controller.getCompileStack(); OperandStack operandStack = controller.getOperandStack(); ClassNode methodResultType = operandStack.getTopOperand(); 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); } else if (expression instanceof VariableExpression || expression instanceof PropertyExpression || expression instanceof FieldExpression) { // here we handle a++ and a.b++ controller.getOperandStack().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 }
Example #2
Source File: ClassNode.java From groovy with Apache License 2.0 | 6 votes |
public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) { MethodNode constructor = getOrAddStaticConstructorNode(); Statement statement = constructor.getCode(); if (statement instanceof BlockStatement) { BlockStatement block = (BlockStatement) statement; // add given statements for explicitly declared static fields just after enum-special fields // are found - the $VALUES binary expression marks the end of such fields. List<Statement> blockStatements = block.getStatements(); ListIterator<Statement> litr = blockStatements.listIterator(); while (litr.hasNext()) { Statement stmt = litr.next(); if (stmt instanceof ExpressionStatement && ((ExpressionStatement) stmt).getExpression() instanceof BinaryExpression) { BinaryExpression bExp = (BinaryExpression) ((ExpressionStatement) stmt).getExpression(); if (bExp.getLeftExpression() instanceof FieldExpression) { FieldExpression fExp = (FieldExpression) bExp.getLeftExpression(); if (fExp.getFieldName().equals("$VALUES")) { for (Statement tmpStmt : staticFieldStatements) { litr.add(tmpStmt); } } } } } } }
Example #3
Source File: AsmClassGenerator.java From groovy with Apache License 2.0 | 6 votes |
private void storeStaticField(final FieldExpression expression) { MethodVisitor mv = controller.getMethodVisitor(); FieldNode field = expression.getField(); ClassNode type = field.getType(); controller.getOperandStack().doGroovyCast(field); if (field.isHolder() && !controller.isInGeneratedFunctionConstructor()) { controller.getOperandStack().box(); mv.visitFieldInsn(GETSTATIC, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type)); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false); } else { mv.visitFieldInsn(PUTSTATIC, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type)); } controller.getOperandStack().remove(1); }
Example #4
Source File: AsmClassGenerator.java From groovy with Apache License 2.0 | 6 votes |
/** * RHS instance field. should move most of the code in the BytecodeHelper */ public void loadInstanceField(final FieldExpression expression) { MethodVisitor mv = controller.getMethodVisitor(); FieldNode field = expression.getField(); ClassNode type = field.getType(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type)); if (field.isHolder() && !controller.isInGeneratedFunctionConstructor()) { mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "get", "()Ljava/lang/Object;", false); controller.getOperandStack().push(ClassHelper.OBJECT_TYPE); } else { controller.getOperandStack().push(type); } }
Example #5
Source File: AsmClassGenerator.java From groovy with Apache License 2.0 | 6 votes |
@Override public void visitFieldExpression(final FieldExpression expression) { if (expression.getField().isStatic()) { if (controller.getCompileStack().isLHS()) { storeStaticField(expression); } else { loadStaticField(expression); } } else { if (controller.getCompileStack().isLHS()) { storeThisInstanceField(expression); } else { loadInstanceField(expression); } } }
Example #6
Source File: TestSupport.java From groovy with Apache License 2.0 | 5 votes |
protected ExpressionStatement createPrintlnStatement(Expression expression) throws NoSuchFieldException { return new ExpressionStatement( new MethodCallExpression( new FieldExpression(FieldNode.newStatic(System.class, "out")), "println", expression)); }
Example #7
Source File: ClosureWriter.java From groovy with Apache License 2.0 | 5 votes |
protected void addFieldsAndGettersForLocalVariables(final InnerClassNode answer, final Parameter[] localVariableParams) { for (Parameter param : localVariableParams) { String paramName = param.getName(); ClassNode type = param.getType(); VariableExpression initialValue = new VariableExpression(paramName); initialValue.setAccessedVariable(param); initialValue.setUseReferenceDirectly(true); ClassNode realType = type; type = ClassHelper.makeReference(); param.setType(ClassHelper.makeReference()); FieldNode paramField = answer.addField(paramName, ACC_PRIVATE | ACC_SYNTHETIC, type, initialValue); paramField.setOriginType(ClassHelper.getWrapper(param.getOriginType())); paramField.setHolder(true); String methodName = Verifier.capitalize(paramName); // let's add a getter & setter Expression fieldExp = new FieldExpression(paramField); markAsGenerated(answer, answer.addMethod( "get" + methodName, ACC_PUBLIC, realType.getPlainNodeReference(), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new ReturnStatement(fieldExp)), true); } }
Example #8
Source File: ClosureWriter.java From groovy with Apache License 2.0 | 5 votes |
public static void loadReference(final String name, final WriterController controller) { CompileStack compileStack = controller.getCompileStack(); MethodVisitor mv = controller.getMethodVisitor(); ClassNode classNode = controller.getClassNode(); AsmClassGenerator acg = controller.getAcg(); // compileStack.containsVariable(name) means to ask if the variable is already declared // compileStack.getScope().isReferencedClassVariable(name) means to ask if the variable is a field // If it is no field and is not yet declared, then it is either a closure shared variable or // an already declared variable. if (!compileStack.containsVariable(name) && compileStack.getScope().isReferencedClassVariable(name)) { acg.visitFieldExpression(new FieldExpression(classNode.getDeclaredField(name))); } else { BytecodeVariable v = compileStack.getVariable(name, !classNodeUsesReferences(controller.getClassNode())); if (v == null) { // variable is not on stack because we are // inside a nested Closure and this variable // was not used before // then load it from the Closure field FieldNode field = classNode.getDeclaredField(name); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, controller.getInternalClassName(), name, BytecodeHelper.getTypeDescription(field.getType())); } else { mv.visitVarInsn(ALOAD, v.getIndex()); } controller.getOperandStack().push(ClassHelper.REFERENCE_TYPE); } }
Example #9
Source File: TraitReceiverTransformer.java From groovy with Apache License 2.0 | 5 votes |
private Expression transformFieldExpression(final FieldExpression exp) { FieldNode field = exp.getField(); MethodCallExpression mce = new MethodCallExpression( createFieldHelperReceiver(), Traits.helperGetterName(field), ArgumentListExpression.EMPTY_ARGUMENTS ); mce.setSourcePosition(exp); mce.setImplicitThis(false); markDynamicCall(mce, field, field.isStatic()); return mce; }
Example #10
Source File: VariableScopeVisitor.java From groovy with Apache License 2.0 | 5 votes |
@Override public void visitFieldExpression(final FieldExpression expression) { String name = expression.getFieldName(); //TODO: change that to get the correct scope Variable variable = findVariableDeclaration(name); checkVariableContextAccess(variable, expression); }
Example #11
Source File: AsmClassGenerator.java From groovy with Apache License 2.0 | 5 votes |
private void storeThisInstanceField(final FieldExpression expression) { OperandStack operandStack = controller.getOperandStack(); MethodVisitor mv = controller.getMethodVisitor(); FieldNode field = expression.getField(); ClassNode type = field.getType(); if (field.isHolder() && expression.isUseReferenceDirectly()) { // rhs is ready to use reference, just put it in the field mv.visitVarInsn(ALOAD, 0); operandStack.push(controller.getClassNode()); operandStack.swap(); mv.visitFieldInsn(PUTFIELD, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type)); } else if (field.isHolder()) { // rhs is normal value, set the value in the Reference operandStack.doGroovyCast(field.getOriginType()); operandStack.box(); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type)); mv.visitInsn(SWAP); mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false); } else { // rhs is normal value, set normal value operandStack.doGroovyCast(field.getOriginType()); mv.visitVarInsn(ALOAD, 0); operandStack.push(controller.getClassNode()); operandStack.swap(); mv.visitFieldInsn(PUTFIELD, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type)); } }
Example #12
Source File: AsmClassGenerator.java From groovy with Apache License 2.0 | 5 votes |
public void loadStaticField(final FieldExpression expression) { MethodVisitor mv = controller.getMethodVisitor(); FieldNode field = expression.getField(); ClassNode type = field.getType(); if (field.isHolder() && !controller.isInGeneratedFunctionConstructor()) { mv.visitFieldInsn(GETSTATIC, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type)); mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "get", "()Ljava/lang/Object;", false); controller.getOperandStack().push(ClassHelper.OBJECT_TYPE); } else { mv.visitFieldInsn(GETSTATIC, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type)); controller.getOperandStack().push(type); } }
Example #13
Source File: ASTNodeVisitor.java From groovy-language-server with Apache License 2.0 | 5 votes |
public void visitFieldExpression(FieldExpression node) { pushASTNode(node); try { super.visitFieldExpression(node); } finally { popASTNode(); } }
Example #14
Source File: Verifier.java From groovy with Apache License 2.0 | 5 votes |
private static boolean extractImplicitThis$0StmtIfInnerClassFromExpression(final List<Statement> stmts, final Statement bstmt) { Expression expr = ((ExpressionStatement) bstmt).getExpression(); if (expr instanceof BinaryExpression) { Expression lExpr = ((BinaryExpression) expr).getLeftExpression(); if (lExpr instanceof FieldExpression) { if ("this$0".equals(((FieldExpression) lExpr).getFieldName())) { stmts.remove(bstmt); // remove from here and let the caller reposition it return true; } } } return false; }
Example #15
Source File: VerifierCodeVisitor.java From groovy with Apache License 2.0 | 4 votes |
public void visitFieldExpression(FieldExpression expression) { if (!expression.getField().isSynthetic()) { assertValidIdentifier(expression.getFieldName(), "field name", expression); } super.visitFieldExpression(expression); }
Example #16
Source File: IfElseTest.java From groovy with Apache License 2.0 | 4 votes |
public void testLoop() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null)); classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null)); classNode.addProperty(new PropertyNode("result", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null)); BooleanExpression expression = new BooleanExpression( new BinaryExpression( new FieldExpression( new FieldNode("bar", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("==", 0, 0), new ConstantExpression("abc"))); Statement trueStatement = new ExpressionStatement( new BinaryExpression( new FieldExpression( new FieldNode("result", ACC_PRIVATE, ClassHelper.STRING_TYPE, classNode, ConstantExpression.NULL)), Token.newSymbol("=", 0, 0), new ConstantExpression("worked"))); Statement falseStatement = createPrintlnStatement(new ConstantExpression("false")); IfStatement statement = new IfStatement(expression, trueStatement, falseStatement); classNode.addMethod(new MethodNode("ifDemo", ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statement)); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); Object bean = fooClass.getDeclaredConstructor().newInstance(); assertTrue("Managed to create bean", bean != null); assertSetProperty(bean, "bar", "abc"); System.out.println("################ Now about to invoke method"); Object[] array = { }; InvokerHelper.invokeMethod(bean, "ifDemo", array); System.out.println("################ Done"); assertGetProperty(bean, "result", "worked"); }
Example #17
Source File: Verifier.java From groovy with Apache License 2.0 | 4 votes |
protected void addFieldInitialization(List list, List staticList, FieldNode fieldNode, boolean isEnumClassNode, List initStmtsAfterEnumValuesInit, Set explicitStaticPropsInEnum) { Expression expression = fieldNode.getInitialExpression(); if (expression != null) { final FieldExpression fe = new FieldExpression(fieldNode); if (fieldNode.getType().equals(ClassHelper.REFERENCE_TYPE) && ((fieldNode.getModifiers() & ACC_SYNTHETIC) != 0)) { fe.setUseReferenceDirectly(true); } ExpressionStatement statement = new ExpressionStatement( new BinaryExpression( fe, Token.newSymbol(Types.EQUAL, fieldNode.getLineNumber(), fieldNode.getColumnNumber()), expression)); if (fieldNode.isStatic()) { // GROOVY-3311: pre-defined constants added by groovy compiler for numbers/characters should be // initialized first so that code dependent on it does not see their values as empty Expression initialValueExpression = fieldNode.getInitialValueExpression(); Expression transformed = transformInlineConstants(initialValueExpression, fieldNode.getType()); if (transformed instanceof ConstantExpression) { ConstantExpression cexp = (ConstantExpression) transformed; cexp = transformToPrimitiveConstantIfPossible(cexp); if (fieldNode.isFinal() && ClassHelper.isStaticConstantInitializerType(cexp.getType()) && cexp.getType().equals(fieldNode.getType())) { fieldNode.setInitialValueExpression(transformed); return; // GROOVY-5150: primitive type constants will be initialized directly } staticList.add(0, statement); } else { staticList.add(statement); } fieldNode.setInitialValueExpression(null); // to avoid double initialization in case of several constructors /* * If it is a statement for an explicitly declared static field inside an enum, store its * reference. For enums, they need to be handled differently as such init statements should * come after the enum values have been initialized inside <clinit> block. GROOVY-3161. */ if (isEnumClassNode && explicitStaticPropsInEnum.contains(fieldNode.getName())) { initStmtsAfterEnumValuesInit.add(statement); } } else { list.add(statement); } } }
Example #18
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static FieldExpression fieldX(final ClassNode owner, final String fieldName) { return new FieldExpression(owner.getField(fieldName)); }
Example #19
Source File: GeneralUtils.java From groovy with Apache License 2.0 | 4 votes |
public static FieldExpression fieldX(final FieldNode fieldNode) { return new FieldExpression(fieldNode); }
Example #20
Source File: CodeVisitorSupport.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visitFieldExpression(FieldExpression expression) { }
Example #21
Source File: TransformingCodeVisitor.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visitFieldExpression(final FieldExpression expression) { super.visitFieldExpression(expression); trn.visitFieldExpression(expression); }
Example #22
Source File: SecureASTCustomizer.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visitFieldExpression(final FieldExpression expression) { assertExpressionAuthorized(expression); }
Example #23
Source File: ContextualClassCodeVisitor.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visitFieldExpression(final FieldExpression expression) { pushContext(expression); super.visitFieldExpression(expression); popContext(); }
Example #24
Source File: ASTFinder.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visitFieldExpression(final FieldExpression expression) { super.visitFieldExpression(expression); tryFind(FieldExpression.class, expression); }
Example #25
Source File: PathFinderVisitor.java From netbeans with Apache License 2.0 | 4 votes |
@Override public void visitFieldExpression(FieldExpression node) { if (isInside(node, line, column)) { super.visitFieldExpression(node); } }
Example #26
Source File: DriverCompilationCustomizer.java From arcusplatform with Apache License 2.0 | 4 votes |
@Override public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException { LOGGER.trace("Customize [phase: {} {}, classNode: {}]", source.getPhase(), source.getPhaseDescription(), classNode); if(classNode.getField("_HASH") == null) { String hash = hash(source); if(hash != null) { classNode.addField( "_HASH", Modifier.PUBLIC | Modifier.FINAL, new ClassNode(String.class), new ConstantExpression(hash) ); } } ClassNode groovyCapabilityDefinition = new ClassNode(GroovyCapabilityDefinition.class); for(CapabilityDefinition definition: capabilityRegistry.listCapabilityDefinitions()) { if(classNode.getProperty(definition.getCapabilityName()) != null) { continue; } if(!isDeviceCapability(definition)) { continue; } String fieldName = definition.getNamespace(); FieldNode field = classNode.addField( fieldName, Modifier.PRIVATE | Modifier.FINAL, groovyCapabilityDefinition, new StaticMethodCallExpression( new ClassNode(GroovyCapabilityDefinitionFactory.class), "create", new TupleExpression( new ConstantExpression(definition.getCapabilityName()), VariableExpression.THIS_EXPRESSION ) ) ); classNode.addProperty( definition.getCapabilityName(), Modifier.PUBLIC | Modifier.FINAL, groovyCapabilityDefinition, new FieldExpression(field), new ReturnStatement(new FieldExpression(field)), null ); } }
Example #27
Source File: GroovyCodeVisitor.java From groovy with Apache License 2.0 | votes |
void visitFieldExpression(FieldExpression expression);