This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Running methods by reflection

 
Greenhorn
Posts: 29
IntelliJ IDE Python Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am writing program that is intended to run any method by reflection to check if I understand correctly chapter of book.
For now I am writing part that will run static methods, to focus on smaller parts.
This is how far I am:



The problem is that I get exception:
Exception in thread "main" java.lang.NoSuchMethodException: ch05.my_reflection.ReflectiveMethod.testMethod(java.lang.Integer, java.lang.Integer)

I guess autoboxing do not work here, but I am not sure. Also do you have  any idea how I could make it more functional? I want to be able to  run non-static methods and recieve return values if there is one
 
Bartender
Posts: 1200
38
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tough question, man
From the error you got, I think that autoboxing works, indeed your program tries to lookup for a method accepting two Integers, not two ints. I presume that the culprit is the signature of staticMethod runner, which accepts an array of Object as varargs, so that the two '2' are cast to Integer (2).
What happens if you change the signature of test method so that it will accept two Integers?
 
Saloon Keeper
Posts: 2760
359
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Claude Moore wrote:Tough question, man
From the error you got, I think that autoboxing works, indeed your program tries to lookup for a method accepting two Integers, not two ints. I presume that the culprit is the signature of staticMethod runner, which accepts an array of Object as varargs, so that the two '2' are cast to Integer (2).
What happens if you change the signature of test method so that it will accept two Integers?


I think you are right - the signatures don't match.

Change the signature of the called method to: testMethod(Integer a, Integer b)
    or
change the signature in getMethod to: getMethod(method_name, int.class, int.class)
 
Ron McLeod
Saloon Keeper
Posts: 2760
359
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Andrzej Zahorski wrote: Also do you have  any idea how I could make it more functional?


Maybe you could also use reflection to discover the methods and their associated parameters to build the appropriate signature.
 
Bartender
Posts: 2406
106
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that your utility method signature itself results in the variable getting autoboxed:
Also I am curious, why do you want to come up with such a method in the first place ?
 
Saloon Keeper
Posts: 21266
138
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's been a long time since I did this, and since I'm pretty sure that "int.class" isn't valid, I don't recall how I dealt with this, since some of the features being discussed only came in in Java 5, and that gives you an idea of how long I've gone since doing it. I'm thinking that I simply looked up the method by name and then compared each method in the set (remember, the same method can have multiple implementations) with a model signature string.

If you want a really good look at effective use of reflection, I suggest you pull the source for the Apache BeanUtils library. That's the component used by implementations of JEE Expression Language processors, and thus by EL-implementing sysems like JSPs, JavaServer Faces, and possibly even JPA. BeanUtils parses EL statements and renders them as calls to the referenced elements or their accessor methods (in the case of bean properties) using reflection. I think, in fact, there's a separate reflection library it uses to enhance the process, although that library's name escapes me at the moment.
 
Ron McLeod
Saloon Keeper
Posts: 2760
359
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an incomplete example, but demonstrates what I was thinking about when I said you could discover the method and their parameters:
    - iterate through the methods
    - filter out those where the name does not match
    - filter out those where the parameters do not match / are not compatible
    - invoke the method with the signature for the matching method

 
Master Rancher
Posts: 3399
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:and since I'm pretty sure that "int.class" isn't valid


It is valid - it's how we talk about parameter types and return types with reflection, allowing us to distinguish between int.class and Integer.class.  There's even a Void.class just to allow us to know that a method returns void.
 
Tim Holloway
Saloon Keeper
Posts: 21266
138
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And thus I am doubly corrected.

Although it's possible that the ability to use ".class" on primitive types was not part of Java last time I did that degree of introspection. It may, in fact, have come in about the same release as auto-boxing, which was somewhere around Java 5.

I'm showing my age...
 
Mike Simmons
Master Rancher
Posts: 3399
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think int.class and the like have been around since JDK 1.1, when Class.getMethod() and the Method class (with getReturnType()) first came out.  Because that's the point at which they had to decide how or whether to distinguish between an int and an Integer... and since it was Sun, whatever they decided then would have stuck with us for ages afterwards.  I don't think JLS first edition had any class literals, but here's a link to the second edition: http://titanium.cs.berkeley.edu/doc/java-langspec-2.0.pdf

15.8.2 Class Literals

A class literal is an expression consisting of the name of a class, interface, array,
or primitive type followed by a ‘.’ and the token class. The type of a class literal
is Class. It evaluates to the Class object for the named type (or for void) as
defined by the defining class loader of the class of the current instance



Seems like "or primitive type" was there early on...
 
Andrzej Zahorski
Greenhorn
Posts: 29
IntelliJ IDE Python Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Also I am curious, why do you want to come up with such a method in the first place ?



The only reason that I am making this is, because example in book I'm reading was not interesting for me (Core Java I by Cay Horstmann, Chapter 5). Also I found that making by myself is optimal way to imagine how features work.
This function is first idea that hit my head. Also maybe after extending it it might be useful for debugging purposes - I could pass for example collection of arguments, collection of functions and return collection of output from them. But I'm not really sure  -I am just  Java greenhorn (I think so).

So what would be remedy for not being able to use primitive types? The only thing that comes to my mind is to convert wrappers inside function to primitive types, but it feels like chore.


Also for answer with usage of streams - this is currently ahead of me. I want to use things, that I put into practice so my view of whole core functionalities in Java will be structured.
However it looks like nice hack indeed.
 
Andrzej Zahorski
Greenhorn
Posts: 29
IntelliJ IDE Python Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for grammar and missing words  - it is a bit late at my place. I'm trying to do one chapter every day, so sometimes it can consume a big chunk of time and energy.
 
Marshal
Posts: 66236
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It appears that one chapter per day might be too much and maybe you should go slower.
 
Sheriff
Posts: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:

Tim Holloway wrote:and since I'm pretty sure that "int.class" isn't valid


It is valid - it's how we talk about parameter types and return types with reflection, allowing us to distinguish between int.class and Integer.class.  There's even a Void.class just to allow us to know that a method returns void.


Void.class is not the same as void.class. The first is a representation of class Void, which only exists to deal with void in reflection, and has only one possible value - null. The second is the actual representation of void.

Regarding int.class, I'm pretty sure that used to be invalid because otherwise there wouldn't have been need for Integer.TYPE (which is the same as int.class).
 
Squanch that. And squanch this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!