@Test public void testVisitFieldInsn() { AsmClass tagNameClass = asmClassProvider.getClass("tags/TagName"); AsmField nameField = tagNameClass.getField("name"); AsmMethod constructorWithString = tagNameClass.getMethod("<init>(Ljava/lang/String;)V"); assertThat(constructorWithString.getCallsToField()).containsOnly(nameField); }
@Test public void testVisitTryCatchBlock() { AsmClass sourceFileClass = asmClassProvider.getClass("tags/SourceFile"); AsmClass tagExceptionClass = asmClassProvider.getClass("tags/TagException"); AsmMethod readSourceFileMethod = sourceFileClass.getMethod("readSourceFile()V"); assertThat(readSourceFileMethod.getDistinctUsedAsmClasses()).contains(tagExceptionClass); }
@Test public void testVisitMethodInsn() { AsmClass sourceFileClass = asmClassProvider.getClass("tags/SourceFile"); AsmMethod readMethod = sourceFileClass.getMethod("read()V"); AsmMethod readSourceFileMethod = sourceFileClass.getMethod("readSourceFile()V"); assertThat(readSourceFileMethod.getCallsToMethod()).contains(readMethod).hasSize(2); assertThat(readSourceFileMethod.getCallsToField()).hasSize(1); }
@Test public void testVisitTypeInsn() { AsmClass sourceFileClass = asmClassProvider.getClass("tags/SourceFile"); AsmMethod constructor = sourceFileClass.getMethod("<init>()V"); assertThat( constructor .getDistinctUsedAsmClasses() .contains(asmClassProvider.getClass("java/util/ArrayList"))) .isNotNull(); }
AsmMethod getMethodOrCreateIt(String key) { AsmMethod method = getMethod(key); if (method != null) { return method; } method = new AsmMethod(this, key); method.setBodyLoaded(false); addMethod(method); return method; }
@Override public void visitFieldInsn(int opcode, String owner, String fieldName, String fieldDescription) { AsmClass targetClass = asmClassProvider.getClass(owner, DETAIL_LEVEL.NOTHING); AsmField targetField = targetClass.getFieldOrCreateIt(fieldName); method.addEdge(new AsmEdge(method, targetField, SourceCodeEdgeUsage.CALLS_FIELD, lineNumber)); emptyMethod = false; }
@Override public void visitTryCatchBlock(Label start, Label end, Label handler, String exception) { if (exception != null) { AsmClass exceptionClass = asmClassProvider.getClass(exception, DETAIL_LEVEL.NOTHING); method.addEdge(new AsmEdge(method, exceptionClass, SourceCodeEdgeUsage.USES, lineNumber)); } emptyMethod = false; }
@Override public void visitLdcInsn(Object cst) { if (cst instanceof Type) { Type type = (Type) cst; AsmClass usedClass = asmClassProvider.getClass(type.getInternalName(), DETAIL_LEVEL.NOTHING); method.addEdge(new AsmEdge(method, usedClass, SourceCodeEdgeUsage.USES, lineNumber)); } emptyMethod = false; }
private void computeAccessedField() { if (!isConstructor()) { for (AsmEdge edge : getOutgoingEdges()) { if (isCallToNonStaticInternalField(edge)) { if (isFieldAccesingDifferentField((AsmField) edge.getTo())) { accessedField = null; break; } accessedField = (AsmField) edge.getTo(); } else if (isCallToNonStaticInternalMethod(edge)) { AsmMethod method = (AsmMethod) edge.getTo(); if (isMethodNotAccessorOrAccessingDifferentField(method)) { accessedField = null; break; } accessedField = method.getAccessedField(); } } } }
@Override public void visitMethodInsn( int opcode, String owner, String methodName, String methodDescription) { if (isNotCallToJavaArrayMethod(owner)) { AsmClass targetClass = asmClassProvider.getClass(owner, DETAIL_LEVEL.STRUCTURE); AsmMethod targetMethod = targetClass.getMethodOrCreateIt(methodName + methodDescription); method.addEdge( new AsmEdge(method, targetMethod, SourceCodeEdgeUsage.CALLS_METHOD, lineNumber)); } emptyMethod = false; }
@Test public void testVisitMethod() { AsmClass asmClass = asmClassProvider.getClass("java/lang/String"); assertThat(asmClass.getMethod("charAt(I)C")).isNotNull(); assertThat(asmClass.getMethod("charAt(I)C").isPublic()).isTrue(); assertThat(asmClass.getMethod("charAt(I)C").isDeprecated()).isFalse(); assertThat(asmClass.getMethod("charAt(I)C").isStatic()).isFalse(); assertThat(asmClass.getMethod("valueOf(C)Ljava/lang/String;").isStatic()).isTrue(); asmClass = asmClassProvider.getClass("tags/File"); AsmMethod getLines = asmClass.getMethod("read(Ljava/util/Collection;Ljava/lang/String;)Ljava/lang/String;"); assertThat(getLines.getDistinctUsedAsmClasses().contains(new AsmClass("java/util/Collection"))) .isTrue(); assertThat(getLines.getDistinctUsedAsmClasses().contains(new AsmClass("tags/File"))).isTrue(); assertThat(getLines.getDistinctUsedAsmClasses().contains(new AsmClass("java/lang/String"))) .isTrue(); assertThat( getLines .getDistinctUsedAsmClasses() .contains(new AsmClass("java/lang/RuntimeException"))) .isTrue(); }
private boolean isMethodNotAccessorOrAccessingDifferentField(AsmMethod method) { return !method.isAccessor() || (accessedField != null && !accessedField.equals(method.getAccessedField())); }
@Override public void visitEnd() { method.setEmpty(emptyMethod); }
@Override public void visitTypeInsn(int opcode, String internalName) { AsmClass usedClass = asmClassProvider.getClass(internalName, DETAIL_LEVEL.NOTHING); method.addEdge(new AsmEdge(method, usedClass, SourceCodeEdgeUsage.USES, lineNumber)); emptyMethod = false; }
void addMethod(AsmMethod asmMethod) { methods.put(asmMethod.getKey(), asmMethod); }