use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterTest method imports_enclosing_method_of_anonymous_class.
@Test
public void imports_enclosing_method_of_anonymous_class() throws ClassNotFoundException {
@SuppressWarnings("unused")
class ClassCreatingAnonymousClassInMethod {
void someMethod() {
new Serializable() {
};
}
}
String anonymousClassName = ClassCreatingAnonymousClassInMethod.class.getName() + "$1";
JavaClasses classes = new ClassFileImporter().importClasses(ClassCreatingAnonymousClassInMethod.class, Class.forName(anonymousClassName));
JavaClass enclosingClass = classes.get(ClassCreatingAnonymousClassInMethod.class);
JavaClass anonymousClass = classes.get(anonymousClassName);
assertThat(anonymousClass.getEnclosingCodeUnit()).contains(enclosingClass.getMethod("someMethod"));
assertThat(anonymousClass.getEnclosingClass()).contains(enclosingClass);
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterTest method reflect_works.
@Test
public void reflect_works() {
JavaClasses classes = new ClassFileImporter().importUrl(getClass().getResource("testexamples/innerclassimport"));
JavaClass calledClass = classes.get(CalledClass.class);
assertThat(calledClass.reflect()).isEqualTo(CalledClass.class);
assertThat(calledClass.getField("someString").reflect()).isEqualTo(field(CalledClass.class, "someString"));
assertThat(calledClass.getConstructor().reflect()).isEqualTo(constructor(CalledClass.class));
assertThat(calledClass.getConstructor(String.class).reflect()).isEqualTo(constructor(CalledClass.class, String.class));
assertThat(calledClass.getCodeUnitWithParameterTypes(CONSTRUCTOR_NAME, String.class).reflect()).isEqualTo(constructor(CalledClass.class, String.class));
JavaClass innerClass = classes.get(ClassWithInnerClass.Inner.class);
assertThat(innerClass.reflect()).isEqualTo(ClassWithInnerClass.Inner.class);
assertThat(innerClass.getMethod("call").reflect()).isEqualTo(method(ClassWithInnerClass.Inner.class, "call"));
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterTest method imports_enclosing_constructor_of_local_class.
@Test
public void imports_enclosing_constructor_of_local_class() throws ClassNotFoundException {
@SuppressWarnings("unused")
class ClassCreatingLocalClassInConstructor {
ClassCreatingLocalClassInConstructor() {
class SomeLocalClass {
}
}
}
String localClassName = ClassCreatingLocalClassInConstructor.class.getName() + "$1SomeLocalClass";
JavaClasses classes = new ClassFileImporter().importClasses(ClassCreatingLocalClassInConstructor.class, Class.forName(localClassName));
JavaClass enclosingClass = classes.get(ClassCreatingLocalClassInConstructor.class);
JavaClass localClass = classes.get(localClassName);
assertThat(localClass.getEnclosingCodeUnit()).contains(enclosingClass.getConstructor(getClass()));
assertThat(localClass.getEnclosingClass()).contains(enclosingClass);
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterTest method imports_no_enclosing_code_unit_of_anonymous_class_defined_outside_of_method.
@Test
public void imports_no_enclosing_code_unit_of_anonymous_class_defined_outside_of_method() throws ClassNotFoundException {
@SuppressWarnings("unused")
class ClassCreatingAnonymousClassInConstructor {
final Serializable field = new Serializable() {
};
}
String anonymousClassName = ClassCreatingAnonymousClassInConstructor.class.getName() + "$1";
JavaClasses classes = new ClassFileImporter().importClasses(ClassCreatingAnonymousClassInConstructor.class, Class.forName(anonymousClassName));
JavaClass enclosingClass = classes.get(ClassCreatingAnonymousClassInConstructor.class);
JavaClass anonymousClass = classes.get(anonymousClassName);
assertThat(anonymousClass.getEnclosingClass()).contains(enclosingClass);
assertThat(anonymousClass.getEnclosingCodeUnit()).isEmpty();
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileProcessorTest method ClassResolverFromClassPath_resolves_robustly.
@Test
public void ClassResolverFromClassPath_resolves_robustly() {
Optional<JavaClass> resolved = new ClassResolverFromClasspath().tryResolve("not.There");
assertThat(resolved).isEmpty();
}
Aggregations