Java Code Examples for javax.lang.model.type.TypeKind#WILDCARD

The following examples show how to use javax.lang.model.type.TypeKind#WILDCARD . 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: StateProcessor.java    From android-state with Eclipse Public License 1.0 6 votes vote down vote up
private CompatibilityType getCompatibilityType(Element field) {
    TypeMirror typeMirror = field.asType();
    for (CompatibilityType compatibilityType : CompatibilityType.values()) {
        if (compatibilityType == CompatibilityType.PARCELABLE_ARRAY) {
            TypeMirror arrayType = getArrayType(field);
            if (arrayType != null && isAssignable(arrayType, PARCELABLE_CLASS_NAME)) {
                return CompatibilityType.PARCELABLE_ARRAY;
            }

        } else if (compatibilityType.mGenericClass == null) {
            if (isAssignable(typeMirror, compatibilityType.mClass)) {
                return compatibilityType;
            }

        } else if (typeMirror.getKind() != TypeKind.WILDCARD) {
            if (isAssignable(mTypeUtils.erasure(typeMirror), compatibilityType.mClass)) {
                List<? extends TypeMirror> typeArguments = ((DeclaredType) typeMirror).getTypeArguments();
                if (typeArguments != null && typeArguments.size() >= 1 && isAssignable(typeArguments.get(0), compatibilityType.mGenericClass)) {
                    return compatibilityType;
                }
            }
        }
    }
    return null;
}
 
Example 2
Source File: TypeUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static TypeMirror resolveCapturedType(CompilationInfo info, TypeMirror tm) {
    if (tm == null) {
        return tm;
    }
    if (tm.getKind() == TypeKind.ERROR) {
        tm = info.getTrees().getOriginalType((ErrorType) tm);
    }
    TypeMirror type = resolveCapturedTypeInt(info, tm);
    if (type == null) {
        return tm;
    }
    if (type.getKind() == TypeKind.WILDCARD) {
        TypeMirror tmirr = ((WildcardType) type).getExtendsBound();
        if (tmirr != null)
            return tmirr;
        else { //no extends, just '?'
            TypeElement te = info.getElements().getTypeElement("java.lang.Object"); // NOI18N
            return te == null ? null : te.asType();
        }
            
    }
    
    return type;
}
 
Example 3
Source File: TypeUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public StringBuilder visitWildcard(WildcardType t, Boolean p) {
    int len = DEFAULT_VALUE.length();
    DEFAULT_VALUE.append("?"); //NOI18N
    TypeMirror bound = t.getSuperBound();
    if (bound == null) {
        bound = t.getExtendsBound();
        if (bound != null) {
            DEFAULT_VALUE.append(" extends "); //NOI18N
            if (bound.getKind() == TypeKind.WILDCARD)
                bound = ((WildcardType)bound).getSuperBound();
            visit(bound, p);
        } else if (len == 0) {
            bound = SourceUtils.getBound(t);
            if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N
                DEFAULT_VALUE.append(" extends "); //NOI18N
                visit(bound, p);
            }
        }
    } else {
        DEFAULT_VALUE.append(" super "); //NOI18N
        visit(bound, p);
    }
    return DEFAULT_VALUE;
}
 
Example 4
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public StringBuilder visitWildcard(WildcardType t, Boolean p) {
    int len = DEFAULT_VALUE.length();
    DEFAULT_VALUE.append("?"); //NOI18N
    TypeMirror bound = t.getSuperBound();
    if (bound == null) {
        bound = t.getExtendsBound();
        if (bound != null) {
            DEFAULT_VALUE.append(" extends "); //NOI18N
            if (bound.getKind() == TypeKind.WILDCARD)
                bound = ((WildcardType)bound).getSuperBound();
            visit(bound, p);
        } else if (len == 0) {
            bound = SourceUtils.getBound(t);
            if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N
                DEFAULT_VALUE.append(" extends "); //NOI18N
                visit(bound, p);
            }
        }
    } else {
        DEFAULT_VALUE.append(" super "); //NOI18N
        visit(bound, p);
    }
    return DEFAULT_VALUE;
}
 
