Java Code Examples for javax.lang.model.type.TypeMirror#equals()

The following examples show how to use javax.lang.model.type.TypeMirror#equals() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeUtil.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("TypeEquals")
public boolean isObjcAssignable(TypeMirror t1, TypeMirror t2) {
  if (!isReferenceType(t1) || !isReferenceType(t2)) {
    if (t1 instanceof PointerType && t2 instanceof PointerType) {
      return isObjcAssignable(((PointerType) t1).getPointeeType(),
                              ((PointerType) t2).getPointeeType());
    }
    return t1.equals(t2);
  }
  outer: for (TypeElement t2Class : getObjcUpperBounds(t2)) {
    for (TypeElement t1Class : getObjcUpperBounds(t1)) {
      if (isObjcSubtype(t1Class, t2Class)) {
        continue outer;
      }
    }
    return false;
  }
  return true;
}
 
Example 2
Source File: PullUpTransformer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean deepSearchTypes(DeclaredType currentElement, TypeMirror orig, TypeMirror something, Map<TypeMirror, TypeParameterElement> mappings) {
    Types types = workingCopy.getTypes();
    List<? extends TypeMirror> directSupertypes = types.directSupertypes(currentElement);
    for (TypeMirror superType : directSupertypes) {
        DeclaredType type = (DeclaredType) superType;
        List<? extends TypeMirror> typeArguments = type.getTypeArguments();
        for (int i = 0; i < typeArguments.size(); i++) {
            TypeMirror typeArgument = typeArguments.get(i);
            if (something.equals(typeArgument)) {
                TypeElement asElement = (TypeElement) type.asElement();
                mappings.put(orig, asElement.getTypeParameters().get(i));
                if (types.erasure(targetType.asType()).equals(types.erasure(superType))) {
                    return true;
                }
                if(deepSearchTypes(type, orig, typeArgument, mappings)) {
                    break;
                }
            }
        }
        if (types.erasure(targetType.asType()).equals(types.erasure(superType))) {
            mappings.remove(orig);
            return true;
        }
    }
    return false;
}
 
Example 3
Source File: ClassWriterImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the class helper tree for the given class.
 *
 * @param type the class to print the helper for
 * @return a content tree for class helper
 */
private Content getTreeForClassHelper(TypeMirror type) {
    Content li = new HtmlTree(HtmlTag.LI);
    if (type.equals(typeElement.asType())) {
        Content typeParameters = getTypeParameterLinks(
                new LinkInfoImpl(configuration, LinkInfoImpl.Kind.TREE,
                typeElement));
        if (configuration.shouldExcludeQualifier(utils.containingPackage(typeElement).toString())) {
            li.addContent(utils.asTypeElement(type).getSimpleName());
            li.addContent(typeParameters);
        } else {
            li.addContent(utils.asTypeElement(type).getQualifiedName());
            li.addContent(typeParameters);
        }
    } else {
        Content link = getLink(new LinkInfoImpl(configuration,
                LinkInfoImpl.Kind.CLASS_TREE_PARENT, type)
                .label(configuration.getClassName(utils.asTypeElement(type))));
        li.addContent(link);
    }
    return li;
}
 
Example 4
Source File: ApNavigator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 5
Source File: TypeUtil.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("TypeEquals")
public boolean isSameType(TypeMirror t1, TypeMirror t2) {
  if (isGeneratedType(t1) || isGeneratedType(t2)) {
    return t1.equals(t2);
  }
  return javacTypes.isSameType(t1, t2);
}
 
Example 6
Source File: EntityType.java    From requery with Apache License 2.0 5 votes vote down vote up
private boolean isMethodProcessable(ExecutableElement element) {
    // if an immutable type with an implementation provided skip it
    if (!isUnimplementable() && element().getKind().isClass() && isImmutable() &&
        !element.getModifiers().contains(Modifier.ABSTRACT)) {
        if (!ImmutableAnnotationKind.of(element()).isPresent() ||
            !ImmutableAnnotationKind.of(element()).get().hasAnyMemberAnnotation(element)) {
            return false;
        }
    }
    String name = element.getSimpleName().toString();
    // skip kotlin data class methods with component1, component2.. names
    if (isUnimplementable() &&
        name.startsWith("component") && name.length() > "component".length()) {
        return false;
    }

    TypeMirror type = element.getReturnType();
    boolean isInterface = element().getKind().isInterface();
    boolean isTransient = Mirrors.findAnnotationMirror(element, Transient.class).isPresent();

    // must be a getter style method with no args, can't return void or itself or its builder
    return type.getKind() != TypeKind.VOID &&
           element.getParameters().isEmpty() &&
           (isImmutable() || isInterface || !element.getModifiers().contains(Modifier.FINAL)) &&
           (!isImmutable() || !type.equals(element().asType())) &&
           !type.equals(builderType().orElse(null)) &&
           !element.getModifiers().contains(Modifier.STATIC) &&
           !element.getModifiers().contains(Modifier.DEFAULT) &&
           (!isTransient || isInterface) &&
           !name.equals("toString") && !name.equals("hashCode");
}
 
