本文整理汇总了Java中javax.lang.model.util.Types.getDeclaredType方法的典型用法代码示例。如果您正苦于以下问题:Java Types.getDeclaredType方法的具体用法?Java Types.getDeclaredType怎么用?Java Types.getDeclaredType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.lang.model.util.Types
的用法示例。
在下文中一共展示了Types.getDeclaredType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fillSubTypes
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private List<DeclaredType> fillSubTypes(CompilationController cc, DeclaredType dType) {
List<DeclaredType> subtypes = new ArrayList<>();
//Set<? extends SearchScopeType> scope = Collections.singleton(new ClassSearchScopeType(prefix));
Types types = cc.getTypes();
if (prefix != null && prefix.length() > 2 && lastPrefixDot < 0) {
//Trees trees = cc.getTrees();
ClassIndex.NameKind kind = ClassIndex.NameKind.CASE_INSENSITIVE_PREFIX;
for (ElementHandle<TypeElement> handle : cpi.getClassIndex().getDeclaredTypes(prefix, kind, EnumSet.allOf(ClassIndex.SearchScope.class))) {
TypeElement te = handle.resolve(cc);
if (te != null && /*trees.isAccessible(scope, te) &&*/ types.isSubtype(types.getDeclaredType(te), dType)) {
subtypes.add(types.getDeclaredType(te));
}
}
} else {
HashSet<TypeElement> elems = new HashSet<>();
LinkedList<DeclaredType> bases = new LinkedList<>();
bases.add(dType);
ClassIndex index = cpi.getClassIndex();
while (!bases.isEmpty()) {
DeclaredType head = bases.remove();
TypeElement elem = (TypeElement) head.asElement();
if (!elems.add(elem)) {
continue;
}
if (accept(elem)) {
subtypes.add(head);
}
//List<? extends TypeMirror> tas = head.getTypeArguments();
//boolean isRaw = !tas.iterator().hasNext();
for (ElementHandle<TypeElement> eh : index.getElements(ElementHandle.create(elem), EnumSet.of(ClassIndex.SearchKind.IMPLEMENTORS), EnumSet.allOf(ClassIndex.SearchScope.class))) {
TypeElement e = eh.resolve(cc);
if (e != null) {
DeclaredType dt = types.getDeclaredType(e);
bases.add(dt);
}
}
}
}
return subtypes;
}
示例2: testAddImport3
import javax.lang.model.util.Types; //导入方法依赖的package包/类
public void testAddImport3() throws IOException {
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
MethodTree node = (MethodTree) clazz.getMembers().get(0);
BlockTree body = node.getBody();
List<StatementTree> stats = new ArrayList<StatementTree>();
for (StatementTree st : body.getStatements()) {
stats.add(st);
}
TypeElement list = workingCopy.getElements().getTypeElement("java.util.List");
TypeElement collection = workingCopy.getElements().getTypeElement("java.util.Collection");
Types types = workingCopy.getTypes();
TypeMirror tm = types.getDeclaredType(list, types.erasure(collection.asType()));
stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "utilList", make.Type(tm), null));
workingCopy.rewrite(body, make.Block(stats, false));
}
};
src.runModificationTask(task).commit();
assertFiles("testAddImport3.pass");
}
示例3: testAddImport4
import javax.lang.model.util.Types; //导入方法依赖的package包/类
public void testAddImport4() throws IOException {
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
MethodTree node = (MethodTree) clazz.getMembers().get(0);
BlockTree body = node.getBody();
List<StatementTree> stats = new ArrayList<StatementTree>();
for (StatementTree st : body.getStatements()) {
stats.add(st);
}
TypeElement list = workingCopy.getElements().getTypeElement("java.util.List");
TypeElement collection = workingCopy.getElements().getTypeElement("java.util.Collection");
Types types = workingCopy.getTypes();
TypeMirror tm = types.getDeclaredType(list, types.getWildcardType(types.erasure(collection.asType()), null));
stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "utilList", make.Type(tm), null));
workingCopy.rewrite(body, make.Block(stats, false));
}
};
src.runModificationTask(task).commit();
assertFiles("testAddImport4.pass");
}
示例4: testAddImport5
import javax.lang.model.util.Types; //导入方法依赖的package包/类
public void testAddImport5() throws IOException {
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
MethodTree node = (MethodTree) clazz.getMembers().get(0);
BlockTree body = node.getBody();
List<StatementTree> stats = new ArrayList<StatementTree>();
for (StatementTree st : body.getStatements()) {
stats.add(st);
}
TypeElement list = workingCopy.getElements().getTypeElement("java.util.List");
TypeElement collection = workingCopy.getElements().getTypeElement("java.util.Collection");
Types types = workingCopy.getTypes();
TypeMirror tm = types.getDeclaredType(list, types.getWildcardType(null, types.erasure(collection.asType())));
stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "utilList", make.Type(tm), null));
workingCopy.rewrite(body, make.Block(stats, false));
}
};
src.runModificationTask(task).commit();
assertFiles("testAddImport5.pass");
}
示例5: visitDeclared
import javax.lang.model.util.Types; //导入方法依赖的package包/类
@Override
public TypeMirror visitDeclared(DeclaredType t, Types types) {
if (TypeModeler.isSubElement((TypeElement) t.asElement(), collectionType)
|| TypeModeler.isSubElement((TypeElement) t.asElement(), mapType)) {
Collection<? extends TypeMirror> args = t.getTypeArguments();
TypeMirror[] safeArgs = new TypeMirror[args.size()];
int i = 0;
for (TypeMirror arg : args) {
safeArgs[i++] = visit(arg, types);
}
return types.getDeclaredType((TypeElement) t.asElement(), safeArgs);
}
return types.erasure(t);
}
示例6: getCorrectedReturnType
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private TypeMirror getCorrectedReturnType(Env env, ExecutableType et, ExecutableElement el, TypeMirror site) {
TypeMirror type = et.getReturnType();
if (site != null && site.getKind() == TypeKind.DECLARED) {
if ("getClass".contentEquals(el.getSimpleName()) && et.getParameterTypes().isEmpty() //NOI18N
&& type.getKind() == TypeKind.DECLARED
&& JAVA_LANG_CLASS.contentEquals(((TypeElement) ((DeclaredType) type).asElement()).getQualifiedName())
&& ((TypeElement) ((DeclaredType) type).asElement()).getTypeParameters().size() == 1) {
Types types = env.getController().getTypes();
type = types.getDeclaredType((TypeElement) ((DeclaredType) type).asElement(), types.getWildcardType(site, null));
}
}
return type;
}
示例7: getDeclaredType
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private DeclaredType getDeclaredType(TypeElement e, HashMap<? extends Element, ? extends TypeMirror> map, Types types) {
List<? extends TypeParameterElement> tpes = e.getTypeParameters();
TypeMirror[] targs = new TypeMirror[tpes.size()];
int i = 0;
for (Iterator<? extends TypeParameterElement> it = tpes.iterator(); it.hasNext();) {
TypeParameterElement tpe = it.next();
TypeMirror t = map.get(tpe);
targs[i++] = t != null ? t : tpe.asType();
}
Element encl = e.getEnclosingElement();
if ((encl.getKind().isClass() || encl.getKind().isInterface()) && !((TypeElement) encl).getTypeParameters().isEmpty()) {
return types.getDeclaredType(getDeclaredType((TypeElement) encl, map, types), e, targs);
}
return types.getDeclaredType(e, targs);
}
示例8: getDeclaredType
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private DeclaredType getDeclaredType(TypeElement e, HashMap<? extends Element, ? extends TypeMirror> map, Types types) {
List<? extends TypeParameterElement> tpes = e.getTypeParameters();
TypeMirror[] targs = new TypeMirror[tpes.size()];
int i = 0;
for (Iterator<? extends TypeParameterElement> it = tpes.iterator(); it.hasNext();) {
TypeParameterElement tpe = it.next();
TypeMirror t = map.get(tpe);
targs[i++] = t != null ? t : tpe.asType();
}
Element encl = e.getEnclosingElement();
if ((encl.getKind().isClass() || encl.getKind().isInterface()) && !((TypeElement)encl).getTypeParameters().isEmpty())
return types.getDeclaredType(getDeclaredType((TypeElement)encl, map, types), e, targs);
return types.getDeclaredType(e, targs);
}
示例9: isParcelableArrayList
import javax.lang.model.util.Types; //导入方法依赖的package包/类
public static boolean isParcelableArrayList(Types typeUtils, Elements elementUtils, Element element) {
TypeElement typeArrayList = elementUtils.getTypeElement(ArrayList.class.getName());
TypeElement typeParcelable = elementUtils.getTypeElement(Parcelable.class.getName());
WildcardType wildcardType = typeUtils.getWildcardType(typeParcelable.asType(), null);
DeclaredType declaredType = typeUtils.getDeclaredType(typeArrayList, wildcardType);
return typeUtils.isSubtype(element.asType(), declaredType);
}
示例10: erasure
import javax.lang.model.util.Types; //导入方法依赖的package包/类
public TypeMirror erasure(TypeMirror t) {
Types tu = env.getTypeUtils();
t = tu.erasure(t);
if (t.getKind().equals(TypeKind.DECLARED)) {
DeclaredType dt = (DeclaredType)t;
if (!dt.getTypeArguments().isEmpty())
return tu.getDeclaredType((TypeElement) dt.asElement());
}
return t;
}
示例11: verifyHintMethod
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private boolean verifyHintMethod(ExecutableElement method) {
StringBuilder error = new StringBuilder();
Elements elements = processingEnv.getElementUtils();
TypeElement errDesc = elements.getTypeElement("org.netbeans.spi.editor.hints.ErrorDescription");
TypeElement jlIterable = elements.getTypeElement("java.lang.Iterable");
TypeElement hintCtx = elements.getTypeElement("org.netbeans.spi.java.hints.HintContext");
if (errDesc == null || jlIterable == null || hintCtx == null) {
return true;
}
Types types = processingEnv.getTypeUtils();
TypeMirror errDescType = errDesc.asType(); //no type params, no need to erasure
TypeMirror jlIterableErrDesc = types.getDeclaredType(jlIterable, errDescType);
TypeMirror ret = method.getReturnType();
if (!types.isSameType(ret, errDescType) && !types.isAssignable(ret, jlIterableErrDesc)) {
error.append(ERR_RETURN_TYPE);
error.append("\n");
}
if (method.getParameters().size() != 1 || !types.isSameType(method.getParameters().get(0).asType(), hintCtx.asType())) {
error.append(ERR_PARAMETERS);
error.append("\n");
}
if (!method.getModifiers().contains(Modifier.STATIC)) {
error.append(ERR_MUST_BE_STATIC);
error.append("\n");
}
if (error.length() == 0) {
return true;
}
if (error.charAt(error.length() - 1) == '\n') {
error.delete(error.length() - 1, error.length());
}
processingEnv.getMessager().printMessage(Kind.ERROR, error.toString(), method);
return false;
}
示例12: getCorpusModule
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private static final TypeMirror getCorpusModule(Types typeUtils, Elements elementUtils) {
TypeElement moduleTypeElement = elementUtils.getTypeElement(Module.class.getCanonicalName());
TypeElement corpusTypeElement = elementUtils.getTypeElement(Corpus.class.getCanonicalName());
TypeMirror corpusTypeMirror = corpusTypeElement.asType();
return typeUtils.getDeclaredType(moduleTypeElement, corpusTypeMirror);
}
示例13: getModuleBase
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private static final TypeMirror getModuleBase(Types typeUtils, Elements elementUtils) {
TypeElement moduleBaseTypeElement = elementUtils.getTypeElement(ModuleBase.class.getCanonicalName());
WildcardType wildcard = typeUtils.getWildcardType(null, null);
return typeUtils.getDeclaredType(moduleBaseTypeElement, wildcard);
}
示例14: getElementList
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private static final TypeMirror getElementList(Types typeUtils, Elements elementUtils) {
TypeElement listTypeElement = getTypeElement(elementUtils, List.class);
TypeMirror elementTypeMirror = getTypeMirror(elementUtils, fr.inra.maiage.bibliome.alvisnlp.core.corpus.Element.class);
return typeUtils.getDeclaredType(listTypeElement, elementTypeMirror);
}
示例15: getElementIterator
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private static final TypeMirror getElementIterator(Types typeUtils, Elements elementUtils) {
TypeElement listTypeElement = getTypeElement(elementUtils, Iterator.class);
TypeMirror elementTypeMirror = getTypeMirror(elementUtils, fr.inra.maiage.bibliome.alvisnlp.core.corpus.Element.class);
return typeUtils.getDeclaredType(listTypeElement, elementTypeMirror);
}