duke@1: /* ohair@554: * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.model; duke@1: duke@1: import java.lang.annotation.Annotation; duke@1: import java.lang.annotation.Inherited; duke@1: import java.util.Map; duke@1: import javax.lang.model.SourceVersion; duke@1: import javax.lang.model.element.*; duke@1: import javax.lang.model.type.DeclaredType; duke@1: import javax.lang.model.util.Elements; duke@1: import javax.tools.JavaFileObject; duke@1: import com.sun.tools.javac.code.*; duke@1: import com.sun.tools.javac.code.Symbol.*; duke@1: import com.sun.tools.javac.code.TypeTags; duke@1: import com.sun.tools.javac.comp.AttrContext; duke@1: import com.sun.tools.javac.comp.Enter; duke@1: import com.sun.tools.javac.comp.Env; duke@1: import com.sun.tools.javac.main.JavaCompiler; duke@1: import com.sun.tools.javac.processing.PrintingProcessor; duke@1: import com.sun.tools.javac.tree.JCTree; duke@1: import com.sun.tools.javac.tree.JCTree.*; duke@1: import com.sun.tools.javac.tree.TreeInfo; duke@1: import com.sun.tools.javac.tree.TreeScanner; jjg@113: import com.sun.tools.javac.util.*; duke@1: import com.sun.tools.javac.util.Name; duke@1: duke@1: import static javax.lang.model.util.ElementFilter.methodsIn; duke@1: duke@1: /** duke@1: * Utility methods for operating on program elements. duke@1: * jjg@581: *

This is NOT part of any supported API. duke@1: * If you write code that depends on this, you do so at your own duke@1: * risk. This code and its internal interfaces are subject to change duke@1: * or deletion without notice.

duke@1: */ duke@1: public class JavacElements implements Elements { duke@1: duke@1: private JavaCompiler javaCompiler; duke@1: private Symtab syms; jjg@113: private Names names; duke@1: private Types types; duke@1: private Enter enter; duke@1: duke@1: public static JavacElements instance(Context context) { jjg@706: JavacElements instance = context.get(JavacElements.class); jjg@706: if (instance == null) duke@1: instance = new JavacElements(context); duke@1: return instance; duke@1: } duke@1: duke@1: /** duke@1: * Public for use only by JavacProcessingEnvironment duke@1: */ jjg@706: protected JavacElements(Context context) { duke@1: setContext(context); duke@1: } duke@1: duke@1: /** duke@1: * Use a new context. May be called from outside to update duke@1: * internal state for a new annotation-processing round. duke@1: */ duke@1: public void setContext(Context context) { jjg@706: context.put(JavacElements.class, this); duke@1: javaCompiler = JavaCompiler.instance(context); duke@1: syms = Symtab.instance(context); jjg@113: names = Names.instance(context); duke@1: types = Types.instance(context); duke@1: enter = Enter.instance(context); duke@1: } duke@1: duke@1: duke@1: /** duke@1: * An internal-use utility that creates a reified annotation. duke@1: */ duke@1: public static A getAnnotation(Symbol annotated, duke@1: Class annoType) { duke@1: if (!annoType.isAnnotation()) duke@1: throw new IllegalArgumentException("Not an annotation type: " duke@1: + annoType); duke@1: String name = annoType.getName(); duke@1: for (Attribute.Compound anno : annotated.getAnnotationMirrors()) duke@1: if (name.equals(anno.type.tsym.flatName().toString())) duke@1: return AnnotationProxyMaker.generateAnnotation(anno, annoType); duke@1: return null; duke@1: } duke@1: duke@1: /** duke@1: * An internal-use utility that creates a reified annotation. duke@1: * This overloaded version take annotation inheritance into account. duke@1: */ duke@1: public static A getAnnotation(ClassSymbol annotated, duke@1: Class annoType) { duke@1: boolean inherited = annoType.isAnnotationPresent(Inherited.class); duke@1: A result = null; jjg@113: while (annotated.name != annotated.name.table.names.java_lang_Object) { duke@1: result = getAnnotation((Symbol)annotated, annoType); duke@1: if (result != null || !inherited) duke@1: break; duke@1: Type sup = annotated.getSuperclass(); duke@1: if (sup.tag != TypeTags.CLASS || sup.isErroneous()) duke@1: break; duke@1: annotated = (ClassSymbol) sup.tsym; duke@1: } duke@1: return result; duke@1: } duke@1: duke@1: duke@1: public PackageSymbol getPackageElement(CharSequence name) { duke@1: String strName = name.toString(); duke@1: if (strName.equals("")) duke@1: return syms.unnamedPackage; duke@1: return SourceVersion.isName(strName) duke@1: ? nameToSymbol(strName, PackageSymbol.class) duke@1: : null; duke@1: } duke@1: duke@1: public ClassSymbol getTypeElement(CharSequence name) { duke@1: String strName = name.toString(); duke@1: return SourceVersion.isName(strName) duke@1: ? nameToSymbol(strName, ClassSymbol.class) duke@1: : null; duke@1: } duke@1: duke@1: /** duke@1: * Returns a symbol given the type's or packages's canonical name, duke@1: * or null if the name isn't found. duke@1: */ duke@1: private S nameToSymbol(String nameStr, Class clazz) { duke@1: Name name = names.fromString(nameStr); duke@1: // First check cache. duke@1: Symbol sym = (clazz == ClassSymbol.class) duke@1: ? syms.classes.get(name) duke@1: : syms.packages.get(name); duke@1: duke@1: try { duke@1: if (sym == null) duke@1: sym = javaCompiler.resolveIdent(nameStr); duke@1: duke@1: sym.complete(); duke@1: duke@1: return (sym.kind != Kinds.ERR && duke@1: sym.exists() && duke@1: clazz.isInstance(sym) && duke@1: name.equals(sym.getQualifiedName())) duke@1: ? clazz.cast(sym) duke@1: : null; duke@1: } catch (CompletionFailure e) { duke@1: return null; duke@1: } duke@1: } duke@1: duke@1: public JavacSourcePosition getSourcePosition(Element e) { duke@1: Pair treeTop = getTreeAndTopLevel(e); duke@1: if (treeTop == null) duke@1: return null; duke@1: JCTree tree = treeTop.fst; duke@1: JCCompilationUnit toplevel = treeTop.snd; duke@1: JavaFileObject sourcefile = toplevel.sourcefile; duke@1: if (sourcefile == null) duke@1: return null; duke@1: return new JavacSourcePosition(sourcefile, tree.pos, toplevel.lineMap); duke@1: } duke@1: duke@1: public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a) { duke@1: Pair treeTop = getTreeAndTopLevel(e); duke@1: if (treeTop == null) duke@1: return null; duke@1: JCTree tree = treeTop.fst; duke@1: JCCompilationUnit toplevel = treeTop.snd; duke@1: JavaFileObject sourcefile = toplevel.sourcefile; duke@1: if (sourcefile == null) duke@1: return null; duke@1: duke@1: JCTree annoTree = matchAnnoToTree(a, e, tree); duke@1: if (annoTree == null) duke@1: return null; duke@1: return new JavacSourcePosition(sourcefile, annoTree.pos, duke@1: toplevel.lineMap); duke@1: } duke@1: duke@1: public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a, duke@1: AnnotationValue v) { duke@1: // TODO: better accuracy in getSourcePosition(... AnnotationValue) duke@1: return getSourcePosition(e, a); duke@1: } duke@1: duke@1: /** duke@1: * Returns the tree for an annotation given the annotated element duke@1: * and the element's own tree. Returns null if the tree cannot be found. duke@1: */ duke@1: private JCTree matchAnnoToTree(AnnotationMirror findme, duke@1: Element e, JCTree tree) { duke@1: Symbol sym = cast(Symbol.class, e); duke@1: class Vis extends JCTree.Visitor { duke@1: List result = null; duke@1: public void visitTopLevel(JCCompilationUnit tree) { duke@1: result = tree.packageAnnotations; duke@1: } duke@1: public void visitClassDef(JCClassDecl tree) { duke@1: result = tree.mods.annotations; duke@1: } duke@1: public void visitMethodDef(JCMethodDecl tree) { duke@1: result = tree.mods.annotations; duke@1: } duke@1: public void visitVarDef(JCVariableDecl tree) { duke@1: result = tree.mods.annotations; duke@1: } duke@1: } duke@1: Vis vis = new Vis(); duke@1: tree.accept(vis); duke@1: if (vis.result == null) duke@1: return null; duke@1: return matchAnnoToTree(cast(Attribute.Compound.class, findme), duke@1: sym.getAnnotationMirrors(), duke@1: vis.result); duke@1: } duke@1: duke@1: /** duke@1: * Returns the tree for an annotation given a list of annotations duke@1: * in which to search (recursively) and their corresponding trees. duke@1: * Returns null if the tree cannot be found. duke@1: */ duke@1: private JCTree matchAnnoToTree(Attribute.Compound findme, duke@1: List annos, duke@1: List trees) { duke@1: for (Attribute.Compound anno : annos) { duke@1: for (JCAnnotation tree : trees) { duke@1: JCTree match = matchAnnoToTree(findme, anno, tree); duke@1: if (match != null) duke@1: return match; duke@1: } duke@1: } duke@1: return null; duke@1: } duke@1: duke@1: /** duke@1: * Returns the tree for an annotation given an Attribute to duke@1: * search (recursively) and its corresponding tree. duke@1: * Returns null if the tree cannot be found. duke@1: */ duke@1: private JCTree matchAnnoToTree(final Attribute.Compound findme, duke@1: final Attribute attr, duke@1: final JCTree tree) { duke@1: if (attr == findme) duke@1: return (tree.type.tsym == findme.type.tsym) ? tree : null; duke@1: duke@1: class Vis implements Attribute.Visitor { duke@1: JCTree result = null; duke@1: public void visitConstant(Attribute.Constant value) { duke@1: } duke@1: public void visitClass(Attribute.Class clazz) { duke@1: } duke@1: public void visitCompound(Attribute.Compound anno) { duke@1: for (Pair pair : anno.values) { duke@1: JCExpression expr = scanForAssign(pair.fst, tree); duke@1: if (expr != null) { duke@1: JCTree match = matchAnnoToTree(findme, pair.snd, expr); duke@1: if (match != null) { duke@1: result = match; duke@1: return; duke@1: } duke@1: } duke@1: } duke@1: } duke@1: public void visitArray(Attribute.Array array) { duke@1: if (tree.getTag() == JCTree.NEWARRAY && duke@1: types.elemtype(array.type).tsym == findme.type.tsym) { duke@1: List elems = ((JCNewArray) tree).elems; duke@1: for (Attribute value : array.values) { duke@1: if (value == findme) { duke@1: result = elems.head; duke@1: return; duke@1: } duke@1: elems = elems.tail; duke@1: } duke@1: } duke@1: } duke@1: public void visitEnum(Attribute.Enum e) { duke@1: } duke@1: public void visitError(Attribute.Error e) { duke@1: } duke@1: } duke@1: Vis vis = new Vis(); duke@1: attr.accept(vis); duke@1: return vis.result; duke@1: } duke@1: duke@1: /** duke@1: * Scans for a JCAssign node with a LHS matching a given duke@1: * symbol, and returns its RHS. Does not scan nested JCAnnotations. duke@1: */ duke@1: private JCExpression scanForAssign(final MethodSymbol sym, duke@1: final JCTree tree) { duke@1: class TS extends TreeScanner { duke@1: JCExpression result = null; duke@1: public void scan(JCTree t) { duke@1: if (t != null && result == null) duke@1: t.accept(this); duke@1: } duke@1: public void visitAnnotation(JCAnnotation t) { duke@1: if (t == tree) duke@1: scan(t.args); duke@1: } duke@1: public void visitAssign(JCAssign t) { duke@1: if (t.lhs.getTag() == JCTree.IDENT) { duke@1: JCIdent ident = (JCIdent) t.lhs; duke@1: if (ident.sym == sym) duke@1: result = t.rhs; duke@1: } duke@1: } duke@1: } duke@1: TS scanner = new TS(); duke@1: tree.accept(scanner); duke@1: return scanner.result; duke@1: } duke@1: duke@1: /** duke@1: * Returns the tree node corresponding to this element, or null duke@1: * if none can be found. duke@1: */ duke@1: public JCTree getTree(Element e) { duke@1: Pair treeTop = getTreeAndTopLevel(e); duke@1: return (treeTop != null) ? treeTop.fst : null; duke@1: } duke@1: duke@1: public String getDocComment(Element e) { duke@1: // Our doc comment is contained in a map in our toplevel, duke@1: // indexed by our tree. Find our enter environment, which gives duke@1: // us our toplevel. It also gives us a tree that contains our duke@1: // tree: walk it to find our tree. This is painful. duke@1: Pair treeTop = getTreeAndTopLevel(e); duke@1: if (treeTop == null) duke@1: return null; duke@1: JCTree tree = treeTop.fst; duke@1: JCCompilationUnit toplevel = treeTop.snd; duke@1: if (toplevel.docComments == null) duke@1: return null; duke@1: return toplevel.docComments.get(tree); duke@1: } duke@1: duke@1: public PackageElement getPackageOf(Element e) { duke@1: return cast(Symbol.class, e).packge(); duke@1: } duke@1: duke@1: public boolean isDeprecated(Element e) { duke@1: Symbol sym = cast(Symbol.class, e); duke@1: return (sym.flags() & Flags.DEPRECATED) != 0; duke@1: } duke@1: duke@1: public Name getBinaryName(TypeElement type) { duke@1: return cast(TypeSymbol.class, type).flatName(); duke@1: } duke@1: duke@1: public Map getElementValuesWithDefaults( duke@1: AnnotationMirror a) { duke@1: Attribute.Compound anno = cast(Attribute.Compound.class, a); duke@1: DeclaredType annotype = a.getAnnotationType(); duke@1: Map valmap = anno.getElementValues(); duke@1: duke@1: for (ExecutableElement ex : duke@1: methodsIn(annotype.asElement().getEnclosedElements())) { duke@1: MethodSymbol meth = (MethodSymbol) ex; duke@1: Attribute defaultValue = meth.getDefaultValue(); duke@1: if (defaultValue != null && !valmap.containsKey(meth)) { duke@1: valmap.put(meth, defaultValue); duke@1: } duke@1: } duke@1: return valmap; duke@1: } duke@1: duke@1: /** duke@1: * {@inheritDoc} duke@1: */ duke@1: public FilteredMemberList getAllMembers(TypeElement element) { duke@1: Symbol sym = cast(Symbol.class, element); duke@1: Scope scope = sym.members().dupUnshared(); duke@1: List closure = types.closure(sym.asType()); duke@1: for (Type t : closure) duke@1: addMembers(scope, t); duke@1: return new FilteredMemberList(scope); duke@1: } duke@1: // where duke@1: private void addMembers(Scope scope, Type type) { duke@1: members: duke@1: for (Scope.Entry e = type.asElement().members().elems; e != null; e = e.sibling) { duke@1: Scope.Entry overrider = scope.lookup(e.sym.getSimpleName()); duke@1: while (overrider.scope != null) { duke@1: if (overrider.sym.kind == e.sym.kind duke@1: && (overrider.sym.flags() & Flags.SYNTHETIC) == 0) duke@1: { duke@1: if (overrider.sym.getKind() == ElementKind.METHOD duke@1: && overrides((ExecutableElement)overrider.sym, (ExecutableElement)e.sym, (TypeElement)type.asElement())) { duke@1: continue members; duke@1: } duke@1: } duke@1: overrider = overrider.next(); duke@1: } duke@1: boolean derived = e.sym.getEnclosingElement() != scope.owner; duke@1: ElementKind kind = e.sym.getKind(); duke@1: boolean initializer = kind == ElementKind.CONSTRUCTOR duke@1: || kind == ElementKind.INSTANCE_INIT duke@1: || kind == ElementKind.STATIC_INIT; duke@1: if (!derived || (!initializer && e.sym.isInheritedIn(scope.owner, types))) duke@1: scope.enter(e.sym); duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Returns all annotations of an element, whether duke@1: * inherited or directly present. duke@1: * duke@1: * @param e the element being examined duke@1: * @return all annotations of the element duke@1: */ duke@1: public List getAllAnnotationMirrors(Element e) { duke@1: Symbol sym = cast(Symbol.class, e); duke@1: List annos = sym.getAnnotationMirrors(); duke@1: while (sym.getKind() == ElementKind.CLASS) { duke@1: Type sup = ((ClassSymbol) sym).getSuperclass(); duke@1: if (sup.tag != TypeTags.CLASS || sup.isErroneous() || duke@1: sup.tsym == syms.objectType.tsym) { duke@1: break; duke@1: } duke@1: sym = sup.tsym; duke@1: List oldAnnos = annos; duke@1: for (Attribute.Compound anno : sym.getAnnotationMirrors()) { duke@1: if (isInherited(anno.type) && duke@1: !containsAnnoOfType(oldAnnos, anno.type)) { duke@1: annos = annos.prepend(anno); duke@1: } duke@1: } duke@1: } duke@1: return annos; duke@1: } duke@1: duke@1: /** duke@1: * Tests whether an annotation type is @Inherited. duke@1: */ duke@1: private boolean isInherited(Type annotype) { duke@1: for (Attribute.Compound anno : annotype.tsym.getAnnotationMirrors()) { duke@1: if (anno.type.tsym == syms.inheritedType.tsym) duke@1: return true; duke@1: } duke@1: return false; duke@1: } duke@1: duke@1: /** duke@1: * Tests whether a list of annotations contains an annotation duke@1: * of a given type. duke@1: */ duke@1: private static boolean containsAnnoOfType(List annos, duke@1: Type type) { duke@1: for (Attribute.Compound anno : annos) { duke@1: if (anno.type.tsym == type.tsym) duke@1: return true; duke@1: } duke@1: return false; duke@1: } duke@1: duke@1: public boolean hides(Element hiderEl, Element hideeEl) { duke@1: Symbol hider = cast(Symbol.class, hiderEl); duke@1: Symbol hidee = cast(Symbol.class, hideeEl); duke@1: duke@1: // Fields only hide fields; methods only methods; types only types. duke@1: // Names must match. Nothing hides itself (just try it). duke@1: if (hider == hidee || duke@1: hider.kind != hidee.kind || duke@1: hider.name != hidee.name) { duke@1: return false; duke@1: } duke@1: duke@1: // Only static methods can hide other methods. duke@1: // Methods only hide methods with matching signatures. duke@1: if (hider.kind == Kinds.MTH) { duke@1: if (!hider.isStatic() || duke@1: !types.isSubSignature(hider.type, hidee.type)) { duke@1: return false; duke@1: } duke@1: } duke@1: duke@1: // Hider must be in a subclass of hidee's class. duke@1: // Note that if M1 hides M2, and M2 hides M3, and M3 is accessible duke@1: // in M1's class, then M1 and M2 both hide M3. duke@1: ClassSymbol hiderClass = hider.owner.enclClass(); duke@1: ClassSymbol hideeClass = hidee.owner.enclClass(); duke@1: if (hiderClass == null || hideeClass == null || duke@1: !hiderClass.isSubClass(hideeClass, types)) { duke@1: return false; duke@1: } duke@1: duke@1: // Hidee must be accessible in hider's class. duke@1: // The method isInheritedIn is poorly named: it checks only access. duke@1: return hidee.isInheritedIn(hiderClass, types); duke@1: } duke@1: duke@1: public boolean overrides(ExecutableElement riderEl, duke@1: ExecutableElement rideeEl, TypeElement typeEl) { duke@1: MethodSymbol rider = cast(MethodSymbol.class, riderEl); duke@1: MethodSymbol ridee = cast(MethodSymbol.class, rideeEl); duke@1: ClassSymbol origin = cast(ClassSymbol.class, typeEl); duke@1: duke@1: return rider.name == ridee.name && duke@1: duke@1: // not reflexive as per JLS duke@1: rider != ridee && duke@1: duke@1: // we don't care if ridee is static, though that wouldn't duke@1: // compile duke@1: !rider.isStatic() && duke@1: duke@1: // Symbol.overrides assumes the following duke@1: ridee.isMemberOf(origin, types) && duke@1: duke@1: // check access and signatures; don't check return types duke@1: rider.overrides(ridee, origin, types, false); duke@1: } duke@1: duke@1: public String getConstantExpression(Object value) { duke@1: return Constants.format(value); duke@1: } duke@1: duke@1: /** duke@1: * Print a representation of the elements to the given writer in duke@1: * the specified order. The main purpose of this method is for duke@1: * diagnostics. The exact format of the output is not duke@1: * specified and is subject to change. duke@1: * duke@1: * @param w the writer to print the output to duke@1: * @param elements the elements to print duke@1: */ duke@1: public void printElements(java.io.Writer w, Element... elements) { duke@1: for (Element element : elements) duke@1: (new PrintingProcessor.PrintingElementVisitor(w, this)).visit(element).flush(); duke@1: } duke@1: duke@1: public Name getName(CharSequence cs) { jjg@113: return names.fromString(cs.toString()); duke@1: } duke@1: duke@1: /** duke@1: * Returns the tree node and compilation unit corresponding to this duke@1: * element, or null if they can't be found. duke@1: */ duke@1: private Pair getTreeAndTopLevel(Element e) { duke@1: Symbol sym = cast(Symbol.class, e); duke@1: Env enterEnv = getEnterEnv(sym); duke@1: if (enterEnv == null) duke@1: return null; duke@1: JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree); duke@1: if (tree == null || enterEnv.toplevel == null) duke@1: return null; duke@1: return new Pair(tree, enterEnv.toplevel); duke@1: } duke@1: duke@1: /** duke@1: * Returns the best approximation for the tree node and compilation unit duke@1: * corresponding to the given element, annotation and value. duke@1: * If the element is null, null is returned. duke@1: * If the annotation is null or cannot be found, the tree node and duke@1: * compilation unit for the element is returned. duke@1: * If the annotation value is null or cannot be found, the tree node and duke@1: * compilation unit for the annotation is returned. duke@1: */ duke@1: public Pair getTreeAndTopLevel( duke@1: Element e, AnnotationMirror a, AnnotationValue v) { duke@1: if (e == null) duke@1: return null; duke@1: duke@1: Pair elemTreeTop = getTreeAndTopLevel(e); duke@1: if (elemTreeTop == null) duke@1: return null; duke@1: duke@1: if (a == null) duke@1: return elemTreeTop; duke@1: duke@1: JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst); duke@1: if (annoTree == null) duke@1: return elemTreeTop; duke@1: duke@1: // 6388543: if v != null, we should search within annoTree to find duke@1: // the tree matching v. For now, we ignore v and return the tree of duke@1: // the annotation. duke@1: return new Pair(annoTree, elemTreeTop.snd); duke@1: } duke@1: duke@1: /** duke@1: * Returns a symbol's enter environment, or null if it has none. duke@1: */ duke@1: private Env getEnterEnv(Symbol sym) { duke@1: // Get enclosing class of sym, or sym itself if it is a class duke@1: // or package. duke@1: TypeSymbol ts = (sym.kind != Kinds.PCK) duke@1: ? sym.enclClass() duke@1: : (PackageSymbol) sym; duke@1: return (ts != null) duke@1: ? enter.getEnv(ts) duke@1: : null; duke@1: } duke@1: duke@1: /** duke@1: * Returns an object cast to the specified type. duke@1: * @throws NullPointerException if the object is {@code null} duke@1: * @throws IllegalArgumentException if the object is of the wrong type duke@1: */ duke@1: private static T cast(Class clazz, Object o) { duke@1: if (! clazz.isInstance(o)) duke@1: throw new IllegalArgumentException(o.toString()); duke@1: return clazz.cast(o); duke@1: } duke@1: }