Java Code Examples for com.strobel.assembler.metadata.TypeReference#isPrimitive()

The following examples show how to use com.strobel.assembler.metadata.TypeReference#isPrimitive() . 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: SerializationIdiom.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
@ClassVisitor
public void visitClass(TypeDefinition td, ClassContext cc) {
    isSerializable = Types.isInstance(td, "java/io/Serializable");
    if(Types.isInstance(td, "java/util/Comparator") && !td.isAnonymous() && !td.isLocalClass()
            && !isSerializable) {
        int priority = 0;
        for(FieldDefinition fd : td.getDeclaredFields()) {
            TypeReference fieldType = fd.getFieldType();
            while(fieldType.isArray())
                fieldType = fieldType.getElementType();
            if(fieldType.isPrimitive())
                continue;
            if(Types.isInstance(fieldType, "java/io/Serializable")) {
                priority+=10;
                if(priority > 20)
                    break;
            }
        }
        cc.report("ComparatorIsNotSerializable", priority, SHOULD_IMPLEMENT.create("java/io/Serializable"));
    }
}
 
Example 2
Source File: SingleType.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
static EType of(TypeReference tr, What what) {
    if (tr == null || tr.isPrimitive() || (what == What.SUBTYPE && Types.isObject(tr)))
        return UNKNOWN;
    TypeDefinition td = tr.resolve();
    if (td == null)
        return UNKNOWN;
    if (td.isFinal() || td.isPrimitive()) {
        if (what == What.SUBTYPE)
            what = What.EXACT;
        if (what == What.NOT)
            what = What.NOT_SUBTYPE;
    }
    TypeReference newTr = td;
    while (tr.isArray()) {
        tr = tr.getElementType();
        newTr = newTr.makeArrayType();
    }
    boolean complete = Types.hasCompleteHierarchy(td);
    return new SingleType(newTr, what, complete);
}
 
Example 3
Source File: Types.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
public static boolean isInstance(TypeReference type, String wantedType) {
    if (type == null || type.isPrimitive())
        return false;
    if (wantedType.equals("java/lang/Object"))
        return true;
    if (type.getInternalName().equals(wantedType))
        return true;
    if (type.isArray()) {
        if(!wantedType.startsWith("["))
            return false;
        return isInstance(type.getElementType(), wantedType.substring(1));
    }
    TypeDefinition td = type.resolve();
    if (td == null)
        return false;
    for (TypeReference iface : td.getExplicitInterfaces()) {
        if (isInstance(iface, wantedType))
            return true;
    }
    TypeReference bt = td.getBaseType();
    if (bt == null)
        return false;
    return isInstance(bt, wantedType);
}
 
Example 4
Source File: CovariantArrays.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
@AstVisitor(nodes=AstNodes.EXPRESSIONS)
public void visit(Expression expr, MethodContext mc, Hierarchy h) {
    if(expr.getCode() == AstCode.StoreElement) {
        TypeReference arrayType = ValuesFlow.reduceType(Exprs.getChild(expr, 0));
        TypeReference valueType = ValuesFlow.reduceType(Exprs.getChild(expr, 2));
        if (arrayType == null || valueType == null || arrayType == BuiltinTypes.Null
            || valueType == BuiltinTypes.Null || valueType.isPrimitive())
            return;
        valueType = toRawType(valueType);
        if(Types.isObject(valueType))
            return;
        if(!arrayType.isArray())
            return;
        TypeReference arrayElementType = arrayType.getElementType();
        arrayElementType = toRawType(arrayElementType);
        if(Types.isObject(arrayElementType))
            return;
        if(!Types.isInstance(valueType, arrayElementType)) {
            int priority = 0;
            if(Types.isInstance(arrayElementType, valueType)) {
                priority += 20;
                if(allImplementationsDerivedFromSubclass(h, valueType, arrayElementType))
                    return;
            }
            mc.report("ContravariantArrayStore", priority, expr, Roles.ARRAY_TYPE.create(arrayType), Roles.VALUE_TYPE.create(valueType));
        }
    }
}
 
Example 5
Source File: MethodTranslator.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private TypeMirror resolve(TypeReference typeRef) {
  if (typeRef.isArray()) {
    // Multi-dimension arrays will recurse.
    TypeMirror componentType = resolve(typeRef.getElementType());
    return parserEnv.typeUtilities().getArrayType(componentType);
  }
  if (typeRef.isPrimitive() || typeRef.isVoid()) {
    return typeUtil.resolvePrimitiveType(typeRef.getSignature());
  }
  String typeName = typeRef.getFullName();
  Element element = parserEnv.elementUtilities().getTypeElement(typeName);
  // TODO(tball): element is raw, any support for type parameters needed?
  return element.asType();
}