本文整理汇总了Java中com.sun.tools.javac.code.Types.isSameType方法的典型用法代码示例。如果您正苦于以下问题:Java Types.isSameType方法的具体用法?Java Types.isSameType怎么用?Java Types.isSameType使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.javac.code.Types
的用法示例。
在下文中一共展示了Types.isSameType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: matches
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
@Override
public boolean matches(BinaryTree tree, VisitorState state) {
Type leftType = ((JCTree) tree.getLeftOperand()).type;
Types types = state.getTypes();
Symtab symtab = state.getSymtab();
if (!(types.isSameType(leftType, symtab.intType))
&& !(types.isSameType(leftType, symtab.byteType))
&& !(types.isSameType(leftType, symtab.shortType))
&& !(types.isSameType(leftType, symtab.charType))) {
return false;
}
ExpressionTree rightOperand = tree.getRightOperand();
if (rightOperand instanceof LiteralTree) {
Object rightValue = ((LiteralTree) rightOperand).getValue();
if (rightValue instanceof Number) {
int intValue = ((Number) rightValue).intValue();
return intValue < 0 || intValue > 31;
}
}
return false;
}
示例2: partitionParametersByType
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
private Multimap<Type, VariableTree> partitionParametersByType(
List<VariableTree> parameters, VisitorState state) {
Types types = state.getTypes();
Multimap<Type, VariableTree> multimap = LinkedListMultimap.create();
variables:
for (VariableTree node : parameters) {
// Normalize Integer => int
Type type = types.unboxedTypeOrType(ASTHelpers.getType(node));
for (Type existingType : multimap.keySet()) {
if (types.isSameType(existingType, type)) {
multimap.put(existingType, node);
continue variables;
}
}
// A new type for the map.
multimap.put(type, node);
}
return multimap;
}
示例3: matchMethodInvocation
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!MATCHER.matches(tree, state)) {
return NO_MATCH;
}
List<? extends ExpressionTree> arguments = tree.getArguments();
Symtab syms = state.getSymtab();
Types types = state.getTypes();
if (types.isSameType(types.unboxedTypeOrType(getType(arguments.get(1))), syms.intType)
&& types.isSameType(types.unboxedTypeOrType(getType(arguments.get(0))), syms.charType)) {
return describeMatch(
tree,
SuggestedFix.builder()
.replace(arguments.get(0), state.getSourceForNode(arguments.get(1)))
.replace(arguments.get(1), state.getSourceForNode(arguments.get(0)))
.build());
}
return NO_MATCH;
}
示例4: matchMethodInvocation
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!MATCHER.matches(tree, state)) {
return NO_MATCH;
}
List<? extends ExpressionTree> arguments = tree.getArguments();
Symtab syms = state.getSymtab();
Types types = state.getTypes();
if (types.isSameType(types.unboxedTypeOrType(getType(arguments.get(0))), syms.intType)
&& types.isSameType(types.unboxedTypeOrType(getType(arguments.get(1))), syms.charType)) {
return describeMatch(
tree,
SuggestedFix.builder()
.replace(arguments.get(0), state.getSourceForNode(arguments.get(1)))
.replace(arguments.get(1), state.getSourceForNode(arguments.get(0)))
.build());
}
return NO_MATCH;
}
示例5: matches
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
@Override
public boolean matches(BinaryTree tree, VisitorState state) {
Type leftType = ((JCTree) tree.getLeftOperand()).type;
Types types = state.getTypes();
Symtab symtab = state.getSymtab();
if (!(types.isSameType(leftType, symtab.intType)) &&
!(types.isSameType(leftType, symtab.byteType)) &&
!(types.isSameType(leftType, symtab.shortType)) &&
!(types.isSameType(leftType, symtab.charType))) {
return false;
}
ExpressionTree rightOperand = tree.getRightOperand();
if (rightOperand instanceof LiteralTree) {
Object rightValue = ((LiteralTree) rightOperand).getValue();
if (rightValue instanceof Number) {
int intValue = ((Number) rightValue).intValue();
return intValue < 0 || intValue > 31;
}
}
return false;
}
示例6: isSameType
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
/** Returns true if {@code erasure(s) == erasure(t)}. */
public static boolean isSameType(Type s, Type t, VisitorState state) {
if (s == null || t == null) {
return false;
}
Types types = state.getTypes();
return types.isSameType(types.erasure(s), types.erasure(t));
}
示例7: removeMessageArgumentIfPresent
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
/** Removes the message argument if it is present. */
private void removeMessageArgumentIfPresent(VisitorState state, List<Type> argumentTypes) {
if (argumentTypes.size() == 2) {
return;
}
Types types = state.getTypes();
Type firstType = argumentTypes.get(0);
if (types.isSameType(firstType, state.getSymtab().stringType)) {
argumentTypes.remove(0);
}
}
示例8: isKnownCheckedException
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
boolean isKnownCheckedException(VisitorState state, Type type) {
Types types = state.getTypes();
Symtab symtab = state.getSymtab();
// Check erasure for generics.
type = types.erasure(type);
return
// Has to be some Exception: A variable of type Throwable might be an Error.
types.isSubtype(type, symtab.exceptionType)
// Has to be some subtype: A variable of type Exception might be a RuntimeException.
&& !types.isSameType(type, symtab.exceptionType)
// Can't be of type RuntimeException.
&& !types.isSubtype(type, symtab.runtimeExceptionType);
}
示例9: doubleAndFloatStatus
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
private DoubleAndFloatStatus doubleAndFloatStatus(
VisitorState state, Type recieverType, Type argType) {
Types types = state.getTypes();
if (!types.isSameType(recieverType, state.getSymtab().floatType)) {
return DoubleAndFloatStatus.NONE;
}
if (types.isSameType(argType, types.boxedClass(state.getSymtab().doubleType).type)) {
return DoubleAndFloatStatus.BOXED_DOUBLE_INTO_FLOAT;
}
if (types.isSameType(argType, state.getSymtab().doubleType)) {
return DoubleAndFloatStatus.PRIMITIVE_DOUBLE_INTO_FLOAT;
}
return DoubleAndFloatStatus.NONE;
}
示例10: matches
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
@Override
public boolean matches(MethodInvocationTree t, VisitorState state) {
Symbol symbol = ASTHelpers.getSymbol(t);
if (!(symbol instanceof MethodSymbol)) {
return false;
}
MethodSymbol methodSymbol = (MethodSymbol) symbol;
// Bail out quickly if the method is not varargs
if (!methodSymbol.isVarArgs()) {
return false;
}
// Last param must be varags
List<VarSymbol> params = methodSymbol.getParameters();
int varargsPosition = params.length() - 1;
ArrayType varargsParamType = (ArrayType) params.last().type;
// Is the argument at the varargsPosition the only varargs argument and a primitive array?
JCMethodInvocation methodInvocation = (JCMethodInvocation) t;
List<JCExpression> arguments = methodInvocation.getArguments();
Types types = state.getTypes();
if (arguments.size() != params.length()) {
return false;
}
Type varargsArgumentType = arguments.get(varargsPosition).type;
if (!types.isArray(varargsArgumentType)
|| !types.elemtype(varargsArgumentType).isPrimitive()) {
return false;
}
// Do the param and argument types actually match? i.e. can boxing even happen?
return !(types.isSameType(varargsParamType, varargsArgumentType)
|| types.isSameType(varargsParamType.getComponentType(), varargsArgumentType));
}
示例11: matches
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
@Override
public boolean matches(T tree, VisitorState state) {
Types types = state.getTypes();
Type typeToCompare = typeToCompareSupplier.get(state);
return (typeToCompare != null &&
types.isSameType(((JCTree) tree).type, typeToCompare));
}
示例12: matches
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
@Override
public boolean matches(MethodInvocationTree t, VisitorState state) {
Symbol symbol = ASTHelpers.getSymbol(t);
if (symbol == null || !(symbol instanceof MethodSymbol)) {
return false;
}
MethodSymbol methodSymbol = (MethodSymbol) symbol;
// Bail out quickly if the method is not varargs
if (!methodSymbol.isVarArgs()) {
return false;
}
// Last param must be varags
List<VarSymbol> params = methodSymbol.getParameters();
int varargsPosition = params.length() - 1;
ArrayType varargsParamType = (ArrayType) params.last().type;
// Is the argument at the varargsPosition the only varargs argument and a primitive array?
JCMethodInvocation methodInvocation = (JCMethodInvocation) t;
List<JCExpression> arguments = methodInvocation.getArguments();
Types types = state.getTypes();
if (arguments.size() != params.length()) {
return false;
}
Type varargsArgumentType = arguments.get(varargsPosition).type;
if (!types.isArray(varargsArgumentType)
|| !types.elemtype(varargsArgumentType).isPrimitive()) {
return false;
}
// Do the param and argument types actually match? i.e. can boxing even happen?
if (types.isSameType(varargsParamType, varargsArgumentType)
|| types.isSameType(varargsParamType.getComponentType(), varargsArgumentType)) {
return false;
}
return true;
}
示例13: 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);
}
示例14: findExtMethod
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
private Symbol.MethodSymbol findExtMethod( JCTree.JCMethodInvocation tree )
{
JCExpression methodSelect = tree.getMethodSelect();
if( methodSelect instanceof MemberSelectTree )
{
JCTree.JCFieldAccess meth = (JCTree.JCFieldAccess)tree.meth;
if( meth.sym == null || !meth.sym.hasAnnotations() )
{
return null;
}
for( Attribute.Compound annotation : meth.sym.getAnnotationMirrors() )
{
if( annotation.type.toString().equals( ExtensionMethod.class.getName() ) )
{
String extensionClass = (String)annotation.values.get( 0 ).snd.getValue();
boolean isStatic = (boolean)annotation.values.get( 1 ).snd.getValue();
BasicJavacTask javacTask = (BasicJavacTask)_tp.getJavacTask(); //JavacHook.instance() != null ? (JavacTaskImpl)JavacHook.instance().getJavacTask() : ClassSymbols.instance( _sp.getTypeLoader().getModule() ).getJavacTask();
Symbol.ClassSymbol extClassSym = ClassSymbols.instance( _sp.getTypeLoader().getModule() ).getClassSymbol( javacTask, extensionClass ).getFirst();
if( extClassSym == null )
{
// This can happen during bootstrapping with Dark Java classes from Manifold itself
// e.g., ManResolve is a darkj class Manifold uses, ManResolve uses String, which may have an extension class which needs ManResole...
// So we short-circuit that here (ManResolve or any other darkj class used during bootstrapping doesn't really need to use extensions)
return null;
}
Types types = Types.instance( javacTask.getContext() );
outer:
for( Symbol elem : IDynamicJdk.instance().getMembers( extClassSym ) )
{
if( elem instanceof Symbol.MethodSymbol && elem.flatName().toString().equals( meth.sym.name.toString() ) )
{
Symbol.MethodSymbol extMethodSym = (Symbol.MethodSymbol)elem;
List<Symbol.VarSymbol> extParams = extMethodSym.getParameters();
List<Symbol.VarSymbol> calledParams = ((Symbol.MethodSymbol)meth.sym).getParameters();
int thisOffset = isStatic ? 0 : 1;
if( extParams.size() - thisOffset != calledParams.size() )
{
continue;
}
for( int i = thisOffset; i < extParams.size(); i++ )
{
Symbol.VarSymbol extParam = extParams.get( i );
Symbol.VarSymbol calledParam = calledParams.get( i - thisOffset );
if( !types.isSameType( types.erasure( extParam.type ), types.erasure( calledParam.type ) ) )
{
continue outer;
}
}
return extMethodSym;
}
}
}
}
}
return null;
}
示例15: compatibilityOfTypes
import com.sun.tools.javac.code.Types; //导入方法依赖的package包/类
public static TypeCompatibilityReport compatibilityOfTypes(
Type receiverType,
Type argumentType,
Set<Type> previousReceiverTypes,
Set<Type> previousArgumentTypes,
VisitorState state) {
if (receiverType == null || argumentType == null) {
return TypeCompatibilityReport.createCompatibleReport();
}
// If one type can be cast into the other, we don't flag the equality test.
// Note: we do this precisely in this order to allow primitive values to be checked pre-1.7:
// 1.6: java.lang.Object can't be cast to primitives
// 1.7: java.lang.Object can be cast to primitives (implicitly through the boxed primitive type)
if (ASTHelpers.isCastable(argumentType, receiverType, state)) {
return leastUpperBoundGenericMismatch(
receiverType, argumentType, previousReceiverTypes, previousArgumentTypes, state);
}
// Otherwise, we explore the superclasses of the receiver type as well as the interfaces it
// implements and we collect all overrides of java.lang.Object.equals(). If one of those
// overrides is inherited by the argument, then we don't flag the equality test.
Types types = state.getTypes();
Predicate<MethodSymbol> equalsPredicate =
methodSymbol ->
!methodSymbol.isStatic()
&& ((methodSymbol.flags() & Flags.SYNTHETIC) == 0)
&& types.isSameType(methodSymbol.getReturnType(), state.getSymtab().booleanType)
&& methodSymbol.getParameters().size() == 1
&& types.isSameType(
methodSymbol.getParameters().get(0).type, state.getSymtab().objectType);
Set<MethodSymbol> overridesOfEquals =
ASTHelpers.findMatchingMethods(
state.getName("equals"), equalsPredicate, receiverType, types);
Symbol argumentClass = ASTHelpers.getUpperBound(argumentType, state.getTypes()).tsym;
for (MethodSymbol method : overridesOfEquals) {
ClassSymbol methodClass = method.enclClass();
if (argumentClass.isSubClass(methodClass, types)
&& !methodClass.equals(state.getSymtab().objectType.tsym)
&& !methodClass.equals(state.getSymtab().enumSym)) {
// The type of the argument shares a superclass
// (other then java.lang.Object or java.lang.Enum) or interface
// with the receiver that implements an override of java.lang.Object.equals().
// These should be compatible, but check any generic types for their compatbilities.
return leastUpperBoundGenericMismatch(
receiverType, argumentType, previousReceiverTypes, previousArgumentTypes, state);
}
}
return TypeCompatibilityReport.incompatible(receiverType, argumentType);
}