This commit is related to BAEL-6298 (#16323)
This commit aims to add three classes (Example, GenericExample, ReflectionExample).
This commit is contained in:
parent
1b0b87bfd2
commit
26f3664371
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.passclassasparameter;
|
||||||
|
|
||||||
|
public class Example {
|
||||||
|
public static void processClass(Class<?> clazz) {
|
||||||
|
System.out.println("Processing class: " + clazz.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
processClass(String.class);
|
||||||
|
processClass(Integer.class);
|
||||||
|
processClass(Double.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.passclassasparameter;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class GenericExample {
|
||||||
|
public static <T> void printListElements(Class<T> clazz, List<T> list) {
|
||||||
|
System.out.println("Elements of " + clazz.getSimpleName() + " list:");
|
||||||
|
for (T element : list) {
|
||||||
|
System.out.println(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
List<String> stringList = new ArrayList<>();
|
||||||
|
stringList.add("Java");
|
||||||
|
stringList.add("is");
|
||||||
|
stringList.add("awesome");
|
||||||
|
|
||||||
|
printListElements(String.class, stringList);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.passclassasparameter;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
public class ReflectionExample {
|
||||||
|
public static void processClass(Class<?> clazz, String methodName) throws Exception {
|
||||||
|
Method method = clazz.getMethod(methodName);
|
||||||
|
Object instance = clazz.getDeclaredConstructor().newInstance();
|
||||||
|
method.invoke(instance);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
processClass(ReflectionTarget.class, "sayHello");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ReflectionTarget {
|
||||||
|
public void sayHello() {
|
||||||
|
System.out.println("Hello, Reflection!");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user