com.strobel.assembler.metadata.TypeDefinition Java Examples

The following examples show how to use com.strobel.assembler.metadata.TypeDefinition. 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: ReturnNull.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
@AstVisitor(nodes = AstNodes.EXPRESSIONS)
public void visit(Expression expr, NodeChain nc, MethodContext mc, MethodDefinition md, TypeDefinition td) {
    if (expr.getCode() == AstCode.Return && !expr.getArguments().isEmpty()) {
        Expression child = Exprs.getChild(expr, 0);
        if (child.getCode() == AstCode.AConstNull) {
            MethodDefinition curMethod = nc.getLambdaMethod();
            if (curMethod == null)
                curMethod = md;
            String warningType = curMethod.getReturnType().isArray() ? "ArrayReturnNull" : TYPE_TO_WARNING
                    .get(curMethod.getReturnType().getInternalName());
            if (warningType != null) {
                int priority = 0;
                if (!td.isPublic() || md.isPrivate() || md.isPackagePrivate())
                    priority = 20;
                else if (md.isProtected() || md != curMethod)
                    priority = 10;
                // Method which simply contains "return null": probably stub or something
                if(nc.getParent() == null && nc.isOnlyChild(expr))
                    priority += 10;
                mc.report(warningType, priority, expr.getArguments().get(0), RETURN_TYPE.create(md
                        .getReturnType()));
            }
        }
    }
}
 
Example #2
Source File: SyncGetClass.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
@AstVisitor
public void visit(Node node, MethodContext mc, TypeHierarchy th, TypeDefinition td) {
    if(node instanceof TryCatchBlock) {
        Expression syncObject = Nodes.getSyncObject((TryCatchBlock) node);
        if(syncObject != null) {
            if(syncObject.getCode() == AstCode.InvokeVirtual && Methods.isGetClass((MethodReference) syncObject.getOperand())
                    && Exprs.isThis(Exprs.getChild(syncObject, 0))) {
                int priority = 0;
                if(th != null && !th.hasSubClasses())
                    priority += 10;
                if(Nodes.find(node, n -> isStaticFieldAccess(n, td)) == null)
                    priority += 15;
                mc.report("SyncOnGetClass", priority, syncObject);
            }
        }
    }
}
 
Example #3
Source File: DecompilerLinkProvider.java    From Luyten with Apache License 2.0 6 votes vote down vote up
private FieldDefinition findFieldInType(TypeDefinition typeDef, String uniqueStr) {
	String[] linkParts = uniqueStr.split("\\|");
	if (linkParts.length != 4)
		return null;
	String fieldName = linkParts[3];
	if (fieldName.trim().length() <= 0)
		return null;
	List<FieldDefinition> declaredFields = typeDef.getDeclaredFields();
	if (declaredFields == null)
		return null;
	boolean isFound = false;
	for (FieldDefinition declaredField : declaredFields) {
		isFound = (declaredField != null && fieldName.equals(declaredField.getName()));
		if (isFound) {
			if (declaredField.isSynthetic()) {
				return null;
			} else {
				return declaredField;
			}
		}
	}
	return null;
}
 
Example #4
Source File: LockProblems.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.InvokeVirtual)
        return;
    MethodReference mr = (MethodReference) expr.getOperand();
    String name = mr.getName();
    if (!Types.isObject(mr.getDeclaringType()) || (!name.equals("wait") && !name.startsWith("notify")))
        return;
    TypeReference type = ValuesFlow.reduceType(expr.getArguments().get(0));
    if (type == null || !type.getInternalName().startsWith("java/util/concurrent/"))
        return;
    TypeDefinition target = type.resolve();
    if (target == null || !target.isPublic())
        return;
    MethodDefinition replacement = findReplacement(name, target);
    if(replacement != null) {
        mc.report("IncorrectConcurrentMethod", 0, expr, TARGET.create(target), Roles.REPLACEMENT_METHOD.create(replacement));
    }
}
 
