Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to reflect a java method statically?

Is there a way to statically reference a method for reflection in Java. Here's some example code to give you an idea of what I am attempting:

public void myFunc(int x) { ... }

public void other() {
    Method m1 = getClass().getMethod("myFunc");  // dynamic
    Method m2 = this.myFunc;                     // static
    Method m3 = MyClass.myFunc;                  // static (alternate)
}

I recognize that the above syntax does not work, but I was wondering if there is some sort of syntax similar to this that actually does work. I want a way to use reflection without worrying about the inherent dangers of referencing a method by a string.

Is there a way to do this, or is it just a pipe-dream?

like image 743
Viper Bailey Avatar asked Nov 25 '12 19:11

Viper Bailey


People also ask

Can a method be declared static?

The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class. The static keyword is used for a constant variable or a method that is the same for every instance of a class.

Can static methods return values?

A static method can also provide an array as a return value.

Can we override the static method?

Can we Override static methods in java? We can declare static methods with the same signature in the subclass, but it is not considered overriding as there won't be any run-time polymorphism. Hence the answer is 'No'.


1 Answers

Method references explains

this method to compare the birth dates of two Person instances already exists as Person.compareByAge. You can invoke this method instead in the body of the lambda expression:

Arrays.sort(rosterAsArray,
    (a, b) -> Person.compareByAge(a, b)
);

Because this lambda expression invokes an existing method, you can use a > method reference instead of a lambda expression:

Arrays.sort(rosterAsArray, Person::compareByAge);

and it goes on to explain the various kinds of method references:

There are four kinds of method references:

Reference to a static method      ContainingClass::staticMethodName

Reference to an instance method
of a particular object            containingObject::instanceMethodName

Reference to an instance method   ContainingType::methodName
of an arbitrary object of a
particular type

Reference to a constructor        ClassName::new

HISTORICAL NOTE (Written before Java 8 was finalized)

I think the Java closures proposal has something like this. Stephen Colebourne says:

Stefan and I are pleased to announce the release of v0.4 of the First-class Methods: Java-style closures proposal.

Changes

Since v0.3, we have tried to incorporate some of the feedback received on the various forums. The main changes are as follows:

1) Constructor and Field literals. It is now possible to create type-safe, compile-time changed instances of java.lang.reflect.Constructor and Field using FCM syntax:

// method literal:
Method m = Integer#valueOf(int);

// constructor literal:
Constructor<Integer> c = Integer#(int);

// field literal:
Field f = Integer#MAX_VALUE;

but I don't think this syntax is available in any shipping JVM. Closures themselves are definitely not in Java 7. You might see it in Java 8.

The Java closures site has a pointer to "Method references" which is a bit more up-to-date though it doesn't look like they've changed the syntax much.

like image 149
Mike Samuel Avatar answered Oct 21 '22 00:10

Mike Samuel