Example 5
Source File: MalformedFormatString.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Unbox a wrapper type into a TypeKind. Some additional types are mapped to TypeKinds which cannot really appear in 
 * expressions (at least I hope)
 * 
 * @param tm
 * @return 
 */
private static TypeKind unboxBoxed(TypeMirror tm) {
    TypeElement te = (TypeElement)((DeclaredType)tm).asElement();
    String qn = te.getQualifiedName().toString();
    if (!qn.startsWith("java.lang.")) { // NO18N
        if (qn.equals("java.math.BigInteger")) { // NOI18N
            return TypeKind.WILDCARD;
        }
        return null;
    }
    switch (qn.substring(10)) {
        case "Short": return TypeKind.SHORT; // NOI18N
        case "Long": return TypeKind.LONG; // NOI18N
        case "Byte": return TypeKind.BYTE; // NOI18N
        case "Integer": return TypeKind.INT; // NOI18N
        case "Double": return TypeKind.DOUBLE; // NOI18N
        case "Float": return TypeKind.FLOAT; // NOI18N
        case "Character": return TypeKind.CHAR; // NOI18N
        case "String": return TypeKind.OTHER; // NOI18N
        case "Object": return TypeKind.PACKAGE; // NOI18N
    }
    return null;
}
 
Example 6
Source File: AutoImport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Void visitWildcard(WildcardType type, Void p) {
    builder.append("?"); //NOI18N
    TypeMirror bound = type.getSuperBound();
    if (bound == null) {
        bound = type.getExtendsBound();
        if (bound != null) {
            builder.append(" extends "); //NOI18N
            if (bound.getKind() == TypeKind.WILDCARD)
                bound = ((WildcardType)bound).getSuperBound();
            visit(bound);
        }
    } else {
        builder.append(" super "); //NOI18N
        visit(bound);
    }
    return null;
}
 
Example 7
Source File: CaseClassItem.java    From soabase-halva with Apache License 2.0 5 votes vote down vote up
CaseClassItem(String name, ExecutableElement element, TypeMirror type, boolean hasDefaultValue, boolean mutable)
{
    this.name = name;
    this.element = element;
    this.type = type;
    this.hasDefaultValue = hasDefaultValue;
    this.mutable = mutable;

    if ( type.getKind() == TypeKind.WILDCARD )
    {
        System.out.println();
    }
}
 
Example 8
Source File: TypeSimplifier.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
@Override public boolean apply(TypeMirror arg) {
  if (arg.getKind() == TypeKind.WILDCARD) {
    WildcardType wildcard = (WildcardType) arg;
    if (wildcard.getExtendsBound() == null || isJavaLangObject(wildcard.getExtendsBound())) {
      // This is <?>, unless there's a super bound, in which case it is <? super Foo> and
      // is erased.
      return (wildcard.getSuperBound() != null);
    }
  }
  return true;
}
 
Example 9
Source File: TypesImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean isSameType(TypeMirror t1, TypeMirror t2) {
	if (t1.getKind() == TypeKind.WILDCARD || t2.getKind() == TypeKind.WILDCARD) {
        // Wildcard types are never equal, according to the spec of this method
		return false;
	}
    if (t1 == t2) {
        return true;
    }
    if (!(t1 instanceof TypeMirrorImpl) || !(t2 instanceof TypeMirrorImpl)) {
        return false;
    }
    Binding b1 = ((TypeMirrorImpl)t1).binding();
    Binding b2 = ((TypeMirrorImpl)t2).binding();

    if (b1 == b2) {
        return true;
    }
    if (!(b1 instanceof TypeBinding) || !(b2 instanceof TypeBinding)) {
        return false;
    }
    TypeBinding type1 = ((TypeBinding) b1);
    TypeBinding type2 = ((TypeBinding) b2);
    if (TypeBinding.equalsEquals(type1,  type2))
    	return true;
    return CharOperation.equals(type1.computeUniqueKey(), type2.computeUniqueKey());
}
 
Example 10
Source File: ModelUtils.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the upper bound of {@code type}.<ul>
 * <li>T -> T
 * <li>? -> Object
 * <li>? extends T -> T
 * <li>? super T -> Object
 * </ul>
 */