Example #5
Source File: CovariantArrays.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
private boolean allImplementationsDerivedFromSubclass(Hierarchy h, TypeReference superClass,
        TypeReference subClass) {
    TypeDefinition td = superClass.resolve();
    if(td == null || (!td.isInterface() && !Flags.testAny(td.getFlags(), Flags.ABSTRACT)) )
        return false;
    for(TypeHierarchy th : h.get(td).getSubClasses()) {
        if(subClass.getInternalName().equals(th.getInternalName()))
            continue;
        if(th.hasFlag(Flags.INTERFACE) || th.hasFlag(Flags.ABSTRACT))
            continue;
        TypeReference subType = td.getResolver().lookupType(th.getInternalName());
        if(subType == null || Types.isInstance(subType, subClass))
            continue;
        return false;
    }
    return true;
}
 
Example #6
Source File: DecompilerLinkProvider.java    From Luyten with Apache License 2.0 6 votes vote down vote up
private TypeReference getMostOuterTypeRefBySlowLookuping(TypeReference typeRef) {
	String name = typeRef.getName();
	if (name == null)
		return typeRef;
	String packageName = typeRef.getPackageName();
	if (packageName == null)
		return typeRef;
	String[] nameParts = name.split("\\$");
	String newName = "";
	String sep = "";
	for (int i = 0; i < nameParts.length - 1; i++) {
		newName = newName + sep + nameParts[i];
		sep = "$";
		String newInternalName = packageName.replaceAll("\\.", "/") + "/" + newName;
		TypeReference newTypeRef = metadataSystem.lookupType(newInternalName);
		if (newTypeRef != null) {
			TypeDefinition newTypeDef = newTypeRef.resolve();
			if (newTypeDef != null) {
				return newTypeRef;
			}
		}
	}
	return typeRef;
}
 
Example #7
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 #8
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 #9
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 #10
Source File: Naming.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
@AstVisitor(nodes = AstNodes.ROOT)
public void checkSameAsConstructor(Block root, MethodDefinition md, TypeDefinition td, MethodContext mc) {
    if (md.getName().equals(td.getSimpleName()) && md.getReturnType().isVoid() && !md.isDeprecated()) {
        int priority = 0;
        if (root.getBody().isEmpty()) {
            priority += 20;
        } else if (root.getBody().size() == 1 && Nodes.isOp(root.getBody().get(0), AstCode.AThrow)) {
            priority += 40;
        }
        if (td.getDeclaredMethods().stream().anyMatch(
            m -> m.isConstructor() && m.getErasedSignature().equals(md.getErasedSignature()))) {
            priority += 10;
        }
        mc.report("BadNameOfMethodSameAsConstructor", priority);
    }
}
 
Example #11
Source File: FieldAccess.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
private boolean checkWrite(FieldContext fc, FieldDefinition fd, TypeDefinition td, FieldRecord fieldRecord, int flags, boolean isConstantType) {
    if(!Flags.testAny(flags, FieldStats.WRITE)) {
        if(fd.isStatic() && fd.isFinal() && isConstantType)
            return false;
        WarningAnnotation<?>[] anno = {};
        int priority = 0;
        String warningType = fd.isPublic() || fd.isProtected() ? "UnwrittenPublicField" : "UnwrittenPrivateField";
        if (fieldRecord != null && fieldRecord.firstRead != null) {
            anno = fieldRecord.firstRead.getAnnotations();
        }
        if(fd.isPublic()) {
            priority += 5;
        }
        priority += tweakForSerialization(fd, td);
        if(fd.getFieldType().getSimpleType() == JvmType.Boolean) {
            priority += 5;
        }
        if(fd.getName().equalsIgnoreCase("debug")) {
            priority += 5;
        }
        fc.report(warningType, priority, anno);
        return true;
    }
    return false;
}
 
Example #12
Source File: FieldAccess.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
private boolean checkNull(FieldContext fc, FieldDefinition fd, TypeDefinition td, FieldRecord fieldRecord, int flags) {
    if(!Flags.testAny(flags, FieldStats.WRITE_NONNULL) && Flags.testAny(flags, FieldStats.READ)) {
        int priority = 0;
        if(fd.isFinal() && fd.isStatic()) {
            priority += 20;
            String lcName = fd.getName().toLowerCase(Locale.ENGLISH);
            if (lcName.contains("null") || lcName.contains("zero") || lcName.contains("empty")) {
                priority += 15;
            }
        } else if(fd.isPublic()) {
            priority += 10;
        }
        priority += tweakForSerialization(fd, td);
        fc.report("FieldIsAlwaysNull", priority, getWriteAnnotations(fieldRecord));
        return true;
    }
    return false;
}
 
