Java Code Examples for org.codehaus.groovy.syntax.Token#newSymbol()
The following examples show how to use
org.codehaus.groovy.syntax.Token#newSymbol() .
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: NAryOperationRewriter.java From groovy with Apache License 2.0 | 6 votes |
private Expression transformBinaryExpression(final BinaryExpression exp) { final int op = exp.getOperation().getType(); int token = TokenUtil.removeAssignment(op); if (token == op) { // no transform needed return super.transform(exp); } BinaryExpression operation = new BinaryExpression( exp.getLeftExpression(), Token.newSymbol(token, -1, -1), exp.getRightExpression() ); operation.setSourcePosition(exp); BinaryExpression result = new BinaryExpression( exp.getLeftExpression(), Token.newSymbol(EQUAL, -1, -1), operation ); result.setSourcePosition(exp); return result; }
Example 2
Source File: TraitComposer.java From groovy with Apache License 2.0 | 5 votes |
private static Statement createSuperFallback(MethodNode forwarderMethod, ClassNode returnType) { ArgumentListExpression args = new ArgumentListExpression(); Parameter[] forwarderMethodParameters = forwarderMethod.getParameters(); for (final Parameter forwarderMethodParameter : forwarderMethodParameters) { args.addExpression(new VariableExpression(forwarderMethodParameter)); } BinaryExpression instanceOfExpr = new BinaryExpression(new VariableExpression("this"), Token.newSymbol(Types.KEYWORD_INSTANCEOF, -1, -1), new ClassExpression(Traits.GENERATED_PROXY_CLASSNODE)); MethodCallExpression superCall = new MethodCallExpression( new VariableExpression("super"), forwarderMethod.getName(), args ); superCall.setImplicitThis(false); CastExpression proxyReceiver = new CastExpression(Traits.GENERATED_PROXY_CLASSNODE, new VariableExpression("this")); MethodCallExpression getProxy = new MethodCallExpression(proxyReceiver, "getProxyTarget", ArgumentListExpression.EMPTY_ARGUMENTS); getProxy.setImplicitThis(true); StaticMethodCallExpression proxyCall = new StaticMethodCallExpression( ClassHelper.make(InvokerHelper.class), "invokeMethod", new ArgumentListExpression(getProxy, new ConstantExpression(forwarderMethod.getName()), new ArrayExpression(ClassHelper.OBJECT_TYPE, args.getExpressions())) ); IfStatement stmt = new IfStatement( new BooleanExpression(instanceOfExpr), new ExpressionStatement(new CastExpression(returnType,proxyCall)), new ExpressionStatement(superCall) ); return stmt; }
Example 3
Source File: TraitReceiverTransformer.java From groovy with Apache License 2.0 | 5 votes |
private TernaryExpression createStaticReceiver(final Expression receiver) { return new TernaryExpression( new BooleanExpression(new BinaryExpression( receiver, Token.newSymbol(Types.KEYWORD_INSTANCEOF, -1, -1), new ClassExpression(ClassHelper.CLASS_Type) )), receiver, new MethodCallExpression(createFieldHelperReceiver(), "getClass", ArgumentListExpression.EMPTY_ARGUMENTS) ); }
Example 4
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 5
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"); }