Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to check Java Reflection Calls?

I posted a question last night about Java Reflection and am discovering compiler warnings this morning.

C:\javasandbox\reflection>javac ReflectionTest.java
Note: ReflectionTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\javasandbox\reflection>javac -Xlint:unchecked ReflectionTest.java
ReflectionTest.java:17: warning: [unchecked] unchecked call to
getDeclaredMethod(java.lang.String,java.lang.Class<?>...) as a member of the raw
type java.lang.Class

        myMethod = myTarget.getDeclaredMethod("getValue");
                                             ^
ReflectionTest.java:22: warning: [unchecked] unchecked call to
getDeclaredMethod(java.lang.String,java.lang.Class<?>...) as a member of the raw
type java.lang.Class

        myMethod = myTarget.getDeclaredMethod("setValue", params);
                                             ^
2 warnings

Is there a "proper" way to check these returned methods? (i.e. Is there a proper way to get rid of these warnings?)

Source code:

import java.lang.reflect.*;

class Target {
    String value;

    public Target() { this.value = new String("."); }
    public void setValue(String value) { this.value = value; }
    public String getValue() { return this.value; }
}

class ReflectionTest {
    public static void main(String args[]) {
        try {
            Class myTarget = Class.forName("Target");

            Method myMethod;
            myMethod = myTarget.getDeclaredMethod("getValue");
            System.out.println("Method Name: " + myMethod.toString());

            Class params[] = new Class[1];
            params[0] = String.class;
            myMethod = myTarget.getDeclaredMethod("setValue", params);
            System.out.println("Method Name: " + myMethod.toString());

        } catch (Exception e) {
            System.out.println("ERROR");
        }
    }
}
like image 235
Nate Avatar asked May 21 '09 12:05

Nate


People also ask

Is it safe to use reflection in Java?

Java Reflection is quite powerful and can be very useful. Java Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time.

How does Java reflection work?

Reflection is a feature in the Java programming language. It allows an executing Java program to examine or "introspect" upon itself, and manipulate internal properties of the program. For example, it's possible for a Java class to obtain the names of all its members and display them.

How is reflection implemented in Java?

The getConstructors() method is used to get the public constructors of the class to which an object belongs. The getMethods() method is used to get the public methods of the class to which an object belongs. We can invoke a method through reflection if we know its name and parameter types.


1 Answers

Change

Class myTarget = Class.forName("Target");

to

Class<?> myTarget = Class.forName("Target");

That basically means, "I know it's generic, but I know nothing about the type argument." They're semantically equivalent, but the compiler can tell the difference. For more information, see the relevant Java Generics FAQ entry ("What is the difference between the unbounded wildcard instantiation and the raw type?").

like image 184
Jon Skeet Avatar answered Oct 14 '22 19:10

Jon Skeet