src/share/classes/com/sun/tools/javac/comp/TransTypes.java

changeset 477
f23b985beb78
parent 360
62fb6cafa93b
child 511
7b69c7083a97
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/TransTypes.java	Fri Jan 15 15:37:13 2010 -0800
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/TransTypes.java	Tue Jan 19 14:28:45 2010 -0800
     1.3 @@ -61,8 +61,6 @@
     1.4          return instance;
     1.5      }
     1.6  
     1.7 -    private boolean debugJSR308;
     1.8 -
     1.9      private Names names;
    1.10      private Log log;
    1.11      private Symtab syms;
    1.12 @@ -71,6 +69,7 @@
    1.13      private boolean allowEnums;
    1.14      private Types types;
    1.15      private final Resolve resolve;
    1.16 +    private final TypeAnnotations typeAnnotations;
    1.17  
    1.18      /**
    1.19       * Flag to indicate whether or not to generate bridge methods.
    1.20 @@ -92,7 +91,7 @@
    1.21          types = Types.instance(context);
    1.22          make = TreeMaker.instance(context);
    1.23          resolve = Resolve.instance(context);
    1.24 -        debugJSR308 = Options.instance(context).get("TA:trans") != null;
    1.25 +        typeAnnotations = TypeAnnotations.instance(context);
    1.26      }
    1.27  
    1.28      /** A hashtable mapping bridge methods to the methods they override after
    1.29 @@ -440,8 +439,7 @@
    1.30      }
    1.31  
    1.32      public void visitClassDef(JCClassDecl tree) {
    1.33 -        new TypeAnnotationPositions().scan(tree);
    1.34 -        new TypeAnnotationLift().scan(tree);
    1.35 +        typeAnnotations.taFillAndLift(tree, true);
    1.36          translateClass(tree.sym);
    1.37          result = tree;
    1.38      }
    1.39 @@ -801,359 +799,4 @@
    1.40          pt = null;
    1.41          return translate(cdef, null);
    1.42      }
    1.43 -
    1.44 -    private class TypeAnnotationPositions extends TreeScanner {
    1.45 -
    1.46 -        private ListBuffer<JCTree> frames = ListBuffer.lb();
    1.47 -        private void push(JCTree t) { frames = frames.prepend(t); }
    1.48 -        private JCTree pop() { return frames.next(); }
    1.49 -        private JCTree peek() { return frames.first(); }
    1.50 -        private JCTree peek2() { return frames.toList().tail.head; }
    1.51 -
    1.52 -        @Override
    1.53 -        public void scan(JCTree tree) {
    1.54 -            push(tree);
    1.55 -            super.scan(tree);
    1.56 -            pop();
    1.57 -        }
    1.58 -
    1.59 -        private boolean inClass = false;
    1.60 -
    1.61 -        @Override
    1.62 -        public void visitClassDef(JCClassDecl tree) {
    1.63 -           if (!inClass) {
    1.64 -               // Do not recurse into nested and inner classes since
    1.65 -               // TransTypes.visitClassDef makes an invocation for each class
    1.66 -               // separately.
    1.67 -               inClass = true;
    1.68 -               try {
    1.69 -                   super.visitClassDef(tree);
    1.70 -               } finally {
    1.71 -                   inClass = false;
    1.72 -               }
    1.73 -           }
    1.74 -        }
    1.75 -
    1.76 -        private TypeAnnotationPosition resolveFrame(JCTree tree, JCTree frame,
    1.77 -                List<JCTree> path, TypeAnnotationPosition p) {
    1.78 -            switch (frame.getKind()) {
    1.79 -                case TYPE_CAST:
    1.80 -                    p.type = TargetType.TYPECAST;
    1.81 -                    p.pos = frame.pos;
    1.82 -                    return p;
    1.83 -
    1.84 -                case INSTANCE_OF:
    1.85 -                    p.type = TargetType.INSTANCEOF;
    1.86 -                    p.pos = frame.pos;
    1.87 -                    return p;
    1.88 -
    1.89 -                case NEW_CLASS:
    1.90 -                    p.type = TargetType.NEW;
    1.91 -                    p.pos = frame.pos;
    1.92 -                    return p;
    1.93 -
    1.94 -                case NEW_ARRAY:
    1.95 -                    p.type = TargetType.NEW;
    1.96 -                    p.pos = frame.pos;
    1.97 -                    return p;
    1.98 -
    1.99 -                case CLASS:
   1.100 -                    p.pos = frame.pos;
   1.101 -                    if (((JCClassDecl)frame).extending == tree) {
   1.102 -                        p.type = TargetType.CLASS_EXTENDS;
   1.103 -                        p.type_index = -1;
   1.104 -                    } else if (((JCClassDecl)frame).implementing.contains(tree)) {
   1.105 -                        p.type = TargetType.CLASS_EXTENDS;
   1.106 -                        p.type_index = ((JCClassDecl)frame).implementing.indexOf(tree);
   1.107 -                    } else if (((JCClassDecl)frame).typarams.contains(tree)) {
   1.108 -                        p.type = TargetType.CLASS_TYPE_PARAMETER;
   1.109 -                        p.parameter_index = ((JCClassDecl)frame).typarams.indexOf(tree);
   1.110 -                    } else
   1.111 -                        throw new AssertionError();
   1.112 -                    return p;
   1.113 -
   1.114 -                case METHOD: {
   1.115 -                    JCMethodDecl frameMethod = (JCMethodDecl)frame;
   1.116 -                    p.pos = frame.pos;
   1.117 -                    if (frameMethod.receiverAnnotations.contains(tree))
   1.118 -                        p.type = TargetType.METHOD_RECEIVER;
   1.119 -                    else if (frameMethod.thrown.contains(tree)) {
   1.120 -                        p.type = TargetType.THROWS;
   1.121 -                        p.type_index = frameMethod.thrown.indexOf(tree);
   1.122 -                    } else if (((JCMethodDecl)frame).restype == tree) {
   1.123 -                        p.type = TargetType.METHOD_RETURN_GENERIC_OR_ARRAY;
   1.124 -                    } else if (frameMethod.typarams.contains(tree)) {
   1.125 -                        p.type = TargetType.METHOD_TYPE_PARAMETER;
   1.126 -                        p.parameter_index = frameMethod.typarams.indexOf(tree);
   1.127 -                    } else
   1.128 -                        throw new AssertionError();
   1.129 -                    return p;
   1.130 -                }
   1.131 -                case MEMBER_SELECT: {
   1.132 -                    JCFieldAccess fieldFrame = (JCFieldAccess)frame;
   1.133 -                    if (fieldFrame.name == names._class) {
   1.134 -                        p.type = TargetType.CLASS_LITERAL;
   1.135 -                        if (fieldFrame.selected instanceof JCAnnotatedType) {
   1.136 -                            p.pos = TreeInfo.typeIn(fieldFrame).pos;
   1.137 -                        } else if (fieldFrame.selected instanceof JCArrayTypeTree) {
   1.138 -                            p.pos = fieldFrame.selected.pos;
   1.139 -                        }
   1.140 -                    } else
   1.141 -                        throw new AssertionError();
   1.142 -                    return p;
   1.143 -                }
   1.144 -                case PARAMETERIZED_TYPE: {
   1.145 -                    TypeAnnotationPosition nextP;
   1.146 -                    if (((JCTypeApply)frame).clazz == tree)
   1.147 -                        nextP = p; // generic: RAW; noop
   1.148 -                    else if (((JCTypeApply)frame).arguments.contains(tree))
   1.149 -                        p.location = p.location.prepend(
   1.150 -                                ((JCTypeApply)frame).arguments.indexOf(tree));
   1.151 -                    else
   1.152 -                        throw new AssertionError();
   1.153 -
   1.154 -                    List<JCTree> newPath = path.tail;
   1.155 -                    return resolveFrame(newPath.head, newPath.tail.head, newPath, p);
   1.156 -                }
   1.157 -
   1.158 -                case ARRAY_TYPE: {
   1.159 -                    p.location = p.location.prepend(0);
   1.160 -                    List<JCTree> newPath = path.tail;
   1.161 -                    return resolveFrame(newPath.head, newPath.tail.head, newPath, p);
   1.162 -                }
   1.163 -
   1.164 -                case TYPE_PARAMETER:
   1.165 -                    if (path.tail.tail.head.getTag() == JCTree.CLASSDEF) {
   1.166 -                        JCClassDecl clazz = (JCClassDecl)path.tail.tail.head;
   1.167 -                        p.type = TargetType.CLASS_TYPE_PARAMETER_BOUND;
   1.168 -                        p.parameter_index = clazz.typarams.indexOf(path.tail.head);
   1.169 -                        p.bound_index = ((JCTypeParameter)frame).bounds.indexOf(tree);
   1.170 -                    } else if (path.tail.tail.head.getTag() == JCTree.METHODDEF) {
   1.171 -                        JCMethodDecl method = (JCMethodDecl)path.tail.tail.head;
   1.172 -                        p.type = TargetType.METHOD_TYPE_PARAMETER_BOUND;
   1.173 -                        p.parameter_index = method.typarams.indexOf(path.tail.head);
   1.174 -                        p.bound_index = ((JCTypeParameter)frame).bounds.indexOf(tree);
   1.175 -                    } else
   1.176 -                        throw new AssertionError();
   1.177 -                    p.pos = frame.pos;
   1.178 -                    return p;
   1.179 -
   1.180 -                case VARIABLE:
   1.181 -                    VarSymbol v = ((JCVariableDecl)frame).sym;
   1.182 -                    p.pos = frame.pos;
   1.183 -                    switch (v.getKind()) {
   1.184 -                        case LOCAL_VARIABLE:
   1.185 -                            p.type = TargetType.LOCAL_VARIABLE; break;
   1.186 -                        case FIELD:
   1.187 -                            p.type = TargetType.FIELD_GENERIC_OR_ARRAY; break;
   1.188 -                        case PARAMETER:
   1.189 -                            p.type = TargetType.METHOD_PARAMETER_GENERIC_OR_ARRAY;
   1.190 -                            p.parameter_index = methodParamIndex(path, frame);
   1.191 -                            break;
   1.192 -                        default: throw new AssertionError();
   1.193 -                    }
   1.194 -                    return p;
   1.195 -
   1.196 -                case ANNOTATED_TYPE: {
   1.197 -                    List<JCTree> newPath = path.tail;
   1.198 -                    return resolveFrame(newPath.head, newPath.tail.head,
   1.199 -                            newPath, p);
   1.200 -                }
   1.201 -
   1.202 -                case METHOD_INVOCATION: {
   1.203 -                    JCMethodInvocation invocation = (JCMethodInvocation)frame;
   1.204 -                    if (!invocation.typeargs.contains(tree))
   1.205 -                        throw new AssertionError("{" + tree + "} is not an argument in the invocation: " + invocation);
   1.206 -                    p.type = TargetType.METHOD_TYPE_ARGUMENT;
   1.207 -                    p.pos = invocation.pos;
   1.208 -                    p.type_index = invocation.typeargs.indexOf(tree);
   1.209 -                    return p;
   1.210 -                }
   1.211 -
   1.212 -                case EXTENDS_WILDCARD:
   1.213 -                case SUPER_WILDCARD: {
   1.214 -                    p.type = TargetType.WILDCARD_BOUND;
   1.215 -                    List<JCTree> newPath = path.tail;
   1.216 -
   1.217 -                    TypeAnnotationPosition wildcard =
   1.218 -                        resolveFrame(newPath.head, newPath.tail.head, newPath,
   1.219 -                                new TypeAnnotationPosition());
   1.220 -                    if (!wildcard.location.isEmpty())
   1.221 -                        wildcard.type = wildcard.type.getGenericComplement();
   1.222 -                    p.wildcard_position = wildcard;
   1.223 -                    p.pos = frame.pos;
   1.224 -                    return p;
   1.225 -                }
   1.226 -            }
   1.227 -            return p;
   1.228 -        }
   1.229 -
   1.230 -        @Override
   1.231 -        public void visitApply(JCMethodInvocation tree) {
   1.232 -            scan(tree.meth);
   1.233 -            scan(tree.typeargs);
   1.234 -            scan(tree.args);
   1.235 -        }
   1.236 -
   1.237 -        private void setTypeAnnotationPos(List<JCTypeAnnotation> annotations, TypeAnnotationPosition position) {
   1.238 -            for (JCTypeAnnotation anno : annotations) {
   1.239 -                anno.annotation_position = position;
   1.240 -                anno.attribute_field.position = position;
   1.241 -            }
   1.242 -        }
   1.243 -
   1.244 -        @Override
   1.245 -        public void visitNewArray(JCNewArray tree) {
   1.246 -            findPosition(tree, tree, tree.annotations);
   1.247 -            int dimAnnosCount = tree.dimAnnotations.size();
   1.248 -
   1.249 -            // handle annotations associated with dimentions
   1.250 -            for (int i = 0; i < dimAnnosCount; ++i) {
   1.251 -                TypeAnnotationPosition p = new TypeAnnotationPosition();
   1.252 -                p.type = TargetType.NEW_GENERIC_OR_ARRAY;
   1.253 -                p.pos = tree.pos;
   1.254 -                p.location = p.location.append(i);
   1.255 -                setTypeAnnotationPos(tree.dimAnnotations.get(i), p);
   1.256 -            }
   1.257 -
   1.258 -            // handle "free" annotations
   1.259 -            int i = dimAnnosCount == 0 ? 0 : dimAnnosCount - 1;
   1.260 -            JCExpression elemType = tree.elemtype;
   1.261 -            while (elemType != null) {
   1.262 -                if (elemType.getTag() == JCTree.ANNOTATED_TYPE) {
   1.263 -                    JCAnnotatedType at = (JCAnnotatedType)elemType;
   1.264 -                    TypeAnnotationPosition p = new TypeAnnotationPosition();
   1.265 -                    p.type = TargetType.NEW_GENERIC_OR_ARRAY;
   1.266 -                    p.pos = tree.pos;
   1.267 -                    p.location = p.location.append(i);
   1.268 -                    setTypeAnnotationPos(at.annotations, p);
   1.269 -                    elemType = at.underlyingType;
   1.270 -                } else if (elemType.getTag() == JCTree.TYPEARRAY) {
   1.271 -                    ++i;
   1.272 -                    elemType = ((JCArrayTypeTree)elemType).elemtype;
   1.273 -                } else
   1.274 -                    break;
   1.275 -            }
   1.276 -
   1.277 -            // find annotations locations of initializer elements
   1.278 -            scan(tree.elems);
   1.279 -        }
   1.280 -
   1.281 -        @Override
   1.282 -        public void visitAnnotatedType(JCAnnotatedType tree) {
   1.283 -            findPosition(tree, peek2(), tree.annotations);
   1.284 -            super.visitAnnotatedType(tree);
   1.285 -        }
   1.286 -
   1.287 -        @Override
   1.288 -        public void visitMethodDef(JCMethodDecl tree) {
   1.289 -            TypeAnnotationPosition p = new TypeAnnotationPosition();
   1.290 -            p.type = TargetType.METHOD_RECEIVER;
   1.291 -            setTypeAnnotationPos(tree.receiverAnnotations, p);
   1.292 -            super.visitMethodDef(tree);
   1.293 -        }
   1.294 -        @Override
   1.295 -        public void visitTypeParameter(JCTypeParameter tree) {
   1.296 -            findPosition(tree, peek2(), tree.annotations);
   1.297 -            super.visitTypeParameter(tree);
   1.298 -        }
   1.299 -
   1.300 -        void findPosition(JCTree tree, JCTree frame, List<JCTypeAnnotation> annotations) {
   1.301 -            if (!annotations.isEmpty()) {
   1.302 -                TypeAnnotationPosition p =
   1.303 -                        resolveFrame(tree, frame, frames.toList(),
   1.304 -                                new TypeAnnotationPosition());
   1.305 -                if (!p.location.isEmpty())
   1.306 -                    p.type = p.type.getGenericComplement();
   1.307 -                setTypeAnnotationPos(annotations, p);
   1.308 -                if (debugJSR308) {
   1.309 -                    System.out.println("trans: " + tree);
   1.310 -                    System.out.println("  target: " + p);
   1.311 -                }
   1.312 -            }
   1.313 -        }
   1.314 -
   1.315 -        private int methodParamIndex(List<JCTree> path, JCTree param) {
   1.316 -            List<JCTree> curr = path;
   1.317 -            if (curr.head != param)
   1.318 -                curr = path.tail;
   1.319 -            JCMethodDecl method = (JCMethodDecl)curr.tail.head;
   1.320 -            return method.params.indexOf(param);
   1.321 -        }
   1.322 -    }
   1.323 -
   1.324 -    private class TypeAnnotationLift extends TreeScanner {
   1.325 -        List<Attribute.TypeCompound> recordedTypeAnnotations = List.nil();
   1.326 -
   1.327 -        boolean isInner = false;
   1.328 -        @Override
   1.329 -        public void visitClassDef(JCClassDecl tree) {
   1.330 -            if (isInner) {
   1.331 -                // tree is an inner class tree.  stop now.
   1.332 -                // TransTypes.visitClassDef makes an invocation for each class
   1.333 -                // seperately.
   1.334 -                return;
   1.335 -            }
   1.336 -            isInner = true;
   1.337 -            List<Attribute.TypeCompound> prevTAs = recordedTypeAnnotations;
   1.338 -            recordedTypeAnnotations = List.nil();
   1.339 -            try {
   1.340 -                super.visitClassDef(tree);
   1.341 -            } finally {
   1.342 -                tree.sym.typeAnnotations = tree.sym.typeAnnotations.appendList(recordedTypeAnnotations);
   1.343 -                recordedTypeAnnotations = prevTAs;
   1.344 -            }
   1.345 -        }
   1.346 -
   1.347 -        @Override
   1.348 -        public void visitMethodDef(JCMethodDecl tree) {
   1.349 -            List<Attribute.TypeCompound> prevTAs = recordedTypeAnnotations;
   1.350 -            recordedTypeAnnotations = List.nil();
   1.351 -            try {
   1.352 -                super.visitMethodDef(tree);
   1.353 -            } finally {
   1.354 -                tree.sym.typeAnnotations = tree.sym.typeAnnotations.appendList(recordedTypeAnnotations);
   1.355 -                recordedTypeAnnotations = prevTAs;
   1.356 -            }
   1.357 -        }
   1.358 -
   1.359 -        @Override
   1.360 -        public void visitVarDef(JCVariableDecl tree) {
   1.361 -            List<Attribute.TypeCompound> prevTAs = recordedTypeAnnotations;
   1.362 -            recordedTypeAnnotations = List.nil();
   1.363 -            ElementKind kind = tree.sym.getKind();
   1.364 -            if (kind == ElementKind.LOCAL_VARIABLE && tree.mods.annotations.nonEmpty()) {
   1.365 -                // need to lift the annotations
   1.366 -                TypeAnnotationPosition position = new TypeAnnotationPosition();
   1.367 -                position.pos = tree.pos;
   1.368 -                position.type = TargetType.LOCAL_VARIABLE;
   1.369 -                for (Attribute.Compound attribute : tree.sym.attributes_field) {
   1.370 -                    Attribute.TypeCompound tc =
   1.371 -                        new Attribute.TypeCompound(attribute.type, attribute.values, position);
   1.372 -                    recordedTypeAnnotations = recordedTypeAnnotations.append(tc);
   1.373 -                }
   1.374 -            }
   1.375 -            try {
   1.376 -                super.visitVarDef(tree);
   1.377 -            } finally {
   1.378 -                if (kind.isField() || kind == ElementKind.LOCAL_VARIABLE)
   1.379 -                    tree.sym.typeAnnotations = tree.sym.typeAnnotations.appendList(recordedTypeAnnotations);
   1.380 -                recordedTypeAnnotations = kind.isField() ? prevTAs : prevTAs.appendList(recordedTypeAnnotations);
   1.381 -            }
   1.382 -        }
   1.383 -
   1.384 -        @Override
   1.385 -        public void visitApply(JCMethodInvocation tree) {
   1.386 -            scan(tree.meth);
   1.387 -            scan(tree.typeargs);
   1.388 -            scan(tree.args);
   1.389 -        }
   1.390 -
   1.391 -        public void visitAnnotation(JCAnnotation tree) {
   1.392 -            if (tree instanceof JCTypeAnnotation)
   1.393 -                recordedTypeAnnotations = recordedTypeAnnotations.append(((JCTypeAnnotation)tree).attribute_field);
   1.394 -            super.visitAnnotation(tree);
   1.395 -        }
   1.396 -    }
   1.397 -
   1.398  }

mercurial