Java Code Examples for com.strobel.assembler.metadata.MethodReference#getDeclaringType()
The following examples show how to use
com.strobel.assembler.metadata.MethodReference#getDeclaringType() .
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: EqualsContract.java From huntbugs with Apache License 2.0 | 6 votes |
private static boolean isComparison(Expression expr) { switch(expr.getCode()) { case CmpEq: case CmpNe: return true; case InvokeVirtual: return Methods.isEqualsMethod((MethodReference) expr.getOperand()); case InvokeStatic: { MethodReference mr = (MethodReference) expr.getOperand(); TypeReference tr = mr.getDeclaringType(); return (mr.getName().equals("equals") || mr.getName().equals("deepEquals")) && (Types.is(tr, Arrays.class) || Types.is(tr, Objects.class)); } default: return false; } }
Example 2
Source File: CheckReturnValue.java From huntbugs with Apache License 2.0 | 6 votes |
@AstVisitor(nodes=AstNodes.EXPRESSIONS) public void visit(Expression expr, NodeChain nc, MethodContext mc) { if(nc.getNode() instanceof Expression) return; if(expr.getCode() == AstCode.InvokeVirtual || expr.getCode() == AstCode.InvokeInterface) { MethodReference mr = (MethodReference) expr.getOperand(); boolean skipMethod = mr.getName().equals("skip") && mr.getSignature().equals("(J)J") || mr.getName().equals("skipBytes") && mr.getSignature().equals("(I)I"); boolean readMethod = mr.getName().equals("read") && (mr.getSignature().equals("([B)I") || mr.getSignature().equals("([BII)I") || mr.getSignature().equals("([C)I") || mr.getSignature().equals("([CII)I")); if(skipMethod || readMethod) { TypeReference type = ValuesFlow.reduceType(expr.getArguments().get(0)); if(type == null) type = mr.getDeclaringType(); if(!isInputStream(type)) return; if(skipMethod && Types.isInstance(type, "javax/imageio/stream/ImageInputStream")) return; int priority = 0; if(!Types.isInstance(type, "java/io/BufferedInputStream")) priority += 15; mc.report(skipMethod ? "ReturnValueOfSkip" : "ReturnValueOfRead", priority, expr); } } }
Example 3
Source File: Nodes.java From huntbugs with Apache License 2.0 | 6 votes |
public static boolean isBoxing(Node node) { if (!isOp(node, AstCode.InvokeStatic)) return false; MethodReference ref = (MethodReference) ((Expression) node).getOperand(); if (!ref.getName().equals("valueOf")) return false; TypeReference type = ref.getDeclaringType(); if (type.getInternalName().equals("java/lang/Double") && ref.getSignature().equals("(D)Ljava/lang/Double;")) return true; if (type.getInternalName().equals("java/lang/Integer") && ref.getSignature().equals("(I)Ljava/lang/Integer;")) return true; if (type.getInternalName().equals("java/lang/Long") && ref.getSignature().equals("(J)Ljava/lang/Long;")) return true; if (type.getInternalName().equals("java/lang/Boolean") && ref.getSignature().equals("(Z)Ljava/lang/Boolean;")) return true; if (type.getInternalName().equals("java/lang/Short") && ref.getSignature().equals("(S)Ljava/lang/Short;")) return true; if (type.getInternalName().equals("java/lang/Character") && ref.getSignature().equals( "(C)Ljava/lang/Character;")) return true; if (type.getInternalName().equals("java/lang/Float") && ref.getSignature().equals("(F)Ljava/lang/Float;")) return true; if (type.getInternalName().equals("java/lang/Byte") && ref.getSignature().equals("(B)Ljava/lang/Byte;")) return true; return false; }
Example 4
Source File: Nodes.java From huntbugs with Apache License 2.0 | 6 votes |
public static boolean isUnboxing(Node node) { if (!isOp(node, AstCode.InvokeVirtual)) return false; MethodReference ref = (MethodReference) ((Expression) node).getOperand(); TypeReference type = ref.getDeclaringType(); if (type.getInternalName().equals("java/lang/Double") && ref.getName().equals("doubleValue")) return true; if (type.getInternalName().equals("java/lang/Integer") && ref.getName().equals("intValue")) return true; if (type.getInternalName().equals("java/lang/Long") && ref.getName().equals("longValue")) return true; if (type.getInternalName().equals("java/lang/Boolean") && ref.getName().equals("booleanValue")) return true; if (type.getInternalName().equals("java/lang/Short") && ref.getName().equals("shortValue")) return true; if (type.getInternalName().equals("java/lang/Character") && ref.getName().equals("charValue")) return true; if (type.getInternalName().equals("java/lang/Float") && ref.getName().equals("floatValue")) return true; if (type.getInternalName().equals("java/lang/Byte") && ref.getName().equals("byteValue")) return true; return false; }
Example 5
Source File: UnnecessaryBoxing.java From huntbugs with Apache License 2.0 | 5 votes |
private boolean isBoxedToString(Expression expr) { if (expr.getCode() == AstCode.InvokeVirtual) { MethodReference mr = (MethodReference) expr.getOperand(); if (mr.getName().equals("toString") && mr.getSignature().equals("()Ljava/lang/String;")) { TypeReference type = mr.getDeclaringType(); if (Types.isBoxed(type)) { return true; } } } return false; }
Example 6
Source File: RandomUsage.java From huntbugs with Apache License 2.0 | 5 votes |
void checkOnlyOnce(Expression node, MethodContext ctx) { if (node.getCode() != AstCode.InvokeVirtual || node.getArguments().get(0).getCode() != AstCode.InitObject) return; MethodReference ctor = (MethodReference) node.getArguments().get(0).getOperand(); TypeReference type = ctor.getDeclaringType(); if (!Types.isRandomClass(type) || type.getInternalName().equals("java/security/SecureRandom")) return; MethodReference mr = (MethodReference) node.getOperand(); if(mr.getReturnType().getPackageName().equals("java.util.stream")) return; if(Inf.BACKLINK.findTransitiveUsages(node, true).allMatch(this::isRandomInit)) return; ctx.report("RandomUsedOnlyOnce", 0, node, RANDOM_TYPE.create(type)); }
Example 7
Source File: Methods.java From huntbugs with Apache License 2.0 | 5 votes |
public static boolean isPure(MethodReference mr) { TypeReference tr = mr.getDeclaringType(); if(Types.isBoxed(tr) || tr.getInternalName().startsWith("java/time/")) return true; if(tr.getInternalName().equals("java/util/String")) return !mr.getName().equals("getChars"); if(tr.getInternalName().equals("java/lang/Math")) return !mr.getName().equals("random"); if(tr.getInternalName().equals("java/util/Objects")) return true; if(tr.getInternalName().equals("java/util/Optional")) return mr.getName().equals("get") || mr.getName().equals("orElse") || mr.getName().equals("isPresent"); return false; }
Example 8
Source File: Methods.java From huntbugs with Apache License 2.0 | 4 votes |
public static boolean isSideEffectFree(MethodReference mr) { if(isPure(mr)) return true; if(isEqualsMethod(mr) || isHashCodeMethod(mr)) return true; TypeReference tr = mr.getDeclaringType(); String sig = mr.getErasedSignature(); String name = mr.getName(); if(name.equals("toString") && sig.equals("()Ljava/lang/String;")) return true; switch(tr.getInternalName()) { case "java/util/Arrays": return name.equals("hashCode") || name.equals("equals") || name.equals("toString") || name.equals("binarySearch") || name.equals("stream") || name.equals("spliterator") || name.startsWith("deep") || name.startsWith("copyOf") || name.equals("asList"); case "java/lang/Object": return mr.isConstructor(); case "java/util/Collections": return name.equals("min") || name.equals("max") || name.startsWith("unmodifiable") || name.startsWith("synchronized") || name.startsWith("empty"); } if(Types.isCollection(tr)) { if(name.equals("contains") && sig.equals("(Ljava/lang/Object;)Z")) return true; if(name.equals("containsAll") && sig.equals("(Ljava/util/Collection;)Z")) return true; if(name.equals("isEmpty") && sig.equals("()Z")) return true; if(name.equals("size") && sig.equals("()I")) return true; if(Types.isInstance(tr, "java/util/List")) { if (name.equals("get") && sig.equals("(I)Ljava/lang/Object;") || name.equals("subList") && sig.equals("(II)Ljava/util/List;")) return true; } return false; } if(Types.isInstance(tr, "java/util/Map")) { if ((name.equals("containsKey") || name.equals("containsValue")) && sig.equals("(Ljava/lang/Object;)Z")) return true; if (name.equals("get") && sig.equals("(Ljava/lang/Object;)Ljava/lang/Object;")) return true; } return Types.isSideEffectFreeType(tr); }