Example #13
Source File: Context.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
MetadataSystem createMetadataSystem() {
    return new MetadataSystem(loader) {
        Set<String> loadedTypes = new HashSet<>();
        
        @Override
        protected TypeDefinition resolveType(String descriptor, boolean mightBePrimitive) {
            if(missingClasses.contains(descriptor)) {
                return null;
            }
            try {
                if(loadedTypes.add(descriptor))
                    incStat("ClassLoadingEfficiency.Total");
                if(classes.add(descriptor))
                    incStat("ClassLoadingEfficiency");
                return super.resolveType(descriptor, mightBePrimitive);
            } catch (Throwable t) {
                addError(new ErrorMessage(null, descriptor, null, null, -1, t));
                missingClasses.add(descriptor);
                return null;
            }
        }
    };
}
 
Example #14
Source File: DecompilerLinkProvider.java    From Luyten with Apache License 2.0 6 votes vote down vote up
private MethodDefinition findMethodInType(TypeDefinition typeDef, String uniqueStr) {
	String[] linkParts = uniqueStr.split("\\|");
	if (linkParts.length != 5)
		return null;
	String methodName = linkParts[3];
	String methodErasedSignature = linkParts[4];
	if (methodName.trim().length() <= 0 || methodErasedSignature.trim().length() <= 0)
		return null;
	List<MethodDefinition> declaredMethods = typeDef.getDeclaredMethods();
	if (declaredMethods == null)
		return null;
	boolean isFound = false;
	for (MethodDefinition declaredMethod : declaredMethods) {
		isFound = (declaredMethod != null && methodName.equals(declaredMethod.getName()));
		isFound = (isFound && methodErasedSignature.equals(declaredMethod.getErasedSignature()));
		if (isFound) {
			if (declaredMethod.isSynthetic() && !settings.getShowSyntheticMembers()) {
				return null;
			} else {
				return declaredMethod;
			}
		}
	}
	return null;
}
 
Example #15
Source File: DeclaredAnnotations.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
@Override
protected void visitType(TypeDefinition td) {
    if (!td.isAnnotation())
        return;
    DeclaredAnnotation da = getOrCreate(td);
    for (CustomAnnotation ca : td.getAnnotations()) {
        if (Types.is(ca.getAnnotationType(), Retention.class)) {
            for (AnnotationParameter ap : ca.getParameters()) {
                if (ap.getMember().equals("value")) {
                    AnnotationElement value = ap.getValue();
                    if (value instanceof EnumAnnotationElement) {
                        EnumAnnotationElement enumValue = (EnumAnnotationElement) value;
                        if (Types.is(enumValue.getEnumType(), RetentionPolicy.class)) {
                            da.policy = RetentionPolicy.valueOf(enumValue.getEnumConstantName());
                        }
                    }
                }
            }
        }
    }
}
 
Example #16
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 #17
Source File: InitializerRefersSubclass.java    From huntbugs with Apache License 2.0 6 votes vote down vote up
@AstVisitor(nodes=AstNodes.EXPRESSIONS, methodName="<clinit>")
public void visit(Expression expr, NodeChain nc, MethodContext mc, TypeDefinition td) {
    if(expr.getOperand() instanceof MemberReference) {
        MemberReference mr = (MemberReference) expr.getOperand();
        TypeReference tr = mr.getDeclaringType();
        TypeDefinition subType = tr == null ? null : tr.resolve();
        if (subType != null && (subType.isAnonymous() || subType.isLocalClass())) {
            subType = subType.getBaseType().resolve();
        }
        if (subType != null && !td.isEquivalentTo(subType) && Types.isInstance(subType, td) && nc
                .getLambdaMethod() == null) {
            mc.report("InitializerRefersSubclass", td.isNonPublic() || subType.isNonPublic() ? 5 : 0, expr,
                Roles.SUBCLASS.create(subType));
        }
    }
}
 
Example #18
Source File: Detector.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
static boolean runExpression(MethodHandle mh, Object det, Node n, NodeChain nc, MethodContext mc,
        MethodDefinition md, TypeDefinition td) throws Throwable {
    if (n instanceof Expression) {
        return (boolean) mh.invokeExact(det, (Expression) n, nc, mc, md, td);
    }
    return true;
}
 
