src/share/classes/com/sun/tools/javac/model/JavacTypes.java

Tue, 07 Sep 2010 17:31:54 +0100

author
mcimadamore
date
Tue, 07 Sep 2010 17:31:54 +0100
changeset 673
7ae4016c5938
parent 581
f2fdd52e4e87
child 675
12d8f7e417fd
permissions
-rw-r--r--

6337171: javac should create bridge methods when type variable bounds restricted
Summary: javac should add synthetic overrides for inherited abstract methods in order to preserve binary compatibility
Reviewed-by: jjg

     1 /*
     2  * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.javac.model;
    28 import java.util.List;
    29 import java.util.Set;
    30 import java.util.EnumSet;
    31 import javax.lang.model.element.*;
    32 import javax.lang.model.type.*;
    33 import com.sun.tools.javac.code.*;
    34 import com.sun.tools.javac.code.Symbol.*;
    35 import com.sun.tools.javac.util.*;
    37 /**
    38  * Utility methods for operating on types.
    39  *
    40  * <p><b>This is NOT part of any supported API.
    41  * If you write code that depends on this, you do so at your own
    42  * risk.  This code and its internal interfaces are subject to change
    43  * or deletion without notice.</b></p>
    44  */
    45 public class JavacTypes implements javax.lang.model.util.Types {
    47     private Symtab syms;
    48     private Types types;
    50     private static final Context.Key<JavacTypes> KEY =
    51             new Context.Key<JavacTypes>();
    53     public static JavacTypes instance(Context context) {
    54         JavacTypes instance = context.get(KEY);
    55         if (instance == null) {
    56             instance = new JavacTypes(context);
    57             context.put(KEY, instance);
    58         }
    59         return instance;
    60     }
    62     /**
    63      * Public for use only by JavacProcessingEnvironment
    64      */
    65     // TODO JavacTypes constructor should be protected
    66     public JavacTypes(Context context) {
    67         setContext(context);
    68     }
    70     /**
    71      * Use a new context.  May be called from outside to update
    72      * internal state for a new annotation-processing round.
    73      * This instance is *not* then registered with the new context.
    74      */
    75     public void setContext(Context context) {
    76         syms = Symtab.instance(context);
    77         types = Types.instance(context);
    78     }
    80     public Element asElement(TypeMirror t) {
    81         Type type = cast(Type.class, t);
    82         if (type.tag != TypeTags.CLASS && type.tag != TypeTags.TYPEVAR)
    83             return null;
    84         return type.asElement();
    85     }
    87     public boolean isSameType(TypeMirror t1, TypeMirror t2) {
    88         return types.isSameType((Type) t1, (Type) t2);
    89     }
    91     public boolean isSubtype(TypeMirror t1, TypeMirror t2) {
    92         validateTypeNotIn(t1, EXEC_OR_PKG);
    93         validateTypeNotIn(t2, EXEC_OR_PKG);
    94         return types.isSubtype((Type) t1, (Type) t2);
    95     }
    97     public boolean isAssignable(TypeMirror t1, TypeMirror t2) {
    98         validateTypeNotIn(t1, EXEC_OR_PKG);
    99         validateTypeNotIn(t2, EXEC_OR_PKG);
   100         return types.isAssignable((Type) t1, (Type) t2);
   101     }
   103     public boolean contains(TypeMirror t1, TypeMirror t2) {
   104         validateTypeNotIn(t1, EXEC_OR_PKG);
   105         validateTypeNotIn(t2, EXEC_OR_PKG);
   106         return ((Type) t1).contains((Type) t2);
   107     }
   109     public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
   110         return types.isSubSignature((Type) m1, (Type) m2);
   111     }
   113     public List<Type> directSupertypes(TypeMirror t) {
   114         validateTypeNotIn(t, EXEC_OR_PKG);
   115         Type type = (Type) t;
   116         Type sup = types.supertype(type);
   117         return (sup == Type.noType || sup == type || sup == null)
   118               ? types.interfaces(type)
   119               : types.interfaces(type).prepend(sup);
   120     }
   122     public TypeMirror erasure(TypeMirror t) {
   123         if (t.getKind() == TypeKind.PACKAGE)
   124             throw new IllegalArgumentException(t.toString());
   125         return types.erasure((Type) t);
   126     }
   128     public TypeElement boxedClass(PrimitiveType p) {
   129         return types.boxedClass((Type) p);
   130     }
   132     public PrimitiveType unboxedType(TypeMirror t) {
   133         if (t.getKind() != TypeKind.DECLARED)
   134             throw new IllegalArgumentException(t.toString());
   135         Type unboxed = types.unboxedType((Type) t);
   136         if (! unboxed.isPrimitive())    // only true primitives, not void
   137             throw new IllegalArgumentException(t.toString());
   138         return unboxed;
   139     }
   141     public TypeMirror capture(TypeMirror t) {
   142         validateTypeNotIn(t, EXEC_OR_PKG);
   143         return types.capture((Type) t);
   144     }
   146     public PrimitiveType getPrimitiveType(TypeKind kind) {
   147         switch (kind) {
   148         case BOOLEAN:   return syms.booleanType;
   149         case BYTE:      return syms.byteType;
   150         case SHORT:     return syms.shortType;
   151         case INT:       return syms.intType;
   152         case LONG:      return syms.longType;
   153         case CHAR:      return syms.charType;
   154         case FLOAT:     return syms.floatType;
   155         case DOUBLE:    return syms.doubleType;
   156         default:
   157             throw new IllegalArgumentException("Not a primitive type: " + kind);
   158         }
   159     }
   161     public NullType getNullType() {
   162         return (NullType) syms.botType;
   163     }
   165     public NoType getNoType(TypeKind kind) {
   166         switch (kind) {
   167         case VOID:      return syms.voidType;
   168         case NONE:      return Type.noType;
   169         default:
   170             throw new IllegalArgumentException(kind.toString());
   171         }
   172     }
   174     public ArrayType getArrayType(TypeMirror componentType) {
   175         switch (componentType.getKind()) {
   176         case VOID:
   177         case EXECUTABLE:
   178         case WILDCARD:  // heh!
   179         case PACKAGE:
   180             throw new IllegalArgumentException(componentType.toString());
   181         }
   182         return new Type.ArrayType((Type) componentType, syms.arrayClass);
   183     }
   185     public WildcardType getWildcardType(TypeMirror extendsBound,
   186                                         TypeMirror superBound) {
   187         BoundKind bkind;
   188         Type bound;
   189         if (extendsBound == null && superBound == null) {
   190             bkind = BoundKind.UNBOUND;
   191             bound = syms.objectType;
   192         } else if (superBound == null) {
   193             bkind = BoundKind.EXTENDS;
   194             bound = (Type) extendsBound;
   195         } else if (extendsBound == null) {
   196             bkind = BoundKind.SUPER;
   197             bound = (Type) superBound;
   198         } else {
   199             throw new IllegalArgumentException(
   200                     "Extends and super bounds cannot both be provided");
   201         }
   202         switch (bound.getKind()) {
   203         case ARRAY:
   204         case DECLARED:
   205         case ERROR:
   206         case TYPEVAR:
   207             return new Type.WildcardType(bound, bkind, syms.boundClass);
   208         default:
   209             throw new IllegalArgumentException(bound.toString());
   210         }
   211     }
   213     public DeclaredType getDeclaredType(TypeElement typeElem,
   214                                         TypeMirror... typeArgs) {
   215         ClassSymbol sym = (ClassSymbol) typeElem;
   217         if (typeArgs.length == 0)
   218             return (DeclaredType) sym.erasure(types);
   219         if (sym.type.getEnclosingType().isParameterized())
   220             throw new IllegalArgumentException(sym.toString());
   222         return getDeclaredType0(sym.type.getEnclosingType(), sym, typeArgs);
   223     }
   225     public DeclaredType getDeclaredType(DeclaredType enclosing,
   226                                         TypeElement typeElem,
   227                                         TypeMirror... typeArgs) {
   228         if (enclosing == null)
   229             return getDeclaredType(typeElem, typeArgs);
   231         ClassSymbol sym = (ClassSymbol) typeElem;
   232         Type outer = (Type) enclosing;
   234         if (outer.tsym != sym.owner.enclClass())
   235             throw new IllegalArgumentException(enclosing.toString());
   236         if (!outer.isParameterized())
   237             return getDeclaredType(typeElem, typeArgs);
   239         return getDeclaredType0(outer, sym, typeArgs);
   240     }
   241     // where
   242         private DeclaredType getDeclaredType0(Type outer,
   243                                               ClassSymbol sym,
   244                                               TypeMirror... typeArgs) {
   245             if (typeArgs.length != sym.type.getTypeArguments().length())
   246                 throw new IllegalArgumentException(
   247                 "Incorrect number of type arguments");
   249             ListBuffer<Type> targs = new ListBuffer<Type>();
   250             for (TypeMirror t : typeArgs) {
   251                 if (!(t instanceof ReferenceType || t instanceof WildcardType))
   252                     throw new IllegalArgumentException(t.toString());
   253                 targs.append((Type) t);
   254             }
   255             // TODO: Would like a way to check that type args match formals.
   257             return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym);
   258         }
   260     /**
   261      * Returns the type of an element when that element is viewed as
   262      * a member of, or otherwise directly contained by, a given type.
   263      * For example,
   264      * when viewed as a member of the parameterized type {@code Set<String>},
   265      * the {@code Set.add} method is an {@code ExecutableType}
   266      * whose parameter is of type {@code String}.
   267      *
   268      * @param containing  the containing type
   269      * @param element     the element
   270      * @return the type of the element as viewed from the containing type
   271      * @throws IllegalArgumentException if the element is not a valid one
   272      *          for the given type
   273      */
   274     public TypeMirror asMemberOf(DeclaredType containing, Element element) {
   275         Type site = (Type)containing;
   276         Symbol sym = (Symbol)element;
   277         if (types.asSuper(site, sym.getEnclosingElement()) == null)
   278             throw new IllegalArgumentException(sym + "@" + site);
   279         return types.memberType(site, sym);
   280     }
   283     private static final Set<TypeKind> EXEC_OR_PKG =
   284             EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE);
   286     /**
   287      * Throws an IllegalArgumentException if a type's kind is one of a set.
   288      */
   289     private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
   290         if (invalidKinds.contains(t.getKind()))
   291             throw new IllegalArgumentException(t.toString());
   292     }
   294     /**
   295      * Returns an object cast to the specified type.
   296      * @throws NullPointerException if the object is {@code null}
   297      * @throws IllegalArgumentException if the object is of the wrong type
   298      */
   299     private static <T> T cast(Class<T> clazz, Object o) {
   300         if (! clazz.isInstance(o))
   301             throw new IllegalArgumentException(o.toString());
   302         return clazz.cast(o);
   303     }
   304 }

mercurial