Skip to content

Commit e248f78

Browse files
committed
clean up field- & return-type
1 parent ee6d3d5 commit e248f78

File tree

1 file changed

+110
-43
lines changed

1 file changed

+110
-43
lines changed

src/test/java/assignment/testing/framework/Utilities.java

Lines changed: 110 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,11 @@ static public void testClass(String fullyQualifiedClassName, Runnable fn) {
9393
var classObject = Utilities.findClass(fullyQualifiedClassName);
9494

9595
if (classObject.isEmpty()) {
96-
throw new AssertionFailedError(
97-
"Class not found: %s".formatted(fullyQualifiedClassName),
98-
"Class located at src/main/java/%s".formatted(fullyQualifiedClassName.replaceAll("\\.", "/")),
99-
"Class not found"
100-
);
96+
Utilities.throwClassNotFound(fullyQualifiedClassName);
97+
}
98+
else {
99+
Utilities.testClass(classObject.get(), fn);
101100
}
102-
103-
Utilities.testClass(classObject.get(), fn);
104101
}
105102

106103
static public void testClass(Class<?> classObject, Runnable fn) {
@@ -261,7 +258,7 @@ static public Optional<Method> findDeclaredMethod(
261258
/** Scoped CLASS */
262259
static public void testMethod(String methodName, List<Class<?>> parameterTypes, Runnable fn) {
263260
Utilities.findMethod(CLASS.get(), methodName, parameterTypes.toArray(new Class[0])).ifPresent(
264-
method -> Utilities.testMethod(method, fn)
261+
method -> Utilities.testMethod(method, fn)
265262
);
266263
}
267264

@@ -295,25 +292,15 @@ static public void testClassMethod(
295292
var classOptional = Utilities.findClass(fullyQualifiedClassName);
296293

297294
if (classOptional.isEmpty()) {
298-
throw new AssertionFailedError(
299-
"Class not found: %s".formatted(fullyQualifiedClassName),
300-
"Class located at src/main/java/%s.java".formatted(fullyQualifiedClassName.replaceAll("\\.", "/")),
301-
"Class not found"
302-
);
295+
Utilities.throwClassNotFound(fullyQualifiedClassName);
303296
}
304297

305298
where(Utilities.CLASS, classOptional.get()).run(() -> {
306299
var parameters = parameterTypes.toArray(new Class[0]);
307300
var methodOptional = Utilities.findMethod(CLASS.get(), methodName, parameters);
308301

309302
if (methodOptional.isEmpty()) {
310-
throw new AssertionFailedError(
311-
"Class-method not found: %s.%s(%s)".formatted(
312-
fullyQualifiedClassName, methodName, parameterTypes.isEmpty() ? "" : Arrays.toString(parameters)
313-
),
314-
"Method to exist within class",
315-
"Method within class does not exist"
316-
);
303+
Utilities.throwClassMethodNotFound(fullyQualifiedClassName, methodName, parameters);
317304
}
318305

319306
where(Utilities.METHOD, methodOptional.get()).run(fn);
@@ -360,6 +347,43 @@ static public boolean methodReturns(Class<?> returnType) {
360347
}
361348

362349

350+
static public String methodReturnType(
351+
String pkg, String className,
352+
String methodName, Class<?>... parameterTypes
353+
) {
354+
return Utilities.methodReturnType(FQCN(pkg, className), methodName, parameterTypes);
355+
}
356+
357+
static public String methodReturnType(
358+
String fullyQualifiedClassName,
359+
String methodName, Class<?>... parameterTypes
360+
) {
361+
var classObject = Utilities.findClass(fullyQualifiedClassName);
362+
363+
if (classObject.isEmpty()) {
364+
Utilities.throwClassNotFound(fullyQualifiedClassName);
365+
}
366+
367+
return Utilities.methodReturnType(classObject.get(), methodName, parameterTypes);
368+
}
369+
370+
static public String methodReturnType(
371+
Class<?> classObject, String methodName, Class<?>... parameterTypes
372+
) {
373+
var methodObject = Utilities.findMethod(classObject, methodName, parameterTypes);
374+
375+
if (methodObject.isEmpty()) {
376+
Utilities.throwClassMethodNotFound(classObject.getName(), methodName, parameterTypes);
377+
}
378+
379+
return Utilities.methodReturnType(methodObject.get());
380+
}
381+
382+
static public String methodReturnType(Method methodObject) {
383+
return Utilities.getTypeName(methodObject.getGenericReturnType());
384+
}
385+
386+
363387
/** Scoped METHOD */
364388
static public boolean methodIsPublic() {
365389
return (METHOD.get().getModifiers() & Modifier.PUBLIC) != 0;
@@ -502,24 +526,14 @@ static public void testClassField(String fullyQualifiedClassName, String fieldNa
502526
var classObject = Utilities.findClass(fullyQualifiedClassName);
503527

504528
if (classObject.isEmpty()) {
505-
throw new AssertionFailedError(
506-
"Class not found: %s".formatted(fullyQualifiedClassName),
507-
"Class located at src/main/java/%s.java".formatted(fullyQualifiedClassName.replaceAll("\\.", "/")),
508-
"Class not found"
509-
);
529+
Utilities.throwClassNotFound(fullyQualifiedClassName);
510530
}
511531

512532
where(Utilities.CLASS, classObject.get()).run(() -> {
513533
var fieldObject = Utilities.findField(CLASS.get(), fieldName);
514534

515535
if (fieldObject.isEmpty()) {
516-
throw new AssertionFailedError(
517-
"Class-field not found: %s#%s".formatted(
518-
fullyQualifiedClassName, fieldName
519-
),
520-
"Field to exist within class",
521-
"Field within class does not exist"
522-
);
536+
Utilities.throwClassFieldNotFound(fullyQualifiedClassName, fieldName);
523537
}
524538

525539
where(Utilities.FIELD, fieldObject.get()).run(fn);
@@ -585,17 +599,40 @@ static public boolean fieldHasModifiers(Field fieldObject, AccessFlag... flags)
585599
}
586600

587601

588-
public String fieldType(Type type) {
602+
/** Scoped FÌELD */
603+
static public String fieldType() {
604+
return Utilities.fieldType(FIELD.get().getGenericType());
605+
}
606+
607+
static public String fieldType(Type type) {
589608
if (type instanceof ParameterizedType parameterizedType) {
590-
return fieldParameterizedType(parameterizedType);
609+
return Utilities.fieldParameterizedType(parameterizedType);
591610
}
592611
else {
593-
return stripPackageFromClassName(type.getTypeName());
612+
return Utilities.stripPackageFromClassName(type.getTypeName());
594613
}
595614
}
596615

597616

598-
public String fieldParameterizedType(ParameterizedType type) {
617+
static public String fieldParameterizedType(ParameterizedType type) {
618+
return Utilities.getParameterizedTypeName(type);
619+
}
620+
621+
622+
623+
///-----------------------------------------------------------------------------------------------------------------
624+
///# Section: Helper-methods
625+
///-----------------------------------------------------------------------------------------------------------------
626+
static public String getTypeName(Type type) {
627+
if (type instanceof ParameterizedType parameterizedType) {
628+
return Utilities.getParameterizedTypeName(parameterizedType);
629+
}
630+
else {
631+
return Utilities.stripPackageFromClassName(type.getTypeName());
632+
}
633+
}
634+
635+
static public String getParameterizedTypeName(ParameterizedType type) {
599636
StringBuilder output = new StringBuilder();
600637

601638
var typeArguments = type.getActualTypeArguments();
@@ -607,13 +644,13 @@ public String fieldParameterizedType(ParameterizedType type) {
607644

608645
for (var typeArgument : typeArguments) {
609646
if (typeArgument instanceof Class) {
610-
types.add(stripPackageFromClassName(((Class<?>) typeArgument).getName()));
647+
types.add(Utilities.stripPackageFromClassName(((Class<?>) typeArgument).getName()));
611648
}
612649
else if (typeArgument instanceof ParameterizedType parameterizedType) {
613-
types.add(fieldParameterizedType(parameterizedType));
650+
types.add(Utilities.getParameterizedTypeName(parameterizedType));
614651
}
615652
else {
616-
types.add(stripPackageFromClassName(typeArgument.toString()));
653+
types.add(Utilities.stripPackageFromClassName(typeArgument.toString()));
617654
}
618655
}
619656

@@ -625,10 +662,6 @@ else if (typeArgument instanceof ParameterizedType parameterizedType) {
625662
}
626663

627664

628-
629-
///-----------------------------------------------------------------------------------------------------------------
630-
///# Section: Helper-methods
631-
///-----------------------------------------------------------------------------------------------------------------
632665
static public String stripPackageFromClassName(String fullyQualifiedClassName) {
633666
return List.of(fullyQualifiedClassName.split("\\.")).getLast();
634667
}
@@ -645,6 +678,40 @@ static public void assertStandardOutputEquals(String input) {
645678
}
646679

647680

681+
static private void throwClassNotFound(String fullyQualifiedClassName) {
682+
throw new AssertionFailedError(
683+
"Class not found: %s".formatted(fullyQualifiedClassName),
684+
"Class located at src/main/java/%s.java".formatted(fullyQualifiedClassName.replaceAll("\\.", "/")),
685+
"Class not found"
686+
);
687+
}
688+
689+
690+
static private void throwClassMethodNotFound(
691+
String fullyQualifiedClassName,
692+
String methodName, Class<?>... parameterTypes
693+
) {
694+
throw new AssertionFailedError(
695+
"Class-method not found: %s.%s(%s)".formatted(
696+
fullyQualifiedClassName, methodName, parameterTypes.length == 0 ? "" : Arrays.toString(parameterTypes)
697+
),
698+
"Method to exist within class",
699+
"Method within class does not exist"
700+
);
701+
}
702+
703+
704+
static private void throwClassFieldNotFound(String fullyQualifiedClassName, String fieldName) {
705+
throw new AssertionFailedError(
706+
"Class-field not found: %s#%s".formatted(
707+
fullyQualifiedClassName, fieldName
708+
),
709+
"Field to exist within class",
710+
"Field within class does not exist"
711+
);
712+
}
713+
714+
648715
///-----------------------------------------------------------------------------------------------------------------
649716
///# Note: Private constructor; prevent instantiation of this class as it strictly contains static helper methods
650717
private Utilities() {}

0 commit comments

Comments
 (0)