Example #19
Source File: ClassFile.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static CompilationUnit decompileClassFile(TypeReference typeRef) {
  TypeDefinition typeDef = typeRef.resolve();
  DeobfuscationUtilities.processType(typeDef);
  DecompilationOptions options = new DecompilationOptions();
  DecompilerSettings settings = DecompilerSettings.javaDefaults();
  settings.setShowSyntheticMembers(true);
  options.setSettings(settings);
  options.setFullDecompilation(true);
  return Languages.java().decompileTypeToAst(typeDef, options);
}
 
Example #20
Source File: MethodStats.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
@Override
protected void visitType(TypeDefinition td) {
    for (MethodDefinition md : td.getDeclaredMethods()) {
        MethodData mdata = getMethodData(md);
        if (md.isFinal() || td.isFinal() || md.isStatic() || md.isPrivate()) {
            mdata.flags |= METHOD_FINAL;
        }
        visitMethod(mdata, md);
        for (MethodDefinition superMethod : Methods.findSuperMethods(md)) {
            getMethodData(superMethod).addSubMethod(mdata);
        }
    }
}
 
Example #21
Source File: Detector.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
static boolean runBody(MethodHandle mh, Object det, Node n, NodeChain nc, MethodContext mc,
        MethodDefinition md, TypeDefinition td) throws Throwable {
    if (nc == null) {
        mh.invokeExact(det, (Block) n, mc, md, td);
    }
    return true;
}
 
Example #22
Source File: Model.java    From Luyten with Apache License 2.0 5 votes vote down vote up
public void startWarmUpThread() {
	new Thread() {
		public void run() {
			try {
				Thread.sleep(500);
				String internalName = FindBox.class.getName();
				TypeReference type = metadataSystem.lookupType(internalName);
				TypeDefinition resolvedType = null;
				if ((type == null) || ((resolvedType = type.resolve()) == null)) {
					return;
				}
				StringWriter stringwriter = new StringWriter();
				PlainTextOutput plainTextOutput = new PlainTextOutput(stringwriter);
				plainTextOutput
						.setUnicodeOutputEnabled(decompilationOptions.getSettings().isUnicodeOutputEnabled());
				settings.getLanguage().decompileType(resolvedType, plainTextOutput, decompilationOptions);
				String decompiledSource = stringwriter.toString();
				OpenFile open = new OpenFile(internalName, "*/" + internalName, getTheme(), mainWindow);
				open.setContent(decompiledSource);
				JTabbedPane pane = new JTabbedPane();
				pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
				pane.addTab("title", open.scrollPane);
				pane.setSelectedIndex(pane.indexOfTab("title"));
			} catch (Exception e) {
				Luyten.showExceptionDialog("Exception!", e);
			}
		}
	}.start();
}
 
Example #23
Source File: Hierarchy.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
@Override
protected void visitType(TypeDefinition td) {
    TypeHierarchy th = getOrCreate(td);
    th.flags = td.getFlags();
    link(th, td.getBaseType());
    for (TypeReference id : td.getExplicitInterfaces())
        link(th, id);
}
 
Example #24
Source File: Mutability.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
@Override
protected void visitType(TypeDefinition td) {
    if(!td.isPublic())
        return;
    for(FieldDefinition fd : td.getDeclaredFields()) {
        if(!fd.isStatic() && !fd.isFinal() && fd.isPublic()) {
            getOrCreate(td);
            return;
        }
    }
}
 
Example #25
Source File: SingleType.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
@Override
public YesNoMaybe is(TypeReference other, boolean exact) {
    switch (what) {
    case EXACT:
        if (exact)
            return YesNoMaybe.of(tr.getInternalName().equals(other.getInternalName()));
        if (Types.isInstance(tr, other))
            return YesNoMaybe.YES;
        if (!complete)
            return YesNoMaybe.MAYBE;
        return YesNoMaybe.NO;
    case NOT:
        if (exact)
            return YesNoMaybe.of(!tr.getInternalName().equals(other.getInternalName()));
        return YesNoMaybe.MAYBE;
    case NOT_SUBTYPE:
        return Types.isInstance(other, tr) ? YesNoMaybe.NO : YesNoMaybe.MAYBE;
    case SUBTYPE: {
        if (exact)
            return Types.isInstance(other, tr) || !Types.hasCompleteHierarchy(other.resolve()) ? YesNoMaybe.MAYBE
                    : YesNoMaybe.NO;
        if (Types.isInstance(tr, other))
            return YesNoMaybe.YES;
        if (!complete || tr.resolve().isInterface())
            return YesNoMaybe.MAYBE;
        TypeDefinition superTd = other.resolve();
        if (superTd == null || superTd.isInterface() || Types.isInstance(other, tr) || !Types.hasCompleteHierarchy(
            superTd))
            return YesNoMaybe.MAYBE;
        return YesNoMaybe.NO;
    }
    default:
        throw new InternalError();
    }
}
 
