|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.codehaus.groovy.classgen; |
| 20 | + |
| 21 | +import org.codehaus.groovy.ast.ClassCodeVisitorSupport; |
| 22 | +import org.codehaus.groovy.ast.ClassHelper; |
| 23 | +import org.codehaus.groovy.ast.ClassNode; |
| 24 | +import org.codehaus.groovy.ast.FieldNode; |
| 25 | +import org.codehaus.groovy.ast.InnerClassNode; |
| 26 | +import org.codehaus.groovy.ast.Parameter; |
| 27 | +import org.codehaus.groovy.ast.expr.ArgumentListExpression; |
| 28 | +import org.codehaus.groovy.ast.expr.BinaryExpression; |
| 29 | +import org.codehaus.groovy.ast.expr.ConstantExpression; |
| 30 | +import org.codehaus.groovy.ast.expr.Expression; |
| 31 | +import org.codehaus.groovy.ast.expr.FieldExpression; |
| 32 | +import org.codehaus.groovy.ast.expr.GStringExpression; |
| 33 | +import org.codehaus.groovy.ast.expr.MethodCallExpression; |
| 34 | +import org.codehaus.groovy.ast.expr.PropertyExpression; |
| 35 | +import org.codehaus.groovy.ast.expr.SpreadExpression; |
| 36 | +import org.codehaus.groovy.ast.expr.VariableExpression; |
| 37 | +import org.codehaus.groovy.ast.stmt.BlockStatement; |
| 38 | +import org.codehaus.groovy.ast.stmt.ExpressionStatement; |
| 39 | +import org.codehaus.groovy.ast.stmt.ReturnStatement; |
| 40 | +import org.codehaus.groovy.syntax.Token; |
| 41 | +import org.codehaus.groovy.syntax.Types; |
| 42 | +import groovyjarjarasm.asm.Opcodes; |
| 43 | + |
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.List; |
| 46 | + |
| 47 | +public abstract class InnerClassVisitorHelper extends ClassCodeVisitorSupport { |
| 48 | + |
| 49 | + protected static void setPropertyGetterDispatcher(BlockStatement block, Expression thiz, Parameter[] parameters) { |
| 50 | + List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>(); |
| 51 | + gStringStrings.add(new ConstantExpression("")); |
| 52 | + gStringStrings.add(new ConstantExpression("")); |
| 53 | + List<Expression> gStringValues = new ArrayList<Expression>(); |
| 54 | + gStringValues.add(new VariableExpression(parameters[0])); |
| 55 | + block.addStatement( |
| 56 | + new ReturnStatement( |
| 57 | + new PropertyExpression( |
| 58 | + thiz, |
| 59 | + new GStringExpression("$name", gStringStrings, gStringValues) |
| 60 | + ) |
| 61 | + ) |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + protected static void setPropertySetterDispatcher(BlockStatement block, Expression thiz, Parameter[] parameters) { |
| 66 | + List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>(); |
| 67 | + gStringStrings.add(new ConstantExpression("")); |
| 68 | + gStringStrings.add(new ConstantExpression("")); |
| 69 | + List<Expression> gStringValues = new ArrayList<Expression>(); |
| 70 | + gStringValues.add(new VariableExpression(parameters[0])); |
| 71 | + block.addStatement( |
| 72 | + new ExpressionStatement( |
| 73 | + new BinaryExpression( |
| 74 | + new PropertyExpression( |
| 75 | + thiz, |
| 76 | + new GStringExpression("$name", gStringStrings, gStringValues) |
| 77 | + ), |
| 78 | + Token.newSymbol(Types.ASSIGN, -1, -1), |
| 79 | + new VariableExpression(parameters[1]) |
| 80 | + ) |
| 81 | + ) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + protected static void setMethodDispatcherCode(BlockStatement block, Expression thiz, Parameter[] parameters) { |
| 86 | + List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>(); |
| 87 | + gStringStrings.add(new ConstantExpression("")); |
| 88 | + gStringStrings.add(new ConstantExpression("")); |
| 89 | + List<Expression> gStringValues = new ArrayList<Expression>(); |
| 90 | + gStringValues.add(new VariableExpression(parameters[0])); |
| 91 | + block.addStatement( |
| 92 | + new ReturnStatement( |
| 93 | + new MethodCallExpression( |
| 94 | + thiz, |
| 95 | + new GStringExpression("$name", gStringStrings, gStringValues), |
| 96 | + new ArgumentListExpression( |
| 97 | + new SpreadExpression(new VariableExpression(parameters[1])) |
| 98 | + ) |
| 99 | + ) |
| 100 | + ) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + protected static boolean isStatic(InnerClassNode node) { |
| 105 | + return node.getDeclaredField("this$0") == null; |
| 106 | + } |
| 107 | + |
| 108 | + protected static ClassNode getClassNode(ClassNode node, boolean isStatic) { |
| 109 | + if (isStatic) node = ClassHelper.CLASS_Type; |
| 110 | + return node; |
| 111 | + } |
| 112 | + |
| 113 | + protected static int getObjectDistance(ClassNode node) { |
| 114 | + int count = 0; |
| 115 | + while (node != null && node != ClassHelper.OBJECT_TYPE) { |
| 116 | + count++; |
| 117 | + node = node.getSuperClass(); |
| 118 | + } |
| 119 | + return count; |
| 120 | + } |
| 121 | + |
| 122 | + protected static void addFieldInit(Parameter p, FieldNode fn, BlockStatement block) { |
| 123 | + VariableExpression ve = new VariableExpression(p); |
| 124 | + FieldExpression fe = new FieldExpression(fn); |
| 125 | + block.addStatement(new ExpressionStatement( |
| 126 | + new BinaryExpression(fe, Token.newSymbol(Types.ASSIGN, -1, -1), ve) |
| 127 | + )); |
| 128 | + } |
| 129 | + |
| 130 | + protected static boolean shouldHandleImplicitThisForInnerClass(ClassNode cn) { |
| 131 | + /* GRECLIPSE edit |
| 132 | + if (cn.isEnum() || cn.isInterface()) return false; |
| 133 | + if ((cn.getModifiers() & Opcodes.ACC_STATIC) != 0) return false; |
| 134 | +
|
| 135 | + if (!(cn instanceof InnerClassNode)) return false; |
| 136 | + InnerClassNode innerClass = (InnerClassNode) cn; |
| 137 | + // scope != null means aic, we don't handle that here |
| 138 | + if (innerClass.getVariableScope() != null) return false; |
| 139 | + // static inner classes don't need this$0 |
| 140 | + return (innerClass.getModifiers() & Opcodes.ACC_STATIC) == 0; |
| 141 | + */ |
| 142 | + final int explicitOrImplicitStatic = Opcodes.ACC_STATIC | Opcodes.ACC_INTERFACE | Opcodes.ACC_ENUM; |
| 143 | + return (cn.getModifiers() & explicitOrImplicitStatic) == 0 && (cn instanceof InnerClassNode && !((InnerClassNode) cn).isAnonymous()); |
| 144 | + // GRECLIPSE end |
| 145 | + } |
| 146 | +} |
0 commit comments