Java Code Examples for com.strobel.assembler.metadata.TypeDefinition#getBaseType()

The following examples show how to use com.strobel.assembler.metadata.TypeDefinition#getBaseType() . 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: Naming.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
@ClassVisitor
public void visitClass(TypeDefinition td, ClassContext cc) {
    if (td.isAnonymous() || td.isSynthetic())
        return;
    String name = td.getSimpleName();
    if (Character.isLetter(name.charAt(0)) && !Character.isUpperCase(name.charAt(0)) && name.indexOf('_') == -1) {
        cc.report("BadNameOfClass", td.isPublic() ? 0 : 15);
    }
    if (name.endsWith("Exception") && !Types.isInstance(td, "java/lang/Throwable")) {
        cc.report("BadNameOfClassException", td.isPublic() ? 0 : 15);
    }
    TypeReference superClass = td.getBaseType();
    if (superClass != null && superClass.getSimpleName().equals(name)) {
        cc.report("BadNameOfClassSameAsSuperclass", td.isPublic() ? 0 : 15, Roles.SUPERCLASS.create(superClass));
    }
    for (TypeReference iface : td.getExplicitInterfaces()) {
        if (iface.getSimpleName().equals(name)) {
            cc.report("BadNameOfClassSameAsInterface", td.isPublic() ? 0 : 15, Roles.INTERFACE.create(iface));
        }
    }
}
 
Example 2
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 3
Source File: Types.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
public static List<TypeReference> getBaseTypes(TypeReference input) {
    List<TypeReference> result = new ArrayList<>();
    while (true) {
        result.add(input);
        TypeDefinition td = input.resolve();
        if (td == null)
            break;
        input = td.getBaseType();
        if (input == null)
            break;
    }
    Collections.reverse(result);
    return result;
}
 
Example 4
Source File: Types.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
/**
 * @param type type to check
 * @return true if all superclasses and superinterfaces could be loaded
 */
public static boolean hasCompleteHierarchy(TypeDefinition type) {
    if(type == null)
        return false;
    if(type.isArray())
        return hasCompleteHierarchy(type.getElementType().resolve());
    TypeReference base = type.getBaseType();
    if(base != null && !hasCompleteHierarchy(base.resolve()))
        return false;
    for(TypeReference tr : type.getExplicitInterfaces()) {
        if(!hasCompleteHierarchy(tr.resolve()))
            return false;
    }
    return true;
}