本文整理汇总了Java中javax.lang.model.util.Types.isSubtype方法的典型用法代码示例。如果您正苦于以下问题:Java Types.isSubtype方法的具体用法?Java Types.isSubtype怎么用?Java Types.isSubtype使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.lang.model.util.Types
的用法示例。
在下文中一共展示了Types.isSubtype方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: detectKind
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private static KindOfType detectKind(CompilationInfo info, TypeMirror tm) {
if (tm.getKind().isPrimitive()) {
return KindOfType.valueOf(tm.getKind().name());
}
if (tm.getKind() == TypeKind.ARRAY) {
return ((ArrayType) tm).getComponentType().getKind().isPrimitive() ? KindOfType.ARRAY_PRIMITIVE : KindOfType.ARRAY;
}
if (tm.getKind() == TypeKind.DECLARED) {
Types t = info.getTypes();
TypeElement en = info.getElements().getTypeElement("java.lang.Enum");
if (en != null) {
if (t.isSubtype(tm, t.erasure(en.asType()))) {
return KindOfType.ENUM;
}
}
if (((DeclaredType) tm).asElement().getKind().isClass() && ((TypeElement) ((DeclaredType) tm).asElement()).getQualifiedName().contentEquals("java.lang.String")) {
return KindOfType.STRING;
}
}
return KindOfType.OTHER;
}
示例2: typeToImport
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private static TypeMirror typeToImport(CompilationInfo info, TreePath tp, TypeMirror origin) {
if (tp.getLeaf().getKind() == Tree.Kind.MEMBER_SELECT) {
MemberSelectTree mst = (MemberSelectTree) tp.getLeaf();
if (mst.getExpression().getKind() == Tree.Kind.IDENTIFIER) {
ClassIndex index = info.getClasspathInfo().getClassIndex();
Types types = info.getTypes();
Trees trees = info.getTrees();
Scope scope = trees.getScope(tp);
for (ElementHandle<TypeElement> teHandle : index.getDeclaredTypes(((IdentifierTree)mst.getExpression()).getName().toString(), ClassIndex.NameKind.SIMPLE_NAME, EnumSet.allOf(ClassIndex.SearchScope.class))) {
TypeElement te = teHandle.resolve(info);
if (te != null && trees.isAccessible(scope, te)) {
TypeMirror toImport = te.asType();
if (types.isSubtype(toImport, origin)) {
return toImport;
}
}
}
}
}
return origin;
}
示例3: sameMethod
import javax.lang.model.util.Types; //导入方法依赖的package包/类
protected boolean sameMethod(ExecutableElement method1, ExecutableElement method2) {
if (!method1.getSimpleName().equals(method2.getSimpleName()))
return false;
Types typeUtils = builder.getProcessingEnvironment().getTypeUtils();
if(!typeUtils.isSameType(method1.getReturnType(), method2.getReturnType())
&& !typeUtils.isSubtype(method2.getReturnType(), method1.getReturnType()))
return false;
List<? extends VariableElement> parameters1 = method1.getParameters();
List<? extends VariableElement> parameters2 = method2.getParameters();
if (parameters1.size() != parameters2.size())
return false;
for (int i = 0; i < parameters1.size(); i++) {
if (!typeUtils.isSameType(parameters1.get(i).asType(), parameters2.get(i).asType()))
return false;
}
return true;
}
示例4: throwsNonRuntimeExceptions
import javax.lang.model.util.Types; //导入方法依赖的package包/类
/**
*/
private static boolean throwsNonRuntimeExceptions(CompilationInfo compInfo,
ExecutableElement method) {
List<? extends TypeMirror> thrownTypes = method.getThrownTypes();
if (thrownTypes.isEmpty()) {
return false;
}
String runtimeExcName = "java.lang.RuntimeException"; //NOI18N
TypeElement runtimeExcElement = compInfo.getElements()
.getTypeElement(runtimeExcName);
if (runtimeExcElement == null) {
Logger.getLogger("testng").log( //NOI18N
Level.WARNING,
"Could not find TypeElement for " //NOI18N
+ runtimeExcName);
return true;
}
Types types = compInfo.getTypes();
TypeMirror runtimeExcType = runtimeExcElement.asType();
for (TypeMirror exceptionType : thrownTypes) {
if (!types.isSubtype(exceptionType, runtimeExcType)) {
return true;
}
}
return false;
}
示例5: findCommonSubtype
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private DeclaredType findCommonSubtype(DeclaredType type1, DeclaredType type2, Env<AttrContext> env) {
List<DeclaredType> subtypes1 = getSubtypes(type1, env);
List<DeclaredType> subtypes2 = getSubtypes(type2, env);
if (subtypes1 == null || subtypes2 == null) return null;
Types types = info.getTypes();
for (DeclaredType subtype1 : subtypes1) {
for (DeclaredType subtype2 : subtypes2) {
if (types.isSubtype(subtype1, subtype2))
return subtype1;
if (types.isSubtype(subtype2, subtype1))
return subtype2;
}
}
return null;
}
示例6: throwsNonRuntimeExceptions
import javax.lang.model.util.Types; //导入方法依赖的package包/类
/**
*/
private static boolean throwsNonRuntimeExceptions(CompilationInfo compInfo,
ExecutableElement method) {
List<? extends TypeMirror> thrownTypes = method.getThrownTypes();
if (thrownTypes.isEmpty()) {
return false;
}
String runtimeExcName = "java.lang.RuntimeException"; //NOI18N
TypeElement runtimeExcElement = compInfo.getElements()
.getTypeElement(runtimeExcName);
if (runtimeExcElement == null) {
Logger.getLogger("junit").log( //NOI18N
Level.WARNING,
"Could not find TypeElement for " //NOI18N
+ runtimeExcName);
return true;
}
Types types = compInfo.getTypes();
TypeMirror runtimeExcType = runtimeExcElement.asType();
for (TypeMirror exceptionType : thrownTypes) {
if (!types.isSubtype(exceptionType, runtimeExcType)) {
return true;
}
}
return false;
}
示例7: visitMethodInvocation
import javax.lang.model.util.Types; //导入方法依赖的package包/类
@Override
public Tree visitMethodInvocation(MethodInvocationTree node, Element p) {
List<? extends ExpressionTree> arguments = node.getArguments();
for (int i = 0; i < arguments.size(); i++) {
ExpressionTree argument = arguments.get(i);
Element argElement = asElement(argument); // TODO: Slow and misses ternary expressions
if(p.equals(argElement)) {
Element element = asElement(node);
if (element.getKind() == ElementKind.METHOD) {
ExecutableElement method = (ExecutableElement) element;
VariableElement parameter = method.getParameters().get(i);
Types types = workingCopy.getTypes();
TypeMirror parameterType = parameter.asType();
if(parameterType.getKind().equals(TypeKind.TYPEVAR)) {
TypeVariable typeVariable = (TypeVariable) parameterType;
TypeMirror upperBound = typeVariable.getUpperBound();
TypeMirror lowerBound = typeVariable.getLowerBound();
if(upperBound != null && !types.isSubtype(superTypeElement.asType(), upperBound)) {
isReplCandidate = false;
}
if(lowerBound != null && !types.isSubtype(lowerBound, superTypeElement.asType())) {
isReplCandidate = false;
}
} else if(!types.isAssignable(superTypeElement.asType(), parameterType)) {
isReplCandidate = false;
}
}
}
}
return super.visitMethodInvocation(node, p);
}
示例8: isRefactorable
import javax.lang.model.util.Types; //导入方法依赖的package包/类
public Boolean isRefactorable() {
prospectives = this.getListRepresentation(loop.getStatement(), true);
if (prospectives != null && !prospectives.isEmpty()) {
prospectives.get(prospectives.size() - 1).eagerize();
if (this.untrasformable) {
return false;
}
for ( int i = 0; i < prospectives.size() - 1; i++) {
if (!prospectives.get(i).isLazy()) {
return false;
}
}
hasIterable = false;
VariableTree var = loop.getVariable();
TypeElement el = workingCopy.getElements().getTypeElement("java.lang.Iterable"); // NOI18N
if (el != null) {
TreePath path = TreePath.getPath(workingCopy.getCompilationUnit(), loop.getExpression());
TypeMirror m = workingCopy.getTrees().getTypeMirror(path);
Types types = workingCopy.getTypes();
hasIterable =
types.isSubtype(
types.erasure(m),
types.erasure(el.asType())
);
}
prospectives = ProspectiveOperation.mergeIntoComposableOperations(prospectives);
return prospectives != null;
} else {
return false;
}
}
示例9: findServiceAnnotation
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private List<TypeMirror> findServiceAnnotation(Element e) throws LayerGenerationException {
for (AnnotationMirror ann : e.getAnnotationMirrors()) {
if (!ProjectServiceProvider.class.getName().equals(ann.getAnnotationType().toString())) {
continue;
}
for (Map.Entry<? extends ExecutableElement,? extends AnnotationValue> attr : ann.getElementValues().entrySet()) {
if (!attr.getKey().getSimpleName().contentEquals("service")) {
continue;
}
List<TypeMirror> r = new ArrayList<TypeMirror>();
for (Object item : (List<?>) attr.getValue().getValue()) {
TypeMirror type = (TypeMirror) ((AnnotationValue) item).getValue();
Types typeUtils = processingEnv.getTypeUtils();
for (TypeMirror otherType : r) {
for (boolean swap : new boolean[] {false, true}) {
TypeMirror t1 = swap ? type : otherType;
TypeMirror t2 = swap ? otherType : type;
if (typeUtils.isSubtype(t1, t2)) {
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "registering under both " + typeUtils.asElement(t2).getSimpleName() + " and its subtype " + typeUtils.asElement(t1).getSimpleName() + " will not work if LookupMerger<" + typeUtils.asElement(t2).getSimpleName() + "> is used (#205151)", e, ann, attr.getValue());
}
}
}
r.add(type);
}
return r;
}
throw new LayerGenerationException("No service attr found", e);
}
throw new LayerGenerationException("No @ProjectServiceProvider found", e);
}
示例10: 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);
}
示例11: addEffectivelyFinalAutoCloseables
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private void addEffectivelyFinalAutoCloseables(final Env env) throws IOException {
final CompilationController controller = env.getController();
final Elements elements = controller.getElements();
final TypeElement te = elements.getTypeElement("java.lang.AutoCloseable"); //NOI18N
if (te != null) {
final Types types = controller.getTypes();
final ElementUtilities eu = controller.getElementUtilities();
final Scope scope = env.getScope();
final Set<? extends TypeMirror> smartTypes = options.contains(Options.ALL_COMPLETION) ? null : getSmartTypes(env);
final TypeElement enclClass = scope.getEnclosingClass();
for (Element e : getLocalMembersAndVars(env)) {
switch (e.getKind()) {
case EXCEPTION_PARAMETER:
case LOCAL_VARIABLE:
case RESOURCE_VARIABLE:
case PARAMETER:
if (types.isSubtype(e.asType(), te.asType()) && eu.isEffectivelyFinal((VariableElement) e)) {
results.add(itemFactory.createVariableItem(env.getController(), (VariableElement) e, e.asType(), anchorOffset, null, env.getScope().getEnclosingClass() != e.getEnclosingElement(), elements.isDeprecated(e), isOfSmartType(env, e.asType(), smartTypes), env.assignToVarPos()));
}
break;
case FIELD:
if (types.isSubtype(e.asType(), te.asType())) {
String name = e.getSimpleName().toString();
if (THIS_KEYWORD.equals(name) || SUPER_KEYWORD.equals(name)) {
results.add(itemFactory.createKeywordItem(name, null, anchorOffset, isOfSmartType(env, e.asType(), smartTypes)));
} else {
TypeMirror tm = asMemberOf(e, enclClass != null ? enclClass.asType() : null, types);
results.add(itemFactory.createVariableItem(env.getController(), (VariableElement) e, tm, anchorOffset, null, env.getScope().getEnclosingClass() != e.getEnclosingElement(), elements.isDeprecated(e), isOfSmartType(env, tm, smartTypes), env.assignToVarPos()));
}
}
break;
}
}
}
}
示例12: asMemberOf
import javax.lang.model.util.Types; //导入方法依赖的package包/类
TypeMirror asMemberOf(Element element, TypeMirror type, Types types) {
TypeMirror ret = element.asType();
TypeMirror enclType = element.getEnclosingElement().asType();
if (enclType.getKind() == TypeKind.DECLARED) {
enclType = types.erasure(enclType);
}
while (type != null && type.getKind() == TypeKind.DECLARED) {
if ((enclType.getKind() != TypeKind.DECLARED || ((DeclaredType) enclType).asElement().getSimpleName().length() > 0) && types.isSubtype(type, enclType)) {
ret = types.asMemberOf((DeclaredType) type, element);
break;
}
type = ((DeclaredType) type).getEnclosingType();
}
return ret;
}
示例13: enumHint
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private static ErrorDescription enumHint(HintContext ctx, String baseName, String targetTypeName, String key, Fix... fixes) {
Element type = ctx.getInfo().getTrees().getElement(ctx.getVariables().get("$param"));
if (type == null || type.getKind() != ElementKind.ENUM) {
return null;
}
Element coll = ctx.getInfo().getTrees().getElement(ctx.getVariables().get("$coll"));
if (coll == null || coll.getKind() != ElementKind.CLASS) {
return null;
}
TypeElement base = ctx.getInfo().getElements().getTypeElement(baseName);
if (base == null) {
return null;
}
Types t = ctx.getInfo().getTypes();
if (!t.isSubtype(t.erasure(coll.asType()), t.erasure(base.asType()))) {
return null;
}
if (targetTypeName != null) {
TypeElement target = ctx.getInfo().getElements().getTypeElement(targetTypeName);
if (target == null) {
return null;
}
if (t.isSubtype(t.erasure(coll.asType()), t.erasure(target.asType()))) {
return null;
}
List<? extends TypeMirror> assignedTo = CreateElementUtilities.resolveType(EnumSet.noneOf(ElementKind.class), ctx.getInfo(), ctx.getPath().getParentPath(), ctx.getPath().getLeaf(), (int) ctx.getInfo().getTrees().getSourcePositions().getEndPosition(ctx.getPath().getCompilationUnit(), ctx.getPath().getLeaf()), new TypeMirror[1], new int[1]);
if (assignedTo != null && assignedTo.size() == 1) {
if (t.isSubtype(t.erasure(assignedTo.get(0)), t.erasure(coll.asType())))
return null;
}
}
String displayName = NbBundle.getMessage(Tiny.class, key);
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), displayName, fixes);
}
示例14: isInActivity
import javax.lang.model.util.Types; //导入方法依赖的package包/类
public static boolean isInActivity(Elements elements, Types types, TypeElement enclosingElement) {
TypeMirror typeActivity = elements.getTypeElement(Constant.ACTIVITY).asType();
return types.isSubtype(enclosingElement.asType(), typeActivity);
}
示例15: createBindings
import javax.lang.model.util.Types; //导入方法依赖的package包/类
private Map<String, Object> createBindings(TypeElement clazz, ExecutableElement element) {
CodeStyle cs = DiffContext.getCodeStyle(copy);
Map<String, Object> bindings = new HashMap<>();
if (clazz != null) {
bindings.put(CLASS_NAME, clazz.getQualifiedName().toString());
bindings.put(SIMPLE_CLASS_NAME, clazz.getSimpleName().toString());
}
if (element != null) {
bindings.put(METHOD_NAME, element.getSimpleName().toString());
bindings.put(METHOD_RETURN_TYPE, element.getReturnType().toString()); //NOI18N
Object value;
switch(element.getReturnType().getKind()) {
case BOOLEAN:
value = "false"; //NOI18N
break;
case BYTE:
case CHAR:
case DOUBLE:
case FLOAT:
case INT:
case LONG:
case SHORT:
value = 0;
break;
default:
value = "null"; //NOI18N
}
bindings.put(DEFAULT_RETURN_TYPE_VALUE, value);
}
if (clazz != null && element != null) {
StringBuilder sb = new StringBuilder();
if (element.isDefault() && element.getEnclosingElement().getKind().isInterface()) {
Types types = copy.getTypes();
Context ctx = ((JavacTaskImpl) copy.impl.getJavacTask()).getContext();
com.sun.tools.javac.code.Types typesImpl = com.sun.tools.javac.code.Types.instance(ctx);
TypeMirror enclType = typesImpl.asSuper((Type)clazz.asType(), ((Type)element.getEnclosingElement().asType()).tsym);
if (!types.isSubtype(clazz.getSuperclass(), enclType)) {
TypeMirror selected = enclType;
for (TypeMirror iface : clazz.getInterfaces()) {
if (types.isSubtype(iface, selected) &&
!types.isSameType(iface, enclType)) {
selected = iface;
break;
}
}
sb.append(((DeclaredType)selected).asElement().getSimpleName()).append('.');
}
}
sb.append("super.").append(element.getSimpleName()).append('('); //NOI18N
for (Iterator<? extends VariableElement> it = element.getParameters().iterator(); it.hasNext();) {
VariableElement ve = it.next();
sb.append(addParamPrefixSuffix(removeParamPrefixSuffix(ve, cs), cs));
if (it.hasNext())
sb.append(","); //NOI18N
}
sb.append(')'); //NOI18N
bindings.put(SUPER_METHOD_CALL, sb);
}
return bindings;
}