What is the purpose of the following code? EXPLAIN HOW IT WORKS . typesMatch is
ID: 673720 • Letter: W
Question
What is the purpose of the following code?EXPLAIN HOW IT WORKS. typesMatch is a method that returns a Boolean if the types in the first parameter match the types in the second parameter.
public static Object whatDoIDo (String name, Object[] args)
{
try{
Class<?> givenClass = Class.forName(name);
Constructor<?>[] givenConstructors = givenClass.getConstructors();
for(int i = 0; i < givenConstructors.length; i++)
if(typesMatch(givenConstructors[i].getParameterTypes(), args)
return givenConstructors[i].newInstance(args);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
Explanation / Answer
ClassNotFoundException:
Answer :
Thrown when an application tries to load in a class through its string name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader.
The loadClass method in class ClassLoader.
but no definition for the class with the specified name could be found.
InstantiationException:
Answer :
public class InstantiationException
extends Exception
Thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated. The instantiation can fail for a
variety of reasons including but not limited to:
the class object represents an abstract class, an interface, an array class, a primitive type, or void
the class has no nullary constructor
IllegalAccessError:
Answer :
public class IllegalAccessError
extends IncompatibleClassChangeError
Thrown if an application attempts to access or modify a field, or to call a method that it
does not have access to.
Normally, this error is caught by the compiler; this error can only occur at run
time if the definition of a class has incompatibly changed.
IllegalArgumentException:
Answer:
Thrown to indicate that a method has been passed an illegal or inappropriate argument.
It seems like a defensive measure to complain about obviously bad input before the input
can get into the works and cause something to fail halfway through with a nonsensical
error message.
It's used for cases where it would be too annoying to throw a checked exception
(although it makes an appearance in the java.lang.reflect code, where concern
about ridiculous levels of checked-exception-throwing is not otherwise apparent).
I would use IllegalArgumentException to do last-ditch defensive argument-checking for
common utilities (trying to stay consistent with the jdk usage), where the expectation
is that a bad argument is a programmer error, similar to an NPE. I wouldn't use it to
implement validation in business code. I certainly wouldn't use it for the email example.
Thing is that, when it tries to invoke some method it throws InvocationTargetException
instead of some other expected exception (specifically ArrayIndexOutOfBoundsException).
As I actually know what method is invoked I went straight to this method code and added
a try-catch block for the line that suppose to throw ArrayIndexOutOfBoundsException and
it really threw ArrayIndexOutOfBoundsException as expected. Yet when going up it somehow
changes to InvocationTargetException and in the code above catch(Exception e) e is
InvocationTargetException and not ArrayIndexOutOfBoundsException as expected.
You've added an extra level of abstraction by calling the method with reflection.
The reflection layer wraps any exception in an InvocationTargetException,
which lets you tell the difference between an exception actually caused by a
failure in the reflection call (maybe your argument list wasn't valid, for example)
and a failure within the method called.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.