Example 7
Source File: MoreTypes.java    From doma with Apache License 2.0 5 votes vote down vote up
public boolean isAssignableWithErasure(TypeMirror lhs, TypeMirror rhs) {
  assertNotNull(lhs, rhs);
  if (lhs.getKind() == TypeKind.NONE || rhs.getKind() == TypeKind.NONE) {
    return false;
  }
  if (lhs.getKind() == TypeKind.NULL) {
    return rhs.getKind() == TypeKind.NULL;
  }
  if (rhs.getKind() == TypeKind.NULL) {
    return lhs.getKind() == TypeKind.NULL;
  }
  if (lhs.getKind() == TypeKind.VOID) {
    return rhs.getKind() == TypeKind.VOID;
  }
  if (rhs.getKind() == TypeKind.VOID) {
    return lhs.getKind() == TypeKind.VOID;
  }
  TypeMirror t1 = typeUtils.erasure(lhs);
  TypeMirror t2 = typeUtils.erasure(rhs);
  if (typeUtils.isSameType(t1, t2) || t1.equals(t2)) {
    return true;
  }
  for (TypeMirror supertype : typeUtils.directSupertypes(t1)) {
    if (isAssignableWithErasure(supertype, t2)) {
      return true;
    }
  }
  return false;
}
 
Example 8
Source File: ApNavigator.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 9
Source File: ApNavigator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 10
Source File: ApNavigator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 11
Source File: ApNavigator.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 12
Source File: ApNavigator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 13
Source File: Declarations.java    From doma with Apache License 2.0 5 votes vote down vote up
private TypeMirror resolveTypeParameter(
    TypeMirror formalType, List<TypeParameterDeclaration> typeParameterDeclarations) {
  for (TypeParameterDeclaration typeParameterDecl : typeParameterDeclarations) {
    if (formalType.equals(typeParameterDecl.getFormalType())) {
      return typeParameterDecl.getActualType();
    }
    DeclaredType declaredType = ctx.getMoreTypes().toDeclaredType(formalType);
    if (declaredType == null) {
      continue;
    }
    if (declaredType.getTypeArguments().isEmpty()) {
      continue;
    }
    List<Optional<TypeMirror>> optTypeArgs =
        declaredType.getTypeArguments().stream()
            .map(
                arg ->
                    typeParameterDeclarations.stream()
                        .filter(declaration -> arg.equals(declaration.getFormalType()))
                        .map(TypeParameterDeclaration::getActualType)
                        .findFirst())
            .collect(Collectors.toList());
    if (optTypeArgs.stream().allMatch(Optional::isPresent)) {
      TypeMirror[] typeArgs = optTypeArgs.stream().map(Optional::get).toArray(TypeMirror[]::new);
      TypeElement typeElement = ctx.getMoreElements().toTypeElement(declaredType.asElement());
      if (typeElement == null) {
        continue;
      }
      return ctx.getMoreTypes().getDeclaredType(typeElement, typeArgs);
    }
  }
  return formalType;
}
 
Example 14
Source File: PatternAnalyser.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void resolveFields(Parameters p) {
    
    // Analyze fields
    List<VariableElement> fields = ElementFilter.fieldsIn(p.element.getEnclosedElements());
    
    String propertyStyle = "this."; //NOI18N
    
    for ( VariableElement field : fields ) {
        
        if ( field.getModifiers().contains(STATIC)) {
            continue;
        }
        
        //System.out.println("Property style " + propertyStyle);   
        String fieldName = nameAsString(field);
        //System.out.println("Field name1 " + fieldName);
        if( fieldName.startsWith(propertyStyle) ){
            fieldName = fieldName.substring(1);
            //System.out.println("Field name2 " + fieldName);
        }
        
        Property pp = p.propertyPatterns.get( fieldName );
        if ( pp == null )
            pp = p.idxPropertyPatterns.get( fieldName );
        if ( pp == null )
            continue;
        TypeMirror ppType = pp.type;
        if ( ppType != null && ppType.equals( field.asType() ) )
            pp.estimatedField = field;
    }
}
 
Example 15
Source File: ToolTipAnnotation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Test if typeElement is a parent of currentElement.
 */
private static boolean testParentOf(Types types, TypeMirror currentElement, TypeMirror typeMirror) {
    List<? extends TypeMirror> directSupertypes = types.directSupertypes(currentElement);
    for (TypeMirror superType : directSupertypes) {
        if (superType.equals(typeMirror)) {
            return true;
        } else {
            boolean isParent = testParentOf(types, superType, typeMirror);
            if (isParent) {
                return true;
            }
        }
    }
    return false;
}
 
Example 16
Source File: ApNavigator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 17
Source File: ApNavigator.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public boolean isArrayButNotByteArray(TypeMirror t) {
    if(!isArray(t))
        return false;

    ArrayType at = (ArrayType) t;
    TypeMirror ct = at.getComponentType();

    return !ct.equals(primitiveByte);
}
 
