forked from TNG/ArchUnit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
analyze local variable instantiations
So far local variable instantiations were not analyzed at all, leaving gaps in the evaluation of dependency rules. If a local variable of a certain type was instantiated in a method, but no other use of that type was present (e.g. a method call on the type, or a field access), the dependency from the method to that type was undetected. Here, if enabled by a new configuration property (to preserve backward compatibility and performance), local variable instantiations add class dependencies from the surrounding method to the type of the variable and, if that type has generic type parameters, also from the method to any concrete generic type. Issue: TNG#768 Signed-off-by: Timo Thomas <work@timothomas.de>
- Loading branch information
Showing
5 changed files
with
286 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...va/com/tngtech/archunit/core/importer/ClassFileImporterLocalVariableDependenciesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.tngtech.archunit.core.importer; | ||
|
||
import com.tngtech.archunit.ArchConfiguration; | ||
import com.tngtech.archunit.core.domain.JavaClasses; | ||
import com.tngtech.archunit.core.domain.ReferencedClassObject; | ||
import com.tngtech.archunit.core.importer.testexamples.referencedclassobjects.ReferencingClassObjectsFromLocalVariable; | ||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.io.FilterInputStream; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.Stack; | ||
|
||
import static com.tngtech.archunit.testutil.Assertions.assertThatReferencedClassObjects; | ||
import static com.tngtech.archunit.testutil.assertion.ReferencedClassObjectsAssertion.referencedClassObject; | ||
|
||
@RunWith(DataProviderRunner.class) | ||
public class ClassFileImporterLocalVariableDependenciesTest { | ||
|
||
@Test | ||
public void imports_referenced_class_object_in_LocalVariable() { | ||
ArchConfiguration.get().setAnalyzeLocalVariableInstantiations(true); | ||
|
||
JavaClasses classes = new ClassFileImporter().importClasses(ReferencingClassObjectsFromLocalVariable.class); | ||
Set<ReferencedClassObject> referencedClassObjects = classes.get(ReferencingClassObjectsFromLocalVariable.class).getReferencedClassObjects(); | ||
|
||
assertThatReferencedClassObjects(referencedClassObjects).containReferencedClassObjects( | ||
referencedClassObject(FilterInputStream.class, 14), | ||
referencedClassObject(List.class, 15), | ||
referencedClassObject(Double.class, 15), | ||
referencedClassObject(FilterInputStream.class, 21), | ||
referencedClassObject(InputStream.class, 22), | ||
referencedClassObject(FilterInputStream.class, 26), | ||
referencedClassObject(InputStream.class, 27), | ||
referencedClassObject(FilterInputStream.class, 32), | ||
referencedClassObject(InputStream.class, 33), | ||
referencedClassObject(FilterInputStream.class, 40), | ||
referencedClassObject(InputStream.class, 41), | ||
referencedClassObject(Map.class, 43), | ||
referencedClassObject(FilterInputStream.class, 43), | ||
referencedClassObject(InputStream.class, 43), | ||
referencedClassObject(Map.class, 45), | ||
referencedClassObject(Set.class, 45), | ||
referencedClassObject(FilterInputStream.class, 45), | ||
referencedClassObject(InputStream.class, 45), | ||
referencedClassObject(Number.class, 53) | ||
); | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...mporter/testexamples/referencedclassobjects/ReferencingClassObjectsFromLocalVariable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.tngtech.archunit.core.importer.testexamples.referencedclassobjects; | ||
|
||
import java.io.FilterInputStream; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
@SuppressWarnings("unused") | ||
public class ReferencingClassObjectsFromLocalVariable<T extends Number> { | ||
|
||
static { | ||
FilterInputStream streamStatic = null; | ||
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection") List<Double> listStatic = new ArrayList<>(); | ||
//noinspection ResultOfMethodCallIgnored | ||
listStatic.size(); // if listStatic is not used it is optimized away. Surprisingly this is not the case for streamStatic above | ||
} | ||
|
||
void reference() { | ||
FilterInputStream stream = null; | ||
InputStream stream2 = null; | ||
System.out.println(stream); | ||
System.out.println(stream2); | ||
// after statement and comment | ||
FilterInputStream stream3 = null; | ||
InputStream stream4 = null; | ||
System.out.println(stream3); | ||
System.out.println(stream4); | ||
{ | ||
// in block | ||
FilterInputStream stream5 = null; | ||
InputStream stream6 = null; | ||
System.out.println(stream5); | ||
System.out.println(stream6); | ||
} | ||
} | ||
|
||
void referenceByGeneric() { | ||
List<FilterInputStream> list = null; | ||
List<InputStream> list2 = null; | ||
// multiple generic parameters | ||
Map<FilterInputStream, InputStream> map = null; | ||
// nested generic parameters | ||
Map<Set<FilterInputStream>, InputStream> map2 = null; | ||
System.out.println(list); | ||
System.out.println(list2); | ||
System.out.println(map); | ||
System.out.println(map2); | ||
} | ||
|
||
void referenceToOwnGenericType() { | ||
T myType = null; | ||
System.out.println(myType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters