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

The following examples show how to use com.strobel.assembler.metadata.TypeReference#isArray() . 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: ToArrayDowncast.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
@AstVisitor(nodes = AstNodes.EXPRESSIONS)
public void visit(Expression expr, MethodContext mc) {
    if (expr.getCode() != AstCode.CheckCast)
        return;
    TypeReference targetType = (TypeReference) expr.getOperand();
    if (!targetType.isArray() || Types.isObject(targetType.getElementType()))
        return;
    Expression arg = Exprs.getChild(expr, 0);
    if (arg.getCode() != AstCode.InvokeVirtual && arg.getCode() != AstCode.InvokeInterface)
        return;
    MethodReference mr = (MethodReference) arg.getOperand();
    if (!mr.getName().equals("toArray") || !mr.getSignature().equals("()[Ljava/lang/Object;"))
        return;
    Expression target = Exprs.getChild(arg, 0);
    if (!Types.isInstance(target.getInferredType(), "java/util/Collection"))
        return;
    mc.report("ImpossibleToArrayDowncast", 0, target, Roles.TARGET_TYPE.create(targetType),
        TARGET_ELEMENT_TYPE.create(targetType.getElementType()));
}
 
Example 3
Source File: IncorrectVarArg.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
@AstVisitor(nodes=AstNodes.EXPRESSIONS)
public void visit(Expression expr, MethodContext mc) {
    if(expr.getOperand() instanceof MethodReference) {
        MethodReference mr = (MethodReference) expr.getOperand();
        if(mr.getErasedSignature().contains("[Ljava/lang/Object;)")) {
            Expression lastArg = expr.getArguments().get(expr.getArguments().size()-1);
            if(lastArg.getCode() == AstCode.InitArray && lastArg.getArguments().size() == 1) {
                TypeReference nested = lastArg.getArguments().get(0).getInferredType();
                if(nested != null && nested.isArray() && nested.getElementType().isPrimitive()) {
                    int priority = 0;
                    if(!highPriority(mr)) {
                        priority = 10;
                        MethodDefinition md = mr.resolve();
                        if(md == null) {
                            priority = 20;
                        } else if(!md.isVarArgs())
                            return;
                    }
                    mc.report("PrimitiveArrayPassedAsVarArg", priority, lastArg, Roles.CALLED_METHOD.create(mr),
                        Roles.ARRAY_TYPE.create(nested));
                }
            }
        }
    }
}
 
Example 4
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 5
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 6
Source File: Types.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
public static TypeReference mergeTypes(TypeReference t1, TypeReference t2) {
    if (t1 == null || t2 == null)
        return null;
    if (t1 == BuiltinTypes.Null)
        return t2;
    if (t2 == BuiltinTypes.Null)
        return t1;
    if (t1.isEquivalentTo(t2))
        return t1;
    if(t1.isArray() ^ t2.isArray())
        return null;
    if(t1.isArray()) {
        TypeReference merged = mergeTypes(t1.getElementType(), t2.getElementType());
        return merged == null ? null : merged.makeArrayType();
    }
    List<TypeReference> chain1 = getBaseTypes(t1);
    List<TypeReference> chain2 = getBaseTypes(t2);
    for (int i = Math.min(chain1.size(), chain2.size()) - 1; i >= 0; i--) {
        if (chain1.get(i).equals(chain2.get(i)))
            return chain1.get(i);
    }
    return null;
}
 
Example 7
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 8
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();
}
 
Example 9
Source File: Types.java    From huntbugs with Apache License 2.0 4 votes vote down vote up
/**
 * @param type
 * @return true if type is known to be mutable
 */
public static boolean isMutable(TypeReference type) {
    if (type.isArray())
        return true;
    return MUTABLE_TYPES.contains(type.getInternalName());
}