Example 18
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public boolean ignoreBounds(TypeMirror bound) {
    return bound.equals(getObjectType()) && !isAnnotated(bound);
}
 
Example 19
Source File: GeneratedPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
protected static void constantArgument(ProcessingEnvironment env, PrintWriter out, InjectedDependencies deps, int argIdx, TypeMirror type, int nodeIdx) {
    if (hasRawtypeWarning(type)) {
        out.printf("            @SuppressWarnings({\"rawtypes\"})\n");
    }
    out.printf("            %s arg%d;\n", getErasedType(type), argIdx);
    out.printf("            if (args[%d].isConstant()) {\n", nodeIdx);
    if (type.equals(resolvedJavaTypeType(env))) {
        out.printf("                arg%d = %s.asJavaType(args[%d].asConstant());\n", argIdx, deps.use(WellKnownDependency.CONSTANT_REFLECTION), nodeIdx);
    } else {
        switch (type.getKind()) {
            case BOOLEAN:
                out.printf("                arg%d = args[%d].asJavaConstant().asInt() != 0;\n", argIdx, nodeIdx);
                break;
            case BYTE:
                out.printf("                arg%d = (byte) args[%d].asJavaConstant().asInt();\n", argIdx, nodeIdx);
                break;
            case CHAR:
                out.printf("                arg%d = (char) args[%d].asJavaConstant().asInt();\n", argIdx, nodeIdx);
                break;
            case SHORT:
                out.printf("                arg%d = (short) args[%d].asJavaConstant().asInt();\n", argIdx, nodeIdx);
                break;
            case INT:
                out.printf("                arg%d = args[%d].asJavaConstant().asInt();\n", argIdx, nodeIdx);
                break;
            case LONG:
                out.printf("                arg%d = args[%d].asJavaConstant().asLong();\n", argIdx, nodeIdx);
                break;
            case FLOAT:
                out.printf("                arg%d = args[%d].asJavaConstant().asFloat();\n", argIdx, nodeIdx);
                break;
            case DOUBLE:
                out.printf("                arg%d = args[%d].asJavaConstant().asDouble();\n", argIdx, nodeIdx);
                break;
            case DECLARED:
                out.printf("                arg%d = %s.asObject(%s.class, args[%d].asJavaConstant());\n", argIdx, deps.use(WellKnownDependency.SNIPPET_REFLECTION), getErasedType(type), nodeIdx);
                break;
            default:
                throw new IllegalArgumentException();
        }
    }
    out.printf("            } else {\n");
    out.printf("                return false;\n");
    out.printf("            }\n");
}
 
Example 20
Source File: SPGPreferenceParser.java    From SharedPreferencesGenerator with Apache License 2.0 4 votes vote down vote up
private SerializerHolder parseSerializerTypeMirror(Element element, TypeMirror typeMirror) {

        final String s = typeMirror.toString();
        if (VOID.equals(s)) {
            return null;
        }

        final TypeMirror spgInterface = extractSPGSerializerInterface(typeMirror);

        if (spgInterface == null) {
            log(Diagnostic.Kind.ERROR, "Serializer `%s` does not implement SPGSerializer", typeMirror);
            return null;
        }

        final Matcher matcher = SERIALIZER_TYPES.matcher(spgInterface.toString());
        if (!matcher.matches()) {
            log(Diagnostic.Kind.ERROR, "Internal error, could not parse types of: %s", spgInterface);
            return null;
        }

        final String type = matcher.group(1);
        final String repr = matcher.group(2);

        // check against declared type
        // check against representation type
        final TypeElement reprTypeElement = mElements.getTypeElement(repr);
        if (reprTypeElement == null) {
            // could not obtain representation Type. Assume that it's a generic value,
            // we cannot process this
            log(Diagnostic.Kind.ERROR, "Serializer `%s` contains representation type parameter " +
                    "as a generic. Type: %s, interface: %s", typeMirror, spgInterface);
            return null;
        }

        final TypeMirror reprMirror = reprTypeElement.asType();
        final ru.noties.spg.processor.data.KeyType reprKeyType = ru.noties.spg.processor.data.KeyType.parse(reprMirror);
        if (reprKeyType == null) {
            log(Diagnostic.Kind.ERROR, "Representation type for StormSerializer is not supported: %s", reprMirror);
            return null;
        }

        final TypeElement serializerType = mElements.getTypeElement(type);
        if (serializerType == null) {
            // well, assume that it's a generic
            // we cannot check now for it's value
        } else {
            final TypeMirror serializerTypeMirror = serializerType.asType();
            if (!serializerTypeMirror.equals(element.asType())) {
                log(Diagnostic.Kind.ERROR, "Type for StormSerializer is wrong: %s, should be: %s", serializerTypeMirror, element.asType());
                return null;
            }
        }

        return new SerializerHolder(s, reprKeyType);
    }