public static TypeMirror upperBound(Elements elements, TypeMirror type) {
  if (type.getKind() == TypeKind.WILDCARD) {
    WildcardType wildcard = (WildcardType) type;
    type = wildcard.getExtendsBound();
    if (type == null) {
      type = elements.getTypeElement(Object.class.getName()).asType();
    }
  }
  return type;
}
 
Example 11
Source File: TypeSimplifier.java    From SimpleWeibo with Apache License 2.0 5 votes vote down vote up
@Override public boolean apply(TypeMirror arg) {
  if (arg.getKind() == TypeKind.WILDCARD) {
    WildcardType wildcard = (WildcardType) arg;
    if (wildcard.getExtendsBound() == null || isJavaLangObject(wildcard.getExtendsBound())) {
      // This is <?>, unless there's a super bound, in which case it is <? super Foo> and
      // is erased.
      return (wildcard.getSuperBound() != null);
    }
  }
  return true;
}
 
Example 12
Source File: AutoCodecProcessor.java    From bazel with Apache License 2.0 5 votes vote down vote up
private Relation findRelationWithGenerics(TypeMirror type1, TypeMirror type2) {
  if (type1.getKind() == TypeKind.TYPEVAR
      || type1.getKind() == TypeKind.WILDCARD
      || type2.getKind() == TypeKind.TYPEVAR
      || type2.getKind() == TypeKind.WILDCARD) {
    return Relation.EQUAL_TO;
  }
  if (env.getTypeUtils().isAssignable(type1, type2)) {
    if (env.getTypeUtils().isAssignable(type2, type1)) {
      return Relation.EQUAL_TO;
    }
    return Relation.INSTANCE_OF;
  }
  if (env.getTypeUtils().isAssignable(type2, type1)) {
    return Relation.SUPERTYPE_OF;
  }
  // From here on out, we can't detect subtype/supertype, we're only checking for equality.
  TypeMirror erasedType1 = env.getTypeUtils().erasure(type1);
  TypeMirror erasedType2 = env.getTypeUtils().erasure(type2);
  if (!env.getTypeUtils().isSameType(erasedType1, erasedType2)) {
    // Technically, there could be a relationship, but it's too hard to figure out for now.
    return Relation.UNRELATED_TO;
  }
  List<? extends TypeMirror> genericTypes1 = ((DeclaredType) type1).getTypeArguments();
  List<? extends TypeMirror> genericTypes2 = ((DeclaredType) type2).getTypeArguments();
  if (genericTypes1.size() != genericTypes2.size()) {
    return null;
  }
  for (int i = 0; i < genericTypes1.size(); i++) {
    Relation result = findRelationWithGenerics(genericTypes1.get(i), genericTypes2.get(i));
    if (result != Relation.EQUAL_TO) {
      return Relation.UNRELATED_TO;
    }
  }
  return Relation.EQUAL_TO;
}
 
Example 13
Source File: StandaloneWildcardType.java    From buck with Apache License 2.0 4 votes vote down vote up
public StandaloneWildcardType(
    @Nullable TypeMirror extendsBound, @Nullable TypeMirror superBound) {
  super(TypeKind.WILDCARD);
  this.extendsBound = extendsBound;
  this.superBound = superBound;
}
 
Example 14
Source File: Type.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public TypeKind getKind() {
    return TypeKind.WILDCARD;
}
 
Example 15
Source File: AbstractAssignabilityChecker.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected boolean checkParameter( TypeMirror argType, TypeMirror varTypeArg )
{
    if ( argType == null || varTypeArg == null ){
        return false;
    }
    Types types = getImplementation().getHelper().getCompilationController().
        getTypes();

    /*
     * Implementation of spec item :
     * the required type parameter and the bean type parameter are actual 
     * types with identical raw type, and, if the type is
     * parameterized, the bean type parameter is assignable to the required 
     * type parameter according to these rules
     */
    if ( argType.getKind()!= TypeKind.TYPEVAR && 
            varTypeArg.getKind()!= TypeKind.TYPEVAR &&
            (argType instanceof ReferenceType) && 
            (varTypeArg instanceof ReferenceType) )
    {
        return checkIsAssignable(getImplementation().getHelper().
                getCompilationController().getTypes(), argType, varTypeArg);
    }
    
    if ( varTypeArg.getKind() == TypeKind.WILDCARD  ){
        return handleWildCard(argType, (WildcardType)varTypeArg, types);
    }
    
    if ( argType.getKind() == TypeKind.TYPEVAR &&
            varTypeArg.getKind() == TypeKind.TYPEVAR)
    {
        return handleBothTypeVars(argType, varTypeArg, types);
    }
    
    if (varTypeArg.getKind() != TypeKind.TYPEVAR
            && argType.getKind() == TypeKind.TYPEVAR)
    {
        return handleBeanTypeVar(argType, varTypeArg, types);
    }
    if (argType.getKind() != TypeKind.TYPEVAR
            && varTypeArg.getKind() == TypeKind.TYPEVAR)
    {
        return handleRequiredTypeVar(argType, varTypeArg, types);
    }
    
    return false;
}
 
