feat(jdk8): move files to new folder to avoid resources compiled.
This commit is contained in:
362
jdkSrc/jdk8/com/sun/tools/javac/model/AnnotationProxyMaker.java
Normal file
362
jdkSrc/jdk8/com/sun/tools/javac/model/AnnotationProxyMaker.java
Normal file
@@ -0,0 +1,362 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.tools.javac.model;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.lang.annotation.*;
|
||||
import java.lang.reflect.Array;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import sun.reflect.annotation.*;
|
||||
|
||||
import javax.lang.model.type.MirroredTypeException;
|
||||
import javax.lang.model.type.MirroredTypesException;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
|
||||
import com.sun.tools.javac.code.*;
|
||||
import com.sun.tools.javac.code.Symbol.*;
|
||||
import com.sun.tools.javac.code.Type.ArrayType;
|
||||
import com.sun.tools.javac.util.*;
|
||||
|
||||
|
||||
/**
|
||||
* A generator of dynamic proxy implementations of
|
||||
* java.lang.annotation.Annotation.
|
||||
*
|
||||
* <p> The "dynamic proxy return form" of an annotation element value is
|
||||
* the form used by sun.reflect.annotation.AnnotationInvocationHandler.
|
||||
*
|
||||
* <p><b>This is NOT part of any supported API.
|
||||
* If you write code that depends on this, you do so at your own risk.
|
||||
* This code and its internal interfaces are subject to change or
|
||||
* deletion without notice.</b>
|
||||
*/
|
||||
|
||||
public class AnnotationProxyMaker {
|
||||
|
||||
private final Attribute.Compound anno;
|
||||
private final Class<? extends Annotation> annoType;
|
||||
|
||||
|
||||
private AnnotationProxyMaker(Attribute.Compound anno,
|
||||
Class<? extends Annotation> annoType) {
|
||||
this.anno = anno;
|
||||
this.annoType = annoType;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a dynamic proxy for an annotation mirror.
|
||||
*/
|
||||
public static <A extends Annotation> A generateAnnotation(
|
||||
Attribute.Compound anno, Class<A> annoType) {
|
||||
AnnotationProxyMaker apm = new AnnotationProxyMaker(anno, annoType);
|
||||
return annoType.cast(apm.generateAnnotation());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a dynamic proxy for an annotation mirror.
|
||||
*/
|
||||
private Annotation generateAnnotation() {
|
||||
return AnnotationParser.annotationForMap(annoType,
|
||||
getAllReflectedValues());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map from element names to their values in "dynamic
|
||||
* proxy return form". Includes all elements, whether explicit or
|
||||
* defaulted.
|
||||
*/
|
||||
private Map<String, Object> getAllReflectedValues() {
|
||||
Map<String, Object> res = new LinkedHashMap<String, Object>();
|
||||
|
||||
for (Map.Entry<MethodSymbol, Attribute> entry :
|
||||
getAllValues().entrySet()) {
|
||||
MethodSymbol meth = entry.getKey();
|
||||
Object value = generateValue(meth, entry.getValue());
|
||||
if (value != null) {
|
||||
res.put(meth.name.toString(), value);
|
||||
} else {
|
||||
// Ignore this element. May (properly) lead to
|
||||
// IncompleteAnnotationException somewhere down the line.
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map from element symbols to their values.
|
||||
* Includes all elements, whether explicit or defaulted.
|
||||
*/
|
||||
private Map<MethodSymbol, Attribute> getAllValues() {
|
||||
Map<MethodSymbol, Attribute> res =
|
||||
new LinkedHashMap<MethodSymbol, Attribute>();
|
||||
|
||||
// First find the default values.
|
||||
ClassSymbol sym = (ClassSymbol) anno.type.tsym;
|
||||
for (Scope.Entry e = sym.members().elems; e != null; e = e.sibling) {
|
||||
if (e.sym.kind == Kinds.MTH) {
|
||||
MethodSymbol m = (MethodSymbol) e.sym;
|
||||
Attribute def = m.getDefaultValue();
|
||||
if (def != null)
|
||||
res.put(m, def);
|
||||
}
|
||||
}
|
||||
// Next find the explicit values, possibly overriding defaults.
|
||||
for (Pair<MethodSymbol, Attribute> p : anno.values)
|
||||
res.put(p.fst, p.snd);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an element value to its "dynamic proxy return form".
|
||||
* Returns an exception proxy on some errors, but may return null if
|
||||
* a useful exception cannot or should not be generated at this point.
|
||||
*/
|
||||
private Object generateValue(MethodSymbol meth, Attribute attr) {
|
||||
ValueVisitor vv = new ValueVisitor(meth);
|
||||
return vv.getValue(attr);
|
||||
}
|
||||
|
||||
|
||||
private class ValueVisitor implements Attribute.Visitor {
|
||||
|
||||
private MethodSymbol meth; // annotation element being visited
|
||||
private Class<?> returnClass; // return type of annotation element
|
||||
private Object value; // value in "dynamic proxy return form"
|
||||
|
||||
ValueVisitor(MethodSymbol meth) {
|
||||
this.meth = meth;
|
||||
}
|
||||
|
||||
Object getValue(Attribute attr) {
|
||||
Method method; // runtime method of annotation element
|
||||
try {
|
||||
method = annoType.getMethod(meth.name.toString());
|
||||
} catch (NoSuchMethodException e) {
|
||||
return null;
|
||||
}
|
||||
returnClass = method.getReturnType();
|
||||
attr.accept(this);
|
||||
if (!(value instanceof ExceptionProxy) &&
|
||||
!AnnotationType.invocationHandlerReturnType(returnClass)
|
||||
.isInstance(value)) {
|
||||
typeMismatch(method, attr);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
public void visitConstant(Attribute.Constant c) {
|
||||
value = c.getValue();
|
||||
}
|
||||
|
||||
public void visitClass(Attribute.Class c) {
|
||||
value = new MirroredTypeExceptionProxy(c.classType);
|
||||
}
|
||||
|
||||
public void visitArray(Attribute.Array a) {
|
||||
Name elemName = ((ArrayType) a.type).elemtype.tsym.getQualifiedName();
|
||||
|
||||
if (elemName.equals(elemName.table.names.java_lang_Class)) { // Class[]
|
||||
// Construct a proxy for a MirroredTypesException
|
||||
ListBuffer<TypeMirror> elems = new ListBuffer<TypeMirror>();
|
||||
for (Attribute value : a.values) {
|
||||
Type elem = ((Attribute.Class) value).classType;
|
||||
elems.append(elem);
|
||||
}
|
||||
value = new MirroredTypesExceptionProxy(elems.toList());
|
||||
|
||||
} else {
|
||||
int len = a.values.length;
|
||||
Class<?> returnClassSaved = returnClass;
|
||||
returnClass = returnClass.getComponentType();
|
||||
try {
|
||||
Object res = Array.newInstance(returnClass, len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
a.values[i].accept(this);
|
||||
if (value == null || value instanceof ExceptionProxy) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Array.set(res, i, value);
|
||||
} catch (IllegalArgumentException e) {
|
||||
value = null; // indicates a type mismatch
|
||||
return;
|
||||
}
|
||||
}
|
||||
value = res;
|
||||
} finally {
|
||||
returnClass = returnClassSaved;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
public void visitEnum(Attribute.Enum e) {
|
||||
if (returnClass.isEnum()) {
|
||||
String constName = e.value.toString();
|
||||
try {
|
||||
value = Enum.valueOf((Class)returnClass, constName);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
value = new EnumConstantNotPresentExceptionProxy(
|
||||
(Class<Enum<?>>) returnClass, constName);
|
||||
}
|
||||
} else {
|
||||
value = null; // indicates a type mismatch
|
||||
}
|
||||
}
|
||||
|
||||
public void visitCompound(Attribute.Compound c) {
|
||||
try {
|
||||
Class<? extends Annotation> nested =
|
||||
returnClass.asSubclass(Annotation.class);
|
||||
value = generateAnnotation(c, nested);
|
||||
} catch (ClassCastException ex) {
|
||||
value = null; // indicates a type mismatch
|
||||
}
|
||||
}
|
||||
|
||||
public void visitError(Attribute.Error e) {
|
||||
if (e instanceof Attribute.UnresolvedClass)
|
||||
value = new MirroredTypeExceptionProxy(((Attribute.UnresolvedClass)e).classType);
|
||||
else
|
||||
value = null; // indicates a type mismatch
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets "value" to an ExceptionProxy indicating a type mismatch.
|
||||
*/
|
||||
private void typeMismatch(Method method, final Attribute attr) {
|
||||
class AnnotationTypeMismatchExceptionProxy extends ExceptionProxy {
|
||||
static final long serialVersionUID = 269;
|
||||
transient final Method method;
|
||||
AnnotationTypeMismatchExceptionProxy(Method method) {
|
||||
this.method = method;
|
||||
}
|
||||
public String toString() {
|
||||
return "<error>"; // eg: @Anno(value=<error>)
|
||||
}
|
||||
protected RuntimeException generateException() {
|
||||
return new AnnotationTypeMismatchException(method,
|
||||
attr.type.toString());
|
||||
}
|
||||
}
|
||||
value = new AnnotationTypeMismatchExceptionProxy(method);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ExceptionProxy for MirroredTypeException.
|
||||
* The toString, hashCode, and equals methods forward to the underlying
|
||||
* type.
|
||||
*/
|
||||
private static final class MirroredTypeExceptionProxy extends ExceptionProxy {
|
||||
static final long serialVersionUID = 269;
|
||||
|
||||
private transient TypeMirror type;
|
||||
private final String typeString;
|
||||
|
||||
MirroredTypeExceptionProxy(TypeMirror t) {
|
||||
type = t;
|
||||
typeString = t.toString();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return typeString;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return (type != null ? type : typeString).hashCode();
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return type != null &&
|
||||
obj instanceof MirroredTypeExceptionProxy &&
|
||||
type.equals(((MirroredTypeExceptionProxy) obj).type);
|
||||
}
|
||||
|
||||
protected RuntimeException generateException() {
|
||||
return new MirroredTypeException(type);
|
||||
}
|
||||
|
||||
// Explicitly set all transient fields.
|
||||
private void readObject(ObjectInputStream s)
|
||||
throws IOException, ClassNotFoundException {
|
||||
s.defaultReadObject();
|
||||
type = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ExceptionProxy for MirroredTypesException.
|
||||
* The toString, hashCode, and equals methods foward to the underlying
|
||||
* types.
|
||||
*/
|
||||
private static final class MirroredTypesExceptionProxy extends ExceptionProxy {
|
||||
static final long serialVersionUID = 269;
|
||||
|
||||
private transient List<TypeMirror> types;
|
||||
private final String typeStrings;
|
||||
|
||||
MirroredTypesExceptionProxy(List<TypeMirror> ts) {
|
||||
types = ts;
|
||||
typeStrings = ts.toString();
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return typeStrings;
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
return (types != null ? types : typeStrings).hashCode();
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
return types != null &&
|
||||
obj instanceof MirroredTypesExceptionProxy &&
|
||||
types.equals(
|
||||
((MirroredTypesExceptionProxy) obj).types);
|
||||
}
|
||||
|
||||
protected RuntimeException generateException() {
|
||||
return new MirroredTypesException(types);
|
||||
}
|
||||
|
||||
// Explicitly set all transient fields.
|
||||
private void readObject(ObjectInputStream s)
|
||||
throws IOException, ClassNotFoundException {
|
||||
s.defaultReadObject();
|
||||
types = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
115
jdkSrc/jdk8/com/sun/tools/javac/model/FilteredMemberList.java
Normal file
115
jdkSrc/jdk8/com/sun/tools/javac/model/FilteredMemberList.java
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.tools.javac.model;
|
||||
|
||||
import java.util.AbstractList;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import com.sun.tools.javac.code.Scope;
|
||||
import com.sun.tools.javac.code.Symbol;
|
||||
|
||||
import static com.sun.tools.javac.code.Flags.*;
|
||||
|
||||
/**
|
||||
* Utility to construct a view of a symbol's members,
|
||||
* filtering out unwanted elements such as synthetic ones.
|
||||
* This view is most efficiently accessed through its iterator() method.
|
||||
*
|
||||
* <p><b>This is NOT part of any supported API.
|
||||
* If you write code that depends on this, you do so at your own risk.
|
||||
* This code and its internal interfaces are subject to change or
|
||||
* deletion without notice.</b>
|
||||
*/
|
||||
public class FilteredMemberList extends AbstractList<Symbol> {
|
||||
|
||||
private final Scope scope;
|
||||
|
||||
public FilteredMemberList(Scope scope) {
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
int cnt = 0;
|
||||
for (Scope.Entry e = scope.elems; e != null; e = e.sibling) {
|
||||
if (!unwanted(e.sym))
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
public Symbol get(int index) {
|
||||
for (Scope.Entry e = scope.elems; e != null; e = e.sibling) {
|
||||
if (!unwanted(e.sym) && (index-- == 0))
|
||||
return e.sym;
|
||||
}
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
// A more efficient implementation than AbstractList's.
|
||||
public Iterator<Symbol> iterator() {
|
||||
return new Iterator<Symbol>() {
|
||||
|
||||
/** The next entry to examine, or null if none. */
|
||||
private Scope.Entry nextEntry = scope.elems;
|
||||
|
||||
private boolean hasNextForSure = false;
|
||||
|
||||
public boolean hasNext() {
|
||||
if (hasNextForSure) {
|
||||
return true;
|
||||
}
|
||||
while (nextEntry != null && unwanted(nextEntry.sym)) {
|
||||
nextEntry = nextEntry.sibling;
|
||||
}
|
||||
hasNextForSure = (nextEntry != null);
|
||||
return hasNextForSure;
|
||||
}
|
||||
|
||||
public Symbol next() {
|
||||
if (hasNext()) {
|
||||
Symbol result = nextEntry.sym;
|
||||
nextEntry = nextEntry.sibling;
|
||||
hasNextForSure = false;
|
||||
return result;
|
||||
} else {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether this is a symbol that should never be seen by
|
||||
* clients, such as a synthetic class. Returns true for null.
|
||||
*/
|
||||
private static boolean unwanted(Symbol s) {
|
||||
return s == null || (s.flags() & SYNTHETIC) != 0;
|
||||
}
|
||||
}
|
||||
608
jdkSrc/jdk8/com/sun/tools/javac/model/JavacElements.java
Normal file
608
jdkSrc/jdk8/com/sun/tools/javac/model/JavacElements.java
Normal file
@@ -0,0 +1,608 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.tools.javac.model;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.lang.model.type.DeclaredType;
|
||||
import javax.lang.model.util.Elements;
|
||||
import javax.tools.JavaFileObject;
|
||||
import static javax.lang.model.util.ElementFilter.methodsIn;
|
||||
|
||||
import com.sun.tools.javac.code.*;
|
||||
import com.sun.tools.javac.code.Symbol.*;
|
||||
import com.sun.tools.javac.comp.AttrContext;
|
||||
import com.sun.tools.javac.comp.Enter;
|
||||
import com.sun.tools.javac.comp.Env;
|
||||
import com.sun.tools.javac.main.JavaCompiler;
|
||||
import com.sun.tools.javac.processing.PrintingProcessor;
|
||||
import com.sun.tools.javac.tree.JCTree;
|
||||
import com.sun.tools.javac.tree.JCTree.*;
|
||||
import com.sun.tools.javac.tree.TreeInfo;
|
||||
import com.sun.tools.javac.tree.TreeScanner;
|
||||
import com.sun.tools.javac.util.*;
|
||||
import com.sun.tools.javac.util.Name;
|
||||
import static com.sun.tools.javac.code.TypeTag.CLASS;
|
||||
import static com.sun.tools.javac.tree.JCTree.Tag.*;
|
||||
|
||||
/**
|
||||
* Utility methods for operating on program elements.
|
||||
*
|
||||
* <p><b>This is NOT part of any supported API.
|
||||
* If you write code that depends on this, you do so at your own
|
||||
* risk. This code and its internal interfaces are subject to change
|
||||
* or deletion without notice.</b></p>
|
||||
*/
|
||||
public class JavacElements implements Elements {
|
||||
|
||||
private JavaCompiler javaCompiler;
|
||||
private Symtab syms;
|
||||
private Names names;
|
||||
private Types types;
|
||||
private Enter enter;
|
||||
|
||||
public static JavacElements instance(Context context) {
|
||||
JavacElements instance = context.get(JavacElements.class);
|
||||
if (instance == null)
|
||||
instance = new JavacElements(context);
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public for use only by JavacProcessingEnvironment
|
||||
*/
|
||||
protected JavacElements(Context context) {
|
||||
setContext(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use a new context. May be called from outside to update
|
||||
* internal state for a new annotation-processing round.
|
||||
*/
|
||||
public void setContext(Context context) {
|
||||
context.put(JavacElements.class, this);
|
||||
javaCompiler = JavaCompiler.instance(context);
|
||||
syms = Symtab.instance(context);
|
||||
names = Names.instance(context);
|
||||
types = Types.instance(context);
|
||||
enter = Enter.instance(context);
|
||||
}
|
||||
|
||||
public PackageSymbol getPackageElement(CharSequence name) {
|
||||
String strName = name.toString();
|
||||
if (strName.equals(""))
|
||||
return syms.unnamedPackage;
|
||||
return SourceVersion.isName(strName)
|
||||
? nameToSymbol(strName, PackageSymbol.class)
|
||||
: null;
|
||||
}
|
||||
|
||||
public ClassSymbol getTypeElement(CharSequence name) {
|
||||
String strName = name.toString();
|
||||
return SourceVersion.isName(strName)
|
||||
? nameToSymbol(strName, ClassSymbol.class)
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a symbol given the type's or packages's canonical name,
|
||||
* or null if the name isn't found.
|
||||
*/
|
||||
private <S extends Symbol> S nameToSymbol(String nameStr, Class<S> clazz) {
|
||||
Name name = names.fromString(nameStr);
|
||||
// First check cache.
|
||||
Symbol sym = (clazz == ClassSymbol.class)
|
||||
? syms.classes.get(name)
|
||||
: syms.packages.get(name);
|
||||
|
||||
try {
|
||||
if (sym == null)
|
||||
sym = javaCompiler.resolveIdent(nameStr);
|
||||
|
||||
sym.complete();
|
||||
|
||||
return (sym.kind != Kinds.ERR &&
|
||||
sym.exists() &&
|
||||
clazz.isInstance(sym) &&
|
||||
name.equals(sym.getQualifiedName()))
|
||||
? clazz.cast(sym)
|
||||
: null;
|
||||
} catch (CompletionFailure e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public JavacSourcePosition getSourcePosition(Element e) {
|
||||
Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
|
||||
if (treeTop == null)
|
||||
return null;
|
||||
JCTree tree = treeTop.fst;
|
||||
JCCompilationUnit toplevel = treeTop.snd;
|
||||
JavaFileObject sourcefile = toplevel.sourcefile;
|
||||
if (sourcefile == null)
|
||||
return null;
|
||||
return new JavacSourcePosition(sourcefile, tree.pos, toplevel.lineMap);
|
||||
}
|
||||
|
||||
public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a) {
|
||||
Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
|
||||
if (treeTop == null)
|
||||
return null;
|
||||
JCTree tree = treeTop.fst;
|
||||
JCCompilationUnit toplevel = treeTop.snd;
|
||||
JavaFileObject sourcefile = toplevel.sourcefile;
|
||||
if (sourcefile == null)
|
||||
return null;
|
||||
|
||||
JCTree annoTree = matchAnnoToTree(a, e, tree);
|
||||
if (annoTree == null)
|
||||
return null;
|
||||
return new JavacSourcePosition(sourcefile, annoTree.pos,
|
||||
toplevel.lineMap);
|
||||
}
|
||||
|
||||
public JavacSourcePosition getSourcePosition(Element e, AnnotationMirror a,
|
||||
AnnotationValue v) {
|
||||
// TODO: better accuracy in getSourcePosition(... AnnotationValue)
|
||||
return getSourcePosition(e, a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tree for an annotation given the annotated element
|
||||
* and the element's own tree. Returns null if the tree cannot be found.
|
||||
*/
|
||||
private JCTree matchAnnoToTree(AnnotationMirror findme,
|
||||
Element e, JCTree tree) {
|
||||
Symbol sym = cast(Symbol.class, e);
|
||||
class Vis extends JCTree.Visitor {
|
||||
List<JCAnnotation> result = null;
|
||||
public void visitTopLevel(JCCompilationUnit tree) {
|
||||
result = tree.packageAnnotations;
|
||||
}
|
||||
public void visitClassDef(JCClassDecl tree) {
|
||||
result = tree.mods.annotations;
|
||||
}
|
||||
public void visitMethodDef(JCMethodDecl tree) {
|
||||
result = tree.mods.annotations;
|
||||
}
|
||||
public void visitVarDef(JCVariableDecl tree) {
|
||||
result = tree.mods.annotations;
|
||||
}
|
||||
}
|
||||
Vis vis = new Vis();
|
||||
tree.accept(vis);
|
||||
if (vis.result == null)
|
||||
return null;
|
||||
|
||||
List<Attribute.Compound> annos = sym.getRawAttributes();
|
||||
return matchAnnoToTree(cast(Attribute.Compound.class, findme),
|
||||
annos,
|
||||
vis.result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tree for an annotation given a list of annotations
|
||||
* in which to search (recursively) and their corresponding trees.
|
||||
* Returns null if the tree cannot be found.
|
||||
*/
|
||||
private JCTree matchAnnoToTree(Attribute.Compound findme,
|
||||
List<Attribute.Compound> annos,
|
||||
List<JCAnnotation> trees) {
|
||||
for (Attribute.Compound anno : annos) {
|
||||
for (JCAnnotation tree : trees) {
|
||||
JCTree match = matchAnnoToTree(findme, anno, tree);
|
||||
if (match != null)
|
||||
return match;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tree for an annotation given an Attribute to
|
||||
* search (recursively) and its corresponding tree.
|
||||
* Returns null if the tree cannot be found.
|
||||
*/
|
||||
private JCTree matchAnnoToTree(final Attribute.Compound findme,
|
||||
final Attribute attr,
|
||||
final JCTree tree) {
|
||||
if (attr == findme)
|
||||
return (tree.type.tsym == findme.type.tsym) ? tree : null;
|
||||
|
||||
class Vis implements Attribute.Visitor {
|
||||
JCTree result = null;
|
||||
public void visitConstant(Attribute.Constant value) {
|
||||
}
|
||||
public void visitClass(Attribute.Class clazz) {
|
||||
}
|
||||
public void visitCompound(Attribute.Compound anno) {
|
||||
for (Pair<MethodSymbol, Attribute> pair : anno.values) {
|
||||
JCExpression expr = scanForAssign(pair.fst, tree);
|
||||
if (expr != null) {
|
||||
JCTree match = matchAnnoToTree(findme, pair.snd, expr);
|
||||
if (match != null) {
|
||||
result = match;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public void visitArray(Attribute.Array array) {
|
||||
if (tree.hasTag(NEWARRAY) &&
|
||||
types.elemtype(array.type).tsym == findme.type.tsym) {
|
||||
List<JCExpression> elems = ((JCNewArray) tree).elems;
|
||||
for (Attribute value : array.values) {
|
||||
if (value == findme) {
|
||||
result = elems.head;
|
||||
return;
|
||||
}
|
||||
elems = elems.tail;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void visitEnum(Attribute.Enum e) {
|
||||
}
|
||||
public void visitError(Attribute.Error e) {
|
||||
}
|
||||
}
|
||||
Vis vis = new Vis();
|
||||
attr.accept(vis);
|
||||
return vis.result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scans for a JCAssign node with a LHS matching a given
|
||||
* symbol, and returns its RHS. Does not scan nested JCAnnotations.
|
||||
*/
|
||||
private JCExpression scanForAssign(final MethodSymbol sym,
|
||||
final JCTree tree) {
|
||||
class TS extends TreeScanner {
|
||||
JCExpression result = null;
|
||||
public void scan(JCTree t) {
|
||||
if (t != null && result == null)
|
||||
t.accept(this);
|
||||
}
|
||||
public void visitAnnotation(JCAnnotation t) {
|
||||
if (t == tree)
|
||||
scan(t.args);
|
||||
}
|
||||
public void visitAssign(JCAssign t) {
|
||||
if (t.lhs.hasTag(IDENT)) {
|
||||
JCIdent ident = (JCIdent) t.lhs;
|
||||
if (ident.sym == sym)
|
||||
result = t.rhs;
|
||||
}
|
||||
}
|
||||
}
|
||||
TS scanner = new TS();
|
||||
tree.accept(scanner);
|
||||
return scanner.result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tree node corresponding to this element, or null
|
||||
* if none can be found.
|
||||
*/
|
||||
public JCTree getTree(Element e) {
|
||||
Pair<JCTree, ?> treeTop = getTreeAndTopLevel(e);
|
||||
return (treeTop != null) ? treeTop.fst : null;
|
||||
}
|
||||
|
||||
public String getDocComment(Element e) {
|
||||
// Our doc comment is contained in a map in our toplevel,
|
||||
// indexed by our tree. Find our enter environment, which gives
|
||||
// us our toplevel. It also gives us a tree that contains our
|
||||
// tree: walk it to find our tree. This is painful.
|
||||
Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
|
||||
if (treeTop == null)
|
||||
return null;
|
||||
JCTree tree = treeTop.fst;
|
||||
JCCompilationUnit toplevel = treeTop.snd;
|
||||
if (toplevel.docComments == null)
|
||||
return null;
|
||||
return toplevel.docComments.getCommentText(tree);
|
||||
}
|
||||
|
||||
public PackageElement getPackageOf(Element e) {
|
||||
return cast(Symbol.class, e).packge();
|
||||
}
|
||||
|
||||
public boolean isDeprecated(Element e) {
|
||||
Symbol sym = cast(Symbol.class, e);
|
||||
return (sym.flags() & Flags.DEPRECATED) != 0;
|
||||
}
|
||||
|
||||
public Name getBinaryName(TypeElement type) {
|
||||
return cast(TypeSymbol.class, type).flatName();
|
||||
}
|
||||
|
||||
public Map<MethodSymbol, Attribute> getElementValuesWithDefaults(
|
||||
AnnotationMirror a) {
|
||||
Attribute.Compound anno = cast(Attribute.Compound.class, a);
|
||||
DeclaredType annotype = a.getAnnotationType();
|
||||
Map<MethodSymbol, Attribute> valmap = anno.getElementValues();
|
||||
|
||||
for (ExecutableElement ex :
|
||||
methodsIn(annotype.asElement().getEnclosedElements())) {
|
||||
MethodSymbol meth = (MethodSymbol) ex;
|
||||
Attribute defaultValue = meth.getDefaultValue();
|
||||
if (defaultValue != null && !valmap.containsKey(meth)) {
|
||||
valmap.put(meth, defaultValue);
|
||||
}
|
||||
}
|
||||
return valmap;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public FilteredMemberList getAllMembers(TypeElement element) {
|
||||
Symbol sym = cast(Symbol.class, element);
|
||||
Scope scope = sym.members().dupUnshared();
|
||||
List<Type> closure = types.closure(sym.asType());
|
||||
for (Type t : closure)
|
||||
addMembers(scope, t);
|
||||
return new FilteredMemberList(scope);
|
||||
}
|
||||
// where
|
||||
private void addMembers(Scope scope, Type type) {
|
||||
members:
|
||||
for (Scope.Entry e = type.asElement().members().elems; e != null; e = e.sibling) {
|
||||
Scope.Entry overrider = scope.lookup(e.sym.getSimpleName());
|
||||
while (overrider.scope != null) {
|
||||
if (overrider.sym.kind == e.sym.kind
|
||||
&& (overrider.sym.flags() & Flags.SYNTHETIC) == 0)
|
||||
{
|
||||
if (overrider.sym.getKind() == ElementKind.METHOD
|
||||
&& overrides((ExecutableElement)overrider.sym, (ExecutableElement)e.sym, (TypeElement)type.asElement())) {
|
||||
continue members;
|
||||
}
|
||||
}
|
||||
overrider = overrider.next();
|
||||
}
|
||||
boolean derived = e.sym.getEnclosingElement() != scope.owner;
|
||||
ElementKind kind = e.sym.getKind();
|
||||
boolean initializer = kind == ElementKind.CONSTRUCTOR
|
||||
|| kind == ElementKind.INSTANCE_INIT
|
||||
|| kind == ElementKind.STATIC_INIT;
|
||||
if (!derived || (!initializer && e.sym.isInheritedIn(scope.owner, types)))
|
||||
scope.enter(e.sym);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all annotations of an element, whether
|
||||
* inherited or directly present.
|
||||
*
|
||||
* @param e the element being examined
|
||||
* @return all annotations of the element
|
||||
*/
|
||||
@Override
|
||||
public List<Attribute.Compound> getAllAnnotationMirrors(Element e) {
|
||||
Symbol sym = cast(Symbol.class, e);
|
||||
List<Attribute.Compound> annos = sym.getAnnotationMirrors();
|
||||
while (sym.getKind() == ElementKind.CLASS) {
|
||||
Type sup = ((ClassSymbol) sym).getSuperclass();
|
||||
if (!sup.hasTag(CLASS) || sup.isErroneous() ||
|
||||
sup.tsym == syms.objectType.tsym) {
|
||||
break;
|
||||
}
|
||||
sym = sup.tsym;
|
||||
List<Attribute.Compound> oldAnnos = annos;
|
||||
List<Attribute.Compound> newAnnos = sym.getAnnotationMirrors();
|
||||
for (Attribute.Compound anno : newAnnos) {
|
||||
if (isInherited(anno.type) &&
|
||||
!containsAnnoOfType(oldAnnos, anno.type)) {
|
||||
annos = annos.prepend(anno);
|
||||
}
|
||||
}
|
||||
}
|
||||
return annos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether an annotation type is @Inherited.
|
||||
*/
|
||||
private boolean isInherited(Type annotype) {
|
||||
return annotype.tsym.attribute(syms.inheritedType.tsym) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether a list of annotations contains an annotation
|
||||
* of a given type.
|
||||
*/
|
||||
private static boolean containsAnnoOfType(List<Attribute.Compound> annos,
|
||||
Type type) {
|
||||
for (Attribute.Compound anno : annos) {
|
||||
if (anno.type.tsym == type.tsym)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hides(Element hiderEl, Element hideeEl) {
|
||||
Symbol hider = cast(Symbol.class, hiderEl);
|
||||
Symbol hidee = cast(Symbol.class, hideeEl);
|
||||
|
||||
// Fields only hide fields; methods only methods; types only types.
|
||||
// Names must match. Nothing hides itself (just try it).
|
||||
if (hider == hidee ||
|
||||
hider.kind != hidee.kind ||
|
||||
hider.name != hidee.name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Only static methods can hide other methods.
|
||||
// Methods only hide methods with matching signatures.
|
||||
if (hider.kind == Kinds.MTH) {
|
||||
if (!hider.isStatic() ||
|
||||
!types.isSubSignature(hider.type, hidee.type)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Hider must be in a subclass of hidee's class.
|
||||
// Note that if M1 hides M2, and M2 hides M3, and M3 is accessible
|
||||
// in M1's class, then M1 and M2 both hide M3.
|
||||
ClassSymbol hiderClass = hider.owner.enclClass();
|
||||
ClassSymbol hideeClass = hidee.owner.enclClass();
|
||||
if (hiderClass == null || hideeClass == null ||
|
||||
!hiderClass.isSubClass(hideeClass, types)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Hidee must be accessible in hider's class.
|
||||
// The method isInheritedIn is poorly named: it checks only access.
|
||||
return hidee.isInheritedIn(hiderClass, types);
|
||||
}
|
||||
|
||||
public boolean overrides(ExecutableElement riderEl,
|
||||
ExecutableElement rideeEl, TypeElement typeEl) {
|
||||
MethodSymbol rider = cast(MethodSymbol.class, riderEl);
|
||||
MethodSymbol ridee = cast(MethodSymbol.class, rideeEl);
|
||||
ClassSymbol origin = cast(ClassSymbol.class, typeEl);
|
||||
|
||||
return rider.name == ridee.name &&
|
||||
|
||||
// not reflexive as per JLS
|
||||
rider != ridee &&
|
||||
|
||||
// we don't care if ridee is static, though that wouldn't
|
||||
// compile
|
||||
!rider.isStatic() &&
|
||||
|
||||
// Symbol.overrides assumes the following
|
||||
ridee.isMemberOf(origin, types) &&
|
||||
|
||||
// check access and signatures; don't check return types
|
||||
rider.overrides(ridee, origin, types, false);
|
||||
}
|
||||
|
||||
public String getConstantExpression(Object value) {
|
||||
return Constants.format(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a representation of the elements to the given writer in
|
||||
* the specified order. The main purpose of this method is for
|
||||
* diagnostics. The exact format of the output is <em>not</em>
|
||||
* specified and is subject to change.
|
||||
*
|
||||
* @param w the writer to print the output to
|
||||
* @param elements the elements to print
|
||||
*/
|
||||
public void printElements(java.io.Writer w, Element... elements) {
|
||||
for (Element element : elements)
|
||||
(new PrintingProcessor.PrintingElementVisitor(w, this)).visit(element).flush();
|
||||
}
|
||||
|
||||
public Name getName(CharSequence cs) {
|
||||
return names.fromString(cs.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFunctionalInterface(TypeElement element) {
|
||||
if (element.getKind() != ElementKind.INTERFACE)
|
||||
return false;
|
||||
else {
|
||||
TypeSymbol tsym = cast(TypeSymbol.class, element);
|
||||
return types.isFunctionalInterface(tsym);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tree node and compilation unit corresponding to this
|
||||
* element, or null if they can't be found.
|
||||
*/
|
||||
private Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e) {
|
||||
Symbol sym = cast(Symbol.class, e);
|
||||
Env<AttrContext> enterEnv = getEnterEnv(sym);
|
||||
if (enterEnv == null)
|
||||
return null;
|
||||
JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree);
|
||||
if (tree == null || enterEnv.toplevel == null)
|
||||
return null;
|
||||
return new Pair<JCTree,JCCompilationUnit>(tree, enterEnv.toplevel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the best approximation for the tree node and compilation unit
|
||||
* corresponding to the given element, annotation and value.
|
||||
* If the element is null, null is returned.
|
||||
* If the annotation is null or cannot be found, the tree node and
|
||||
* compilation unit for the element is returned.
|
||||
* If the annotation value is null or cannot be found, the tree node and
|
||||
* compilation unit for the annotation is returned.
|
||||
*/
|
||||
public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(
|
||||
Element e, AnnotationMirror a, AnnotationValue v) {
|
||||
if (e == null)
|
||||
return null;
|
||||
|
||||
Pair<JCTree, JCCompilationUnit> elemTreeTop = getTreeAndTopLevel(e);
|
||||
if (elemTreeTop == null)
|
||||
return null;
|
||||
|
||||
if (a == null)
|
||||
return elemTreeTop;
|
||||
|
||||
JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst);
|
||||
if (annoTree == null)
|
||||
return elemTreeTop;
|
||||
|
||||
// 6388543: if v != null, we should search within annoTree to find
|
||||
// the tree matching v. For now, we ignore v and return the tree of
|
||||
// the annotation.
|
||||
return new Pair<JCTree, JCCompilationUnit>(annoTree, elemTreeTop.snd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a symbol's enter environment, or null if it has none.
|
||||
*/
|
||||
private Env<AttrContext> getEnterEnv(Symbol sym) {
|
||||
// Get enclosing class of sym, or sym itself if it is a class
|
||||
// or package.
|
||||
TypeSymbol ts = (sym.kind != Kinds.PCK)
|
||||
? sym.enclClass()
|
||||
: (PackageSymbol) sym;
|
||||
return (ts != null)
|
||||
? enter.getEnv(ts)
|
||||
: null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an object cast to the specified type.
|
||||
* @throws NullPointerException if the object is {@code null}
|
||||
* @throws IllegalArgumentException if the object is of the wrong type
|
||||
*/
|
||||
private static <T> T cast(Class<T> clazz, Object o) {
|
||||
if (! clazz.isInstance(o))
|
||||
throw new IllegalArgumentException(o.toString());
|
||||
return clazz.cast(o);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.tools.javac.model;
|
||||
|
||||
import javax.tools.JavaFileObject;
|
||||
import com.sun.tools.javac.util.Position;
|
||||
|
||||
/**
|
||||
* Implementation of model API SourcePosition based on javac internal state.
|
||||
*
|
||||
* <p><b>This is NOT part of any supported API.
|
||||
* If you write code that depends on this, you do so at your own
|
||||
* risk. This code and its internal interfaces are subject to change
|
||||
* or deletion without notice.</b></p>
|
||||
*/
|
||||
class JavacSourcePosition {
|
||||
|
||||
final JavaFileObject sourcefile;
|
||||
final int pos;
|
||||
final Position.LineMap lineMap;
|
||||
|
||||
JavacSourcePosition(JavaFileObject sourcefile,
|
||||
int pos,
|
||||
Position.LineMap lineMap) {
|
||||
this.sourcefile = sourcefile;
|
||||
this.pos = pos;
|
||||
this.lineMap = (pos != Position.NOPOS) ? lineMap : null;
|
||||
}
|
||||
|
||||
public JavaFileObject getFile() {
|
||||
return sourcefile;
|
||||
}
|
||||
|
||||
public int getOffset() {
|
||||
return pos; // makes use of fact that Position.NOPOS == -1
|
||||
}
|
||||
|
||||
public int getLine() {
|
||||
return (lineMap != null) ? lineMap.getLineNumber(pos) : -1;
|
||||
}
|
||||
|
||||
public int getColumn() {
|
||||
return (lineMap != null) ? lineMap.getColumnNumber(pos) : -1;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
int line = getLine();
|
||||
return (line > 0)
|
||||
? sourcefile + ":" + line
|
||||
: sourcefile.toString();
|
||||
}
|
||||
}
|
||||
331
jdkSrc/jdk8/com/sun/tools/javac/model/JavacTypes.java
Normal file
331
jdkSrc/jdk8/com/sun/tools/javac/model/JavacTypes.java
Normal file
@@ -0,0 +1,331 @@
|
||||
/*
|
||||
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package com.sun.tools.javac.model;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.EnumSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.lang.model.element.*;
|
||||
import javax.lang.model.type.*;
|
||||
|
||||
import com.sun.tools.javac.code.*;
|
||||
import com.sun.tools.javac.code.Symbol.*;
|
||||
import com.sun.tools.javac.util.*;
|
||||
|
||||
/**
|
||||
* Utility methods for operating on types.
|
||||
*
|
||||
* <p><b>This is NOT part of any supported API.
|
||||
* If you write code that depends on this, you do so at your own
|
||||
* risk. This code and its internal interfaces are subject to change
|
||||
* or deletion without notice.</b></p>
|
||||
*/
|
||||
public class JavacTypes implements javax.lang.model.util.Types {
|
||||
|
||||
private Symtab syms;
|
||||
private Types types;
|
||||
|
||||
public static JavacTypes instance(Context context) {
|
||||
JavacTypes instance = context.get(JavacTypes.class);
|
||||
if (instance == null)
|
||||
instance = new JavacTypes(context);
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public for use only by JavacProcessingEnvironment
|
||||
*/
|
||||
protected JavacTypes(Context context) {
|
||||
setContext(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use a new context. May be called from outside to update
|
||||
* internal state for a new annotation-processing round.
|
||||
*/
|
||||
public void setContext(Context context) {
|
||||
context.put(JavacTypes.class, this);
|
||||
syms = Symtab.instance(context);
|
||||
types = Types.instance(context);
|
||||
}
|
||||
|
||||
public Element asElement(TypeMirror t) {
|
||||
switch (t.getKind()) {
|
||||
case DECLARED:
|
||||
case INTERSECTION:
|
||||
case ERROR:
|
||||
case TYPEVAR:
|
||||
Type type = cast(Type.class, t);
|
||||
return type.asElement();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isSameType(TypeMirror t1, TypeMirror t2) {
|
||||
return types.isSameType((Type) t1, (Type) t2);
|
||||
}
|
||||
|
||||
public boolean isSubtype(TypeMirror t1, TypeMirror t2) {
|
||||
validateTypeNotIn(t1, EXEC_OR_PKG);
|
||||
validateTypeNotIn(t2, EXEC_OR_PKG);
|
||||
return types.isSubtype((Type) t1, (Type) t2);
|
||||
}
|
||||
|
||||
public boolean isAssignable(TypeMirror t1, TypeMirror t2) {
|
||||
validateTypeNotIn(t1, EXEC_OR_PKG);
|
||||
validateTypeNotIn(t2, EXEC_OR_PKG);
|
||||
return types.isAssignable((Type) t1, (Type) t2);
|
||||
}
|
||||
|
||||
public boolean contains(TypeMirror t1, TypeMirror t2) {
|
||||
validateTypeNotIn(t1, EXEC_OR_PKG);
|
||||
validateTypeNotIn(t2, EXEC_OR_PKG);
|
||||
return types.containsType((Type) t1, (Type) t2);
|
||||
}
|
||||
|
||||
public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
|
||||
return types.isSubSignature((Type) m1, (Type) m2);
|
||||
}
|
||||
|
||||
public List<Type> directSupertypes(TypeMirror t) {
|
||||
validateTypeNotIn(t, EXEC_OR_PKG);
|
||||
return types.directSupertypes((Type) t);
|
||||
}
|
||||
|
||||
public TypeMirror erasure(TypeMirror t) {
|
||||
if (t.getKind() == TypeKind.PACKAGE)
|
||||
throw new IllegalArgumentException(t.toString());
|
||||
return types.erasure((Type) t);
|
||||
}
|
||||
|
||||
public TypeElement boxedClass(PrimitiveType p) {
|
||||
return types.boxedClass((Type) p);
|
||||
}
|
||||
|
||||
public PrimitiveType unboxedType(TypeMirror t) {
|
||||
if (t.getKind() != TypeKind.DECLARED)
|
||||
throw new IllegalArgumentException(t.toString());
|
||||
Type unboxed = types.unboxedType((Type) t);
|
||||
if (! unboxed.isPrimitive()) // only true primitives, not void
|
||||
throw new IllegalArgumentException(t.toString());
|
||||
return (PrimitiveType)unboxed;
|
||||
}
|
||||
|
||||
public TypeMirror capture(TypeMirror t) {
|
||||
validateTypeNotIn(t, EXEC_OR_PKG);
|
||||
return types.capture((Type) t);
|
||||
}
|
||||
|
||||
public PrimitiveType getPrimitiveType(TypeKind kind) {
|
||||
switch (kind) {
|
||||
case BOOLEAN: return syms.booleanType;
|
||||
case BYTE: return syms.byteType;
|
||||
case SHORT: return syms.shortType;
|
||||
case INT: return syms.intType;
|
||||
case LONG: return syms.longType;
|
||||
case CHAR: return syms.charType;
|
||||
case FLOAT: return syms.floatType;
|
||||
case DOUBLE: return syms.doubleType;
|
||||
default:
|
||||
throw new IllegalArgumentException("Not a primitive type: " + kind);
|
||||
}
|
||||
}
|
||||
|
||||
public NullType getNullType() {
|
||||
return (NullType) syms.botType;
|
||||
}
|
||||
|
||||
public NoType getNoType(TypeKind kind) {
|
||||
switch (kind) {
|
||||
case VOID: return syms.voidType;
|
||||
case NONE: return Type.noType;
|
||||
default:
|
||||
throw new IllegalArgumentException(kind.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayType getArrayType(TypeMirror componentType) {
|
||||
switch (componentType.getKind()) {
|
||||
case VOID:
|
||||
case EXECUTABLE:
|
||||
case WILDCARD: // heh!
|
||||
case PACKAGE:
|
||||
throw new IllegalArgumentException(componentType.toString());
|
||||
}
|
||||
return new Type.ArrayType((Type) componentType, syms.arrayClass);
|
||||
}
|
||||
|
||||
public WildcardType getWildcardType(TypeMirror extendsBound,
|
||||
TypeMirror superBound) {
|
||||
BoundKind bkind;
|
||||
Type bound;
|
||||
if (extendsBound == null && superBound == null) {
|
||||
bkind = BoundKind.UNBOUND;
|
||||
bound = syms.objectType;
|
||||
} else if (superBound == null) {
|
||||
bkind = BoundKind.EXTENDS;
|
||||
bound = (Type) extendsBound;
|
||||
} else if (extendsBound == null) {
|
||||
bkind = BoundKind.SUPER;
|
||||
bound = (Type) superBound;
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Extends and super bounds cannot both be provided");
|
||||
}
|
||||
switch (bound.getKind()) {
|
||||
case ARRAY:
|
||||
case DECLARED:
|
||||
case ERROR:
|
||||
case TYPEVAR:
|
||||
return new Type.WildcardType(bound, bkind, syms.boundClass);
|
||||
default:
|
||||
throw new IllegalArgumentException(bound.toString());
|
||||
}
|
||||
}
|
||||
|
||||
public DeclaredType getDeclaredType(TypeElement typeElem,
|
||||
TypeMirror... typeArgs) {
|
||||
ClassSymbol sym = (ClassSymbol) typeElem;
|
||||
|
||||
if (typeArgs.length == 0)
|
||||
return (DeclaredType) sym.erasure(types);
|
||||
if (sym.type.getEnclosingType().isParameterized())
|
||||
throw new IllegalArgumentException(sym.toString());
|
||||
|
||||
return getDeclaredType0(sym.type.getEnclosingType(), sym, typeArgs);
|
||||
}
|
||||
|
||||
public DeclaredType getDeclaredType(DeclaredType enclosing,
|
||||
TypeElement typeElem,
|
||||
TypeMirror... typeArgs) {
|
||||
if (enclosing == null)
|
||||
return getDeclaredType(typeElem, typeArgs);
|
||||
|
||||
ClassSymbol sym = (ClassSymbol) typeElem;
|
||||
Type outer = (Type) enclosing;
|
||||
|
||||
if (outer.tsym != sym.owner.enclClass())
|
||||
throw new IllegalArgumentException(enclosing.toString());
|
||||
if (!outer.isParameterized())
|
||||
return getDeclaredType(typeElem, typeArgs);
|
||||
|
||||
return getDeclaredType0(outer, sym, typeArgs);
|
||||
}
|
||||
// where
|
||||
private DeclaredType getDeclaredType0(Type outer,
|
||||
ClassSymbol sym,
|
||||
TypeMirror... typeArgs) {
|
||||
if (typeArgs.length != sym.type.getTypeArguments().length())
|
||||
throw new IllegalArgumentException(
|
||||
"Incorrect number of type arguments");
|
||||
|
||||
ListBuffer<Type> targs = new ListBuffer<Type>();
|
||||
for (TypeMirror t : typeArgs) {
|
||||
if (!(t instanceof ReferenceType || t instanceof WildcardType))
|
||||
throw new IllegalArgumentException(t.toString());
|
||||
targs.append((Type) t);
|
||||
}
|
||||
// TODO: Would like a way to check that type args match formals.
|
||||
|
||||
return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of an element when that element is viewed as
|
||||
* a member of, or otherwise directly contained by, a given type.
|
||||
* For example,
|
||||
* when viewed as a member of the parameterized type {@code Set<String>},
|
||||
* the {@code Set.add} method is an {@code ExecutableType}
|
||||
* whose parameter is of type {@code String}.
|
||||
*
|
||||
* @param containing the containing type
|
||||
* @param element the element
|
||||
* @return the type of the element as viewed from the containing type
|
||||
* @throws IllegalArgumentException if the element is not a valid one
|
||||
* for the given type
|
||||
*/
|
||||
public TypeMirror asMemberOf(DeclaredType containing, Element element) {
|
||||
Type site = (Type)containing;
|
||||
Symbol sym = (Symbol)element;
|
||||
if (types.asSuper(site, sym.getEnclosingElement()) == null)
|
||||
throw new IllegalArgumentException(sym + "@" + site);
|
||||
return types.memberType(site, sym);
|
||||
}
|
||||
|
||||
|
||||
private static final Set<TypeKind> EXEC_OR_PKG =
|
||||
EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE);
|
||||
|
||||
/**
|
||||
* Throws an IllegalArgumentException if a type's kind is one of a set.
|
||||
*/
|
||||
private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
|
||||
if (invalidKinds.contains(t.getKind()))
|
||||
throw new IllegalArgumentException(t.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an object cast to the specified type.
|
||||
* @throws NullPointerException if the object is {@code null}
|
||||
* @throws IllegalArgumentException if the object is of the wrong type
|
||||
*/
|
||||
private static <T> T cast(Class<T> clazz, Object o) {
|
||||
if (! clazz.isInstance(o))
|
||||
throw new IllegalArgumentException(o.toString());
|
||||
return clazz.cast(o);
|
||||
}
|
||||
|
||||
public Set<MethodSymbol> getOverriddenMethods(Element elem) {
|
||||
if (elem.getKind() != ElementKind.METHOD
|
||||
|| elem.getModifiers().contains(Modifier.STATIC)
|
||||
|| elem.getModifiers().contains(Modifier.PRIVATE))
|
||||
return Collections.emptySet();
|
||||
|
||||
if (!(elem instanceof MethodSymbol))
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
MethodSymbol m = (MethodSymbol) elem;
|
||||
ClassSymbol origin = (ClassSymbol) m.owner;
|
||||
|
||||
Set<MethodSymbol> results = new LinkedHashSet<MethodSymbol>();
|
||||
for (Type t : types.closure(origin.type)) {
|
||||
if (t != origin.type) {
|
||||
ClassSymbol c = (ClassSymbol) t.tsym;
|
||||
for (Scope.Entry e = c.members().lookup(m.name); e.scope != null; e = e.next()) {
|
||||
if (e.sym.kind == Kinds.MTH && m.overrides(e.sym, origin, types, true)) {
|
||||
results.add((MethodSymbol) e.sym);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user