Example #26
Source File: FileSaver.java    From Luyten with Apache License 2.0 5 votes vote down vote up
private void doSaveClassDecompiled(File inFile, File outFile) throws Exception {
	DecompilerSettings settings = cloneSettings();
	LuytenTypeLoader typeLoader = new LuytenTypeLoader();
	MetadataSystem metadataSystem = new MetadataSystem(typeLoader);
	TypeReference type = metadataSystem.lookupType(inFile.getCanonicalPath());

	DecompilationOptions decompilationOptions = new DecompilationOptions();
	decompilationOptions.setSettings(settings);
	decompilationOptions.setFullDecompilation(true);

	boolean isUnicodeEnabled = decompilationOptions.getSettings().isUnicodeOutputEnabled();
	TypeDefinition resolvedType = null;
	if (type == null || ((resolvedType = type.resolve()) == null)) {
		throw new Exception("Unable to resolve type.");
	}
	StringWriter stringwriter = new StringWriter();
	PlainTextOutput plainTextOutput = new PlainTextOutput(stringwriter);
	plainTextOutput.setUnicodeOutputEnabled(isUnicodeEnabled);
	settings.getLanguage().decompileType(resolvedType, plainTextOutput, decompilationOptions);
	String decompiledSource = stringwriter.toString();

	System.out.println("[SaveAll]: " + inFile.getName() + " -> " + outFile.getName());
	try (FileOutputStream fos = new FileOutputStream(outFile);
			OutputStreamWriter writer = isUnicodeEnabled ? new OutputStreamWriter(fos, "UTF-8")
					: new OutputStreamWriter(fos);
			BufferedWriter bw = new BufferedWriter(writer);) {
		bw.write(decompiledSource);
		bw.flush();
	}
}
 
Example #27
Source File: ClassFields.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
public ClassFields(TypeDefinition td, FieldStats fieldStats, MethodStats methodStats) {
    this.ms = methodStats;
    for (FieldDefinition fd : td.getDeclaredFields()) {
        fields.put(new MemberInfo(fd), fd);
        int flags = fieldStats.getFlags(fd);
        if(Flags.testAny(flags, FieldStats.WRITE_CONSTRUCTOR) &&
                !Flags.testAny(flags, FieldStats.WRITE_CLASS | FieldStats.WRITE_PACKAGE | FieldStats.WRITE_OUTSIDE) &&
                !Annotations.hasAnnotation(fd, true)) {
            initializedInCtor.add(fd);
        }
    }
}
 
Example #28
Source File: SampleCustomDetector.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
@MethodVisitor
public void visit(MethodContext mc, MethodDefinition md, TypeDefinition td) {
    TypeReference returnType = md.getReturnType();
    if (Types.isCollection(returnType)) {
        mc.report("SampleCustomDetector", 5);
    }
}
 
Example #29
Source File: UncalledPrivateMethod.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
@Override
protected void visitType(TypeDefinition td) {
    TypeReference tr = td.getDeclaringType();
    if(tr == null) return;
    TypeDefinition outer = tr.resolve();
    if(outer == null || !outer.isAnonymous()) return;
    for(MethodDefinition md : td.getDeclaredMethods()) {
        extractCalls(md, mr -> {
            mis.add(new MemberInfo(mr));
            return true;
        });
    }
}
 
Example #30
Source File: Context.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
void analyzeClass(MetadataSystem ms, String name) {
    classesCount.incrementAndGet();
    TypeDefinition type;
    try {
        type = lookUp(ms, name);
    } catch (Throwable t) {
        addError(new ErrorMessage(null, name, null, null, -1, t));
        return;
    }
    if (type != null)
        registry.analyzeClass(type);
}