Example 16
Source File: ExternalDomainMetaFactory.java    From doma with Apache License 2.0 4 votes vote down vote up
private void doDomainType(
    TypeElement converterElement, TypeMirror domainType, ExternalDomainMeta meta) {
  meta.setType(domainType);

  ArrayType arrayType = ctx.getMoreTypes().toArrayType(domainType);
  if (arrayType != null) {
    TypeMirror componentType = arrayType.getComponentType();
    if (componentType.getKind() == TypeKind.ARRAY) {
      throw new AptException(Message.DOMA4447, converterElement, new Object[] {});
    }
    TypeElement componentElement = ctx.getMoreTypes().toTypeElement(componentType);
    if (componentElement == null) {
      throw new AptIllegalStateException(componentType.toString());
    }
    if (!componentElement.getTypeParameters().isEmpty()) {
      throw new AptException(Message.DOMA4448, converterElement, new Object[] {});
    }
    return;
  }

  TypeElement domainElement = ctx.getMoreTypes().toTypeElement(domainType);
  if (domainElement == null) {
    throw new AptIllegalStateException(domainType.toString());
  }
  if (domainElement.getNestingKind().isNested()) {
    validateEnclosingElement(domainElement);
  }
  PackageElement pkgElement = ctx.getMoreElements().getPackageOf(domainElement);
  if (pkgElement.isUnnamed()) {
    throw new AptException(
        Message.DOMA4197, converterElement, new Object[] {domainElement.getQualifiedName()});
  }
  DeclaredType declaredType = ctx.getMoreTypes().toDeclaredType(domainType);
  if (declaredType == null) {
    throw new AptIllegalStateException(domainType.toString());
  }
  for (TypeMirror typeArg : declaredType.getTypeArguments()) {
    if (typeArg.getKind() != TypeKind.WILDCARD) {
      throw new AptException(
          Message.DOMA4203, converterElement, new Object[] {domainElement.getQualifiedName()});
    }
  }
  meta.setTypeElement(domainElement);
  TypeParametersDef typeParametersDef = ctx.getMoreElements().getTypeParametersDef(domainElement);
  meta.setTypeParametersDef(typeParametersDef);
}
 
Example 17
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Note: may return {@code null}, if an intersection type is encountered, to indicate a 
 * real type cannot be created.
 */
