本文整理汇总了Java中com.sun.tools.javac.code.Types.isAssignable方法的典型用法代码示例。如果您正苦于以下问题:Java Types.isAssignable方法的具体用法?Java Types.isAssignable怎么用?Java Types.isAssignable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.code.Types
的用法示例。
在下文中一共展示了Types.isAssignable方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: linkedListFix
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
private static Optional<Fix> linkedListFix(Tree tree, VisitorState state) {
Type type = getTargetType(state);
if (type == null) {
return Optional.empty();
}
Types types = state.getTypes();
for (String replacement : ImmutableList.of("java.util.ArrayList", "java.util.ArrayDeque")) {
Symbol sym = state.getSymbolFromString(replacement);
if (sym == null) {
continue;
}
if (types.isAssignable(types.erasure(sym.asType()), types.erasure(type))) {
SuggestedFix.Builder fix = SuggestedFix.builder();
while (tree instanceof ParameterizedTypeTree) {
tree = ((ParameterizedTypeTree) tree).getType();
}
fix.replace(tree, SuggestedFixes.qualifyType(state, fix, sym));
return Optional.of(fix.build());
}
}
return Optional.empty();
}
示例2: handleMethodCall
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
private void handleMethodCall(final JCMethodInvocation methodCall) {
JavacNode methodCallNode = annotationNode.getAst().get(methodCall);
if (methodCallNode == null) {
// This should mean the node does not exist in the source at all. This is the case for generated nodes, such as implicit super() calls.
return;
}
JavacNode surroundingType = upToTypeNode(methodCallNode);
TypeSymbol surroundingTypeSymbol = ((JCClassDecl)surroundingType.get()).sym;
JCExpression receiver = receiverOf(methodCall);
String methodName = methodNameOf(methodCall);
if ("this".equals(methodName) || "super".equals(methodName)) return;
Type resolvedMethodCall = CLASS_AND_METHOD.resolveMember(methodCallNode, methodCall);
if (resolvedMethodCall == null) return;
if (!suppressBaseMethods && !(resolvedMethodCall instanceof ErrorType)) return;
Type receiverType = CLASS_AND_METHOD.resolveMember(methodCallNode, receiver);
if (receiverType == null) return;
if (receiverType.tsym.toString().endsWith(receiver.toString())) return;
Types types = Types.instance(annotationNode.getContext());
for (Extension extension : extensions) {
TypeSymbol extensionProvider = extension.extensionProvider;
if (surroundingTypeSymbol == extensionProvider) continue;
for (MethodSymbol extensionMethod : extension.extensionMethods) {
if (!methodName.equals(extensionMethod.name.toString())) continue;
Type extensionMethodType = extensionMethod.type;
if (!MethodType.class.isInstance(extensionMethodType) && !ForAll.class.isInstance(extensionMethodType)) continue;
Type firstArgType = types.erasure(extensionMethodType.asMethodType().argtypes.get(0));
if (!types.isAssignable(receiverType, firstArgType)) continue;
methodCall.args = methodCall.args.prepend(receiver);
methodCall.meth = chainDotsString(annotationNode, extensionProvider.toString() + "." + methodName);
return;
}
}
}
示例3: greatestLowerBound
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
/**
* Returns the greatest lower bound of two {@link TypeMirror}s.
*
* @param processingEnv The {@link ProcessingEnvironment} to use.
* @param tm1 A {@link TypeMirror}.
* @param tm2 A {@link TypeMirror}.
* @return The greatest lower bound of {@code tm1} and {@code tm2}.
*/
public static TypeMirror greatestLowerBound(
ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
Type t1 = (Type) tm1;
Type t2 = (Type) tm2;
JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
Types types = Types.instance(javacEnv.getContext());
if (types.isSameType(t1, t2)) {
// Special case if the two types are equal.
return t1;
}
// Handle the 'null' type manually.
if (t1.getKind() == TypeKind.NULL) {
return t1;
}
if (t2.getKind() == TypeKind.NULL) {
return t2;
}
// Special case for primitives.
if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
if (types.isAssignable(t1, t2)) {
return t1;
} else if (types.isAssignable(t2, t1)) {
return t2;
} else {
// Javac types.glb returns TypeKind.Error when the GLB does
// not exist, but we can't create one. Use TypeKind.NONE
// instead.
return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
}
}
if (t1.getKind() == TypeKind.WILDCARD) {
return t2;
}
if (t2.getKind() == TypeKind.WILDCARD) {
return t1;
}
return types.glb(t1, t2);
}
示例4: matchMethodInvocation
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!CONTAINS_MATCHER.matches(tree, state)) {
return Description.NO_MATCH;
}
Description.Builder result = buildDescription(tree);
// If the collection is not raw, try to figure out if the argument looks like a key
// or a value.
List<Type> tyargs = ASTHelpers.getReceiverType(tree).getTypeArguments();
if (tyargs.size() == 2) {
// map capture variables to their bounds, e.g. `? extends Number` -> `Number`
Types types = state.getTypes();
Type key = ASTHelpers.getUpperBound(tyargs.get(0), types);
Type value = ASTHelpers.getUpperBound(tyargs.get(1), types);
Type arg = ASTHelpers.getType(Iterables.getOnlyElement(tree.getArguments()));
boolean valueShaped = types.isAssignable(arg, value);
boolean keyShaped = types.isAssignable(arg, key);
if (keyShaped && !valueShaped) {
// definitely a key
result.addFix(replaceMethodName(tree, state, "containsKey"));
result.setMessage(
String.format(
"contains() is a legacy method that is equivalent to containsValue(), but the "
+ "argument type '%s' looks like a key",
key));
} else if (valueShaped && !keyShaped) {
// definitely a value
result.addFix(replaceMethodName(tree, state, "containsValue"));
} else if (valueShaped && keyShaped) {
// ambiguous
result.addFix(replaceMethodName(tree, state, "containsValue"));
result.addFix(replaceMethodName(tree, state, "containsKey"));
result.setMessage(
String.format(
"contains() is a legacy method that is equivalent to containsValue(), but the "
+ "argument type '%s' could be a key or a value",
key));
} else {
// this shouldn't have compiled!
throw new AssertionError(
String.format(
"unexpected argument to contains(): key: %s, value: %s, argument: %s",
key, value, arg));
}
} else {
result.addFix(replaceMethodName(tree, state, "containsValue"));
}
return result.build();
}
示例5: greatestLowerBound
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
/**
* Returns the greatest lower bound of two {@link TypeMirror}s, ignoring any annotations on the
* types.
*
* <p>Wrapper around Types.glb to add special handling for null types, primitives, and
* wildcards.
*
* @param processingEnv the {@link ProcessingEnvironment} to use
* @param tm1 a {@link TypeMirror}
* @param tm2 a {@link TypeMirror}
* @return the greatest lower bound of {@code tm1} and {@code tm2}.
*/
public static TypeMirror greatestLowerBound(
ProcessingEnvironment processingEnv, TypeMirror tm1, TypeMirror tm2) {
Type t1 = TypeAnnotationUtils.unannotatedType(tm1);
Type t2 = TypeAnnotationUtils.unannotatedType(tm2);
JavacProcessingEnvironment javacEnv = (JavacProcessingEnvironment) processingEnv;
Types types = Types.instance(javacEnv.getContext());
if (types.isSameType(t1, t2)) {
// Special case if the two types are equal.
return t1;
}
// Handle the 'null' type manually.
if (t1.getKind() == TypeKind.NULL) {
return t1;
}
if (t2.getKind() == TypeKind.NULL) {
return t2;
}
// Special case for primitives.
if (TypesUtils.isPrimitive(t1) || TypesUtils.isPrimitive(t2)) {
if (types.isAssignable(t1, t2)) {
return t1;
} else if (types.isAssignable(t2, t1)) {
return t2;
} else {
// Javac types.glb returns TypeKind.Error when the GLB does
// not exist, but we can't create one. Use TypeKind.NONE
// instead.
return processingEnv.getTypeUtils().getNoType(TypeKind.NONE);
}
}
if (t1.getKind() == TypeKind.WILDCARD) {
return t2;
}
if (t2.getKind() == TypeKind.WILDCARD) {
return t1;
}
// If neither type is a primitive type, null type, or wildcard
// and if the types are not the same, use javac types.glb
return types.glb(t1, t2);
}