Find a list parameter type

Report a typo

There is a class TestClass that has a field of List type. Unfortunately, we don't know a type of the List's parameter:

class TestClass {
    ...
    public List<UNKNOWN_TYPE> listField;
    ..
}

To obtain it, let's implement printParameterType method that accepts an instance of TestClass and the name of its field. The method should print a full name of the parameter type (package + class name).

Sample Input 1:

stringField

Sample Output 1:

java.lang.String
Write a program in Java 17
// Do not remove imports
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Scanner;

class ListParameterInspector {
// Do not change the method
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
String fieldName = scanner.next();

ListParameterInspector inspector = new ListParameterInspector();
inspector.printParameterType(new TestClass(), fieldName);
}

public void printParameterType(TestClass obj, String fieldName) throws Exception {
// Add implementation here
}
}
___

Create a free account to access the full topic