private static TypeMirror resolveCapturedTypeInt(CompilationInfo info, TypeMirror tm) {
    if (tm == null) return tm;
    
    TypeMirror orig = SourceUtils.resolveCapturedType(tm);

    if (orig != null) {
        tm = orig;
    }
    
    if (tm.getKind() == TypeKind.WILDCARD) {
        TypeMirror extendsBound = ((WildcardType) tm).getExtendsBound();
        TypeMirror superBound = ((WildcardType) tm).getSuperBound();
        if (extendsBound != null || superBound != null) {
            TypeMirror rct = resolveCapturedTypeInt(info, extendsBound != null ? extendsBound : superBound);
            if (rct != null) {
                switch (rct.getKind()) {
                    case WILDCARD:
                        return rct;
                    case ARRAY:
                    case DECLARED:
                    case ERROR:
                    case TYPEVAR:
                    case OTHER:
                        return info.getTypes().getWildcardType(
                                extendsBound != null ? rct : null, superBound != null ? rct : null);
                }
            } else {
                // propagate failure out of all wildcards
                return null;
            }
        }
    } else if (tm.getKind() == TypeKind.INTERSECTION) {
        return null;
    }
    
    if (tm.getKind() == TypeKind.DECLARED) {
        DeclaredType dt = (DeclaredType) tm;
        List<TypeMirror> typeArguments = new LinkedList<TypeMirror>();
        
        for (TypeMirror t : dt.getTypeArguments()) {
            TypeMirror targ = resolveCapturedTypeInt(info, t);
            if (targ == null) {
                // bail out, if the type parameter is a wildcard, it's probably not possible
                // to create a proper parametrized type from it
                if (t.getKind() == TypeKind.WILDCARD || t.getKind() == TypeKind.INTERSECTION) {
                    return null;
                }
                // use rawtype
                typeArguments.clear();
                break;
            }
            typeArguments.add(targ);
        }
        
        final TypeMirror enclosingType = dt.getEnclosingType();
        if (enclosingType.getKind() == TypeKind.DECLARED) {
            return info.getTypes().getDeclaredType((DeclaredType) enclosingType, (TypeElement) dt.asElement(), typeArguments.toArray(new TypeMirror[0]));
        } else {
            if (dt.asElement() == null) return dt;
            return info.getTypes().getDeclaredType((TypeElement) dt.asElement(), typeArguments.toArray(new TypeMirror[0]));
        }
    }

    if (tm.getKind() == TypeKind.ARRAY) {
        ArrayType at = (ArrayType) tm;
        TypeMirror tm2 = resolveCapturedTypeInt(info, at.getComponentType());
        return info.getTypes().getArrayType(tm2 != null ? tm2 : tm);
    }
    
    return tm;
}
 
Example 18
Source File: BeanModelBuilder.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void addEventSource(ExecutableElement m) {
    if (consumed) {
        return;
        
    }
    String sn = m.getSimpleName().toString();
    
    if (!isAccessible(m, false)) {
        return;
    }
    
    if (!sn.startsWith(EVENT_PREFIX) || sn.length() == EVENT_PREFIX_LEN) {
        return;
    }
    
    if (m.getParameters().size() != 1) {
        return;
    }
    VariableElement param = m.getParameters().get(0);
    TypeMirror varType = param.asType();
    
    // the type must be assignable to the event handler
    if (compilationInfo.getTypes().isAssignable(varType, getHandlerBaseType())) {
        return;
    }
    ElementHandle<TypeElement> eventHandle = null;
    String eventClassName = null;
    
    if (varType.getKind() == TypeKind.DECLARED) {
        // extract event type as the type of event / argument for the event handler method
        DeclaredType dt = (DeclaredType)varType;
        List<? extends TypeMirror> tParams = dt.getTypeArguments();
        if (tParams.size() != 1) {
            // something very wrong, the event handler has just 1 type parameter
            //throw new IllegalStateException();
            return;
        }
        TypeMirror eventType = tParams.get(0);
        if (eventType.getKind() == TypeKind.WILDCARD) {
            TypeMirror t = ((WildcardType)eventType).getSuperBound();
            if (t == null) {
                t = ((WildcardType)eventType).getExtendsBound();
            }
            eventType = t;
        }
        if (eventType.getKind() != TypeKind.DECLARED) {
            throw new IllegalStateException();
        }
        TypeElement te = (TypeElement)compilationInfo.getTypes().asElement(eventType);
        eventClassName = te.getQualifiedName().toString();
        eventHandle = ElementHandle.create(te);
        addDependency(eventType);
    }

    String eventName = Character.toLowerCase(sn.charAt(EVENT_PREFIX_LEN)) + sn.substring(EVENT_PREFIX_LEN + 1);
    FxEvent ei = new FxEvent(eventName);
    ei.setEventClassName(eventClassName);
    ei.setEventType(eventHandle);
    
    addEvent(ei);
    
    consumed = true;
}
 
Example 19
Source File: Type.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public TypeKind getKind() {
    return TypeKind.WILDCARD;
}
 
Example 20
Source File: WildcardTypeImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public TypeKind getKind() {
	return TypeKind.WILDCARD;
}