本文整理了Java中javax.lang.model.util.Types.isSameType()
方法的一些代码示例,展示了Types.isSameType()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Types.isSameType()
方法的具体详情如下:
包路径:javax.lang.model.util.Types
类名称:Types
方法名:isSameType
[英]Tests whether two TypeMirror objects represent the same type.
Caveat: if either of the arguments to this method represents a wildcard, this method will return false. As a consequence, a wildcard is not the same type as itself. This might be surprising at first, but makes sense once you consider that an example like this must be rejected by the compiler:
List list = new ArrayList(); list.add(list.get(0));
Since annotations are only meta-data associated with a type, the set of annotations on either argument is not taken into account when computing whether or not two TypeMirror objects are the same type. In particular, two TypeMirror objects can have different annotations and still be considered the same.
[中]测试两个TypeMirror对象是否代表同一类型。
警告:如果此方法的任一参数表示通配符,则此方法将返回false。因此,通配符的类型与其本身不同。这可能首先令人惊讶,但一旦你认为这样的例子必须被编译器拒绝,才有意义:
List list = new ArrayList(); list.add(list.get(0));
由于注释只是与类型相关联的元数据,因此在计算两个TypeMirror对象是否为同一类型时,会不考虑两个参数上的注释集。特别是,两个TypeMirror对象可以有不同的注释,但仍然被认为是相同的。
代码示例来源:origin: robolectric/robolectric
@Override
protected boolean doEquivalent(TypeMirror a, TypeMirror b) {
return types.isSameType(a, b);
}
代码示例来源:origin: robolectric/robolectric
public static AnnotationMirror getAnnotationMirror(Types types, Element element,
TypeElement annotation) {
TypeMirror expectedType = annotation.asType();
for (AnnotationMirror m : element.getAnnotationMirrors()) {
if (types.isSameType(expectedType, m.getAnnotationType())) {
return m;
}
}
return null;
}
代码示例来源:origin: google/error-prone
private AnnotationMirror getAnnotationMirror(Element element, TypeMirror annotationType) {
for (AnnotationMirror mirror : element.getAnnotationMirrors()) {
if (processingEnv.getTypeUtils().isSameType(mirror.getAnnotationType(), annotationType)) {
return mirror;
}
}
return null;
}
代码示例来源:origin: robolectric/robolectric
public Helpers(ProcessingEnvironment environment) {
this.elements = environment.getElementUtils();
this.types = environment.getTypeUtils();
TypeMirror objectMirror = elements.getTypeElement(Object.class.getCanonicalName()).asType();
notObject = t -> !types.isSameType(t, objectMirror);
}
代码示例来源:origin: neo4j/neo4j
private boolean matches( TypeMirror fieldType, Set<String> typeNames )
{
return typeMirrors( typeNames ).anyMatch( t -> types.isSameType( t, fieldType ) );
}
代码示例来源:origin: airbnb/epoxy
static boolean isType(Elements elements, Types types, TypeMirror typeMirror, Class<?> clazz) {
TypeMirror classType = KotlinUtilsKt.getTypeMirror(clazz, elements);
return types.isSameType(typeMirror, classType);
}
代码示例来源:origin: androidannotations/androidannotations
private Map<String, TypeMirror> getActualTypes(Types typeUtils, DeclaredType baseClass, TypeMirror annotatedClass) {
List<TypeMirror> superTypes = new ArrayList<>();
superTypes.add(annotatedClass);
while (!superTypes.isEmpty()) {
TypeMirror x = superTypes.remove(0);
if (typeUtils.isSameType(typeUtils.erasure(x), typeUtils.erasure(baseClass))) {
DeclaredType type = (DeclaredType) x;
Map<String, TypeMirror> actualTypes = new HashMap<>();
for (int i = 0; i < type.getTypeArguments().size(); i++) {
TypeMirror actualArg = type.getTypeArguments().get(i);
TypeMirror formalArg = baseClass.getTypeArguments().get(i);
if (!typeUtils.isSameType(actualArg, formalArg)) {
actualTypes.put(formalArg.toString(), actualArg);
}
}
return actualTypes;
}
superTypes.addAll(typeUtils.directSupertypes(x));
}
return Collections.emptyMap();
}
代码示例来源:origin: neo4j/neo4j
@Override
public Boolean visitDeclared( DeclaredType map, Void ignored )
{
List<? extends TypeMirror> typeArguments = map.getTypeArguments();
if ( typeArguments.size() != 2 )
{
return Boolean.FALSE;
}
TypeMirror key = typeArguments.get( 0 );
return typeUtils.isSameType( key, typeMirrors.typeMirror( String.class ) ) &&
test( typeArguments.get( 1 ) );
}
}.visit( typeMirror );
代码示例来源:origin: immutables/immutables
public static ImmutableList<TypeMirror> getTypesFromMirrors(
Types types,
TypeMirror annotationType,
String annotationAttributeName,
List<? extends AnnotationMirror> annotationMirrors) {
ImmutableList.Builder<TypeMirror> builder = ImmutableList.builder();
for (AnnotationMirror annotationMirror : annotationMirrors) {
if (types.isSameType(annotationMirror.getAnnotationType(), annotationType)) {
collectTypesFromAnnotationAttribute(annotationAttributeName, builder, annotationMirror);
}
}
return builder.build();
}
代码示例来源:origin: neo4j/neo4j
private boolean isValidErasedType( TypeMirror actualType )
{
return whitelistedTypes.stream().anyMatch( type ->
{
TypeMirror erasedAllowedType = typeUtils.erasure( type );
TypeMirror map = typeUtils.erasure( typeMirrors.typeMirror( Map.class ) );
TypeMirror list = typeUtils.erasure( typeMirrors.typeMirror( List.class ) );
if ( typeUtils.isSameType( erasedAllowedType, map ) || typeUtils.isSameType( erasedAllowedType, list ) )
{
return typeUtils.isSubtype( actualType, erasedAllowedType );
}
return typeUtils.isSameType( actualType, erasedAllowedType );
} );
}
代码示例来源:origin: f2prateek/dart
private boolean containsTypeMirror(Collection<TypeElement> typeElements, TypeMirror query) {
// Ensure we are checking against a type-erased version for normalization purposes.
query = typeUtils.erasure(query);
for (TypeElement element : typeElements) {
final TypeMirror typeMirror = typeUtils.erasure(element.asType());
if (typeUtils.isSameType(typeMirror, query)) {
return true;
}
}
return false;
}
}
代码示例来源:origin: neo4j/neo4j
private Stream<CompilationMessage> validateReturnType( ExecutableElement method )
{
String streamClassName = Stream.class.getCanonicalName();
TypeMirror streamType = typeUtils.erasure( elementUtils.getTypeElement( streamClassName ).asType() );
TypeMirror returnType = method.getReturnType();
TypeMirror erasedReturnType = typeUtils.erasure( returnType );
TypeMirror voidType = typeUtils.getNoType( TypeKind.VOID );
if ( typeUtils.isSameType( returnType, voidType ) )
{
return Stream.empty();
}
if ( !typeUtils.isSubtype( erasedReturnType, streamType ) )
{
return Stream.of( new ReturnTypeError( method, "Return type of %s#%s must be %s",
method.getEnclosingElement().getSimpleName(), method.getSimpleName(), streamClassName ) );
}
return recordVisitor.visit( returnType );
}
代码示例来源:origin: neo4j/neo4j
private boolean isSameErasedType( Class<?> type, TypeMirror typeMirror )
{
return typeUtils
.isSameType( typeUtils.erasure( typeMirrors.typeMirror( type ) ), typeUtils.erasure( typeMirror ) );
}
代码示例来源:origin: org.jboss.logging/jboss-logging-processor
final TypeMirror type = args.get(i);
final VariableElement parameter = parameters.get(i);
if (types.isSameType(type, parameter.asType())) {
match = true;
} else {
代码示例来源:origin: org.jboss.logging/jboss-logging-processor
@Override
public final boolean isSameAs(final Class<?> type) {
return types.isSameType(types.erasure(this.typeMirror), toType(type));
}
代码示例来源:origin: immutables/immutables
/**
* Returns true if there's a public way to build the value type with an instance no-arg method.
*
* @param attribute value attribute to check.
* @param possibleBuildMethod method which matches {@link StyleMirror#attributeBuilder()}
* @return true if this is the possibleBuildMethod can build the value type.
*/
private static boolean isPossibleBuildMethod(ValueAttribute attribute, Element possibleBuildMethod) {
if (possibleBuildMethod.getKind() != ElementKind.METHOD) {
return false;
}
if (!attribute.containingType.names().possibleAttributeBuilder(possibleBuildMethod.getSimpleName())) {
return false;
}
Types typeUtils = attribute.containingType.constitution.protoclass()
.environment()
.processing()
.getTypeUtils();
ExecutableElement candidateBuildMethod = (ExecutableElement) possibleBuildMethod;
return !candidateBuildMethod.getModifiers().contains(Modifier.STATIC)
&& candidateBuildMethod.getModifiers().contains(Modifier.PUBLIC)
&& candidateBuildMethod.getTypeParameters().isEmpty()
&& candidateBuildMethod.getReturnType().getKind() == TypeKind.DECLARED
&& typeUtils.isSameType(candidateBuildMethod.getReturnType(), attribute.containedTypeElement.asType());
}
代码示例来源:origin: f2prateek/dart
final TypeMirror superType = element.getSuperclass();
if (!typeUtils.isSameType(
superType, elementUtils.getTypeElement("java.lang.Object").asType())) {
代码示例来源:origin: square/dagger
if (invalidTypeElement != null && types.isSameType(returnType,
types.erasure(invalidTypeElement.asType()))) {
error(String.format("@Provides method must not return %s directly: %s.%s",
if (!types.isSameType(moduleType.getSuperclass(), objectType)) {
error("Modules must not extend from other classes: " + elementToString(module), module);
代码示例来源:origin: immutables/immutables
&& typeUtils.isSameType(
candidateCopyMethod.getParameters().get(0).asType(),
valueAttribute.containedTypeElement.asType())) {
代码示例来源:origin: org.jboss.logging/jboss-logging-processor
final Properties expressionProperties, final boolean addGeneratedAnnotation) {
final Types types = processingEnv.getTypeUtils();
if (types.isSameType(interfaceElement.asType(), ElementHelper.toType(processingEnv.getElementUtils(), BasicLogger.class))) {
MessageInterface result = LOGGER_INTERFACE;
if (result == null) {
内容来源于网络,如有侵权,请联系作者删除!