Imagine you have a method like:
public void doGreatThings() throws CantDoGreatThingsException, RuntimeException {...}
Is there any way to programmatically get the declared thrown exceptions by way of reflection?
// It might return something like Exception[] thrownExceptions = [CantDoGreatThingsException.class, RuntimeException.class]
You can use getExceptionTypes() method. You will not get Exception[] since such array would expect exception instances, but you will get instead Class<?>[] which will hold all thrown exception .class.
Demo:
class Demo{
private void test() throws IOException, FileAlreadyExistsException{}
public static void main(java.lang.String[] args) throws Exception {
Method declaredMethod = Demo.class.getDeclaredMethod("test");
Class<?>[] exceptionTypes = declaredMethod.getExceptionTypes();
for (Class<?> exception: exceptionTypes){
System.out.println(exception);
}
}
}
Output:
class java.io.IOException
class java.nio.file.FileAlreadyExistsException
You can do that the reflection api.
// First resolve the method
Method method = MyClass.class.getMethod("doGreatThings");
// Retrieve the Exceptions from the method
System.out.println(Arrays.toString(method.getExceptionTypes()));
if the method requires parameters you need to supply them with the Class.getMethod() call.
Here's an example:
import java.io.IOException;
import java.util.Arrays;
public class Test {
public void test() throws RuntimeException, IOException {
}
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
System.out.println(Arrays.toString(Test.class.getDeclaredMethod("test").getExceptionTypes()));
}
}
Related
I'm learning java reflection. I am using the following code. But when I run, it gives the error
unreported exception ClassNotFoundException; must be caught or
declared to be thrown
Class className=Class.forName("First");
Maybe I'm going wrong somewhere. Please help me out. Here's the code:
import java.lang.reflect.Method;
public class First{
public void print(){}
public void ready(){}
}
public class test{
public static void main(String args[])
{
Class className=Class.forName("com.Test.First");
Method[] methods=className.getMethods();
System.out.println("First method is" + methods[0]);
}
}
All it's saying is that Class.forName throws this (non-runtime) Exception so you must handle it somehow. Here are two ways you could do it
public class test{
public static void main(String args[]) throws ClassNotFoundException
{
Class className=Class.forName("com.Test.First");
Method[] methods=className.getMethods();
System.out.println("First method is" + methods[0]);
}
}
Or
public class test{
public static void main(String args[])
{
try {
Class className=Class.forName("com.Test.First");
Method[] methods=className.getMethods();
System.out.println("First method is" + methods[0]);
}
catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
}
This line is the problem
Class className=Class.forName("com.Test.First");
in the Class.forName("com.Test.First"), you can replace com.Test.First with any gibberish and the compiler shouldn't care enough to validate it for you. All the compiler knows is that it is possible for there to not be a class com.Test.First and therefore you are responsible for handling a ClassNotFoundException.
I have this class:
public class SomeClass {
public void someMethod() {} throws someException
public class someException extends Exception { // Exception class
public someException(String message) {
super(message);
}
}
}
Another class:
public class SomeOtherClass {
public static void main (String[] args) {
SomeClass obj = new SomeClass();
try {
obj.someMethod();
} catch (someException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Eclipse complains that "someException cannot be resolved to a type". I tried to add
import SomeClass.someException
But then it says "The import SomeClass cannot be resolved"
You could of course put someException in a separate file, and not make it nested, is this the only way?
You should be able to use the class by qualifying it with the class name, SomeClass.someException. If you'd rather import it, you have to put your code in a package. You can then do:
import yourpkg.SomeClass.someException;
Also, you got the syntax a bit wrong here:
public void someMethod() {} throws someException
it should be
public void someMethod() throws someException {}
(But perhaps that was a typo in your question.)
You may also want to consider making the nested class static unless you really need to reference the enclosing object:
public static class someException extends Exception {
...
}
I have tracked down an error to line 362 of the java.lang.Class class:
Constructor<T> tmpConstructor = cachedConstructor;
The variable does not seem to get assigned. In the debug expression windows it only says "tmpConstructor cannot be resolved to a variable". The cachedConstructor is not null.
An error is only thrown further down when a the newInstance() function is called:
try {
return tmpConstructor.newInstance((Object[])null);
} catch (InvocationTargetException e) {
Unsafe.getUnsafe().throwException(e.getTargetException());
// Not reached
return null;
}
Context:
Using JSON Plugin with the Struts2 framework to create Java objects from the received JSON.
The field it is trying to parse is a subclass of an abstract class.
On further inspection (thanks to user902838) I was missing that it can't instantiate an abstract class. So I need to find out how it can instantiate subclasses, which is a different question.
Can someone please explain to me why the tmpconstructor is empty?
It's hard to tell without any information about the class you're trying to instantiate or the exception/error you observe, but my best guess would be that the class does not have a nullary constructor. This program exhibits such a problem:
package example;
public class NewInstanceTest {
public NewInstanceTest(String s) {
}
public static void main(String[] args) throws Exception {
Class.forName("example.NewInstanceTest").newInstance();
}
}
The problem can be fixed by adding a nullary constructor:
package example;
public class NewInstanceTest {
/* nullary constructor: */
public NewInstanceTest() {
this("default");
}
public NewInstanceTest(String s) {
}
public static void main(String[] args) throws Exception {
Class.forName("example.NewInstanceTest").newInstance();
}
}
or by removing all non-nullary constructors so that Java will provide a nullary one automatically:
package example;
public class NewInstanceTest {
public static void main(String[] args) throws Exception {
Class.forName("example.NewInstanceTest").newInstance();
}
}
I read this code where the interface throws an exception, but the class which implements it doesn't throw one or catch one, why is that? Is it legal or safe in java?
import java.rmi.*;
public interface MyRemote extends Remote {
public String sayHello() throws RemoteException;
}
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{
public String sayHello() {
return "Server says, 'Hey'";
}
public MyRemoteImpl() throws RemoteException {}
public static void main (String[] args) {
try {
MyRemote service = new MyRemoteImpl();
Naming.rebind("RemoteHello", service);
} catch(Exception ex)
{
ex.printStackTrace();
}
}
}
A general rule of implementing and extending is you can make your new class or interface "less restrictive" but not "more restrictive". If you think of the requirement to handle an exception as a restriction, an implementation that doesn't declare the exception is less restrictive. Anybody who codes to the interface will not have trouble with your class.
— Stan James
As part of the discussion at http://www.coderanch.com/t/399874/java/java/Methods-throwing-Exception-Interface
If a Java method overrides another in a parent class, or implements a method defined in an interface, it may not throw additional checked exceptions, but it may throw fewer.
public class A {
public void thrower() throws SQLException {...}
}
public class B extends A {
#Override
public void thrower() throws SQLException, RuntimeException, NamingException {...}
}
SQLException is fine; it's declared in the overridden method. It could even be replaced by a subclass like SerialException.
RuntimeException is fine; those can be used anywhere.
NamingException is illegal. It isn't a RuntimeException, and isn't in A's list, even as a subtype.
Great answer by #Chetter Hummin.
One way to look at this, and I find it easy to remember, is interface's implementations can be more specific but not more general.
For example in interface void test() throws Exception means "test may throw exception"
then implementation can be void test() means "test will not throw exception" (more specific)
or implementation can be void test() throws NullpointerException (more specific)
interface x {
void testException() throws Exception;
}
public class ExceptionTest implements x {
#Override
public void testException() { //this is fine
}
////// or
#Override
public void testException() throws NullPointerException { // this is fine
}
}
How can I invoke a method with parameters using reflection ?
I want to specify the values of those parameters.
Here's a simple example of invoking a method using reflection involving primitives.
import java.lang.reflect.*;
public class ReflectionExample {
public int test(int i) {
return i + 1;
}
public static void main(String args[]) throws Exception {
Method testMethod = ReflectionExample.class.getMethod("test", int.class);
int result = (Integer) testMethod.invoke(new ReflectionExample(), 100);
System.out.println(result); // 101
}
}
To be robust, you should catch and handle all checked reflection-related exceptions NoSuchMethodException, IllegalAccessException, InvocationTargetException.
To call a class method using reflection is very simple.
You need to create a class and generate method in it. like as follows.
package reflectionpackage;
public class My {
public My() {
}
public void myReflectionMethod() {
System.out.println("My Reflection Method called");
}
}
and call this method in another class using reflection.
package reflectionpackage;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionClass {
public static void main(String[] args)
throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class c=Class.forName("reflectionpackage.My");
Method m=c.getDeclaredMethod("myReflectionMethod");
Object t = c.newInstance();
Object o= m.invoke(t);
}
}
Find more details here.
You can use getClass in any Object to discover its class. Then you can use getMethods to discover all the available methods. Once you have the correct method you can call invoke with any number of parameters
this is the easiest way I know of, it needs to be surrounded with try & catch:
Method m = .class.getDeclaredMethod("", arg_1.class, arg_2.class, ... arg_n.class);
result = () m.invoke(null,(Object) arg_1, (Object) arg_2 ... (Object) arg_n);
this is for invoking a static method, if you want to invoke a non static method, you need to replace the first argument of m.invoke() from null to the object the underlying method is invoked from.
don't forget to add an import to java.lang.reflect.*;