Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Reflection: Retrieving Class instances

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, glad to meet the forum!

I need some help in which direction to move.

I learning now Reflection and I need to solve problem:

Implement a method that will find the class of the provided method by its name. This method accepts two arguments, the name of the method and an array of fully-qualified class names, where:
String methodName - is the name of the method that needs to be found;
String[] classNames - contains one class that has the method with the given name.
It should return the fully-qualified name of the class that has the method with the given name.
For example, the method name is abs and possible classes are java.lang.String, java.lang.StringBuffer and java.lang.Math.
String and StringBuffer have no method with the name abs. So they are not the class we are looking for.
Math class has a method with the name abs. The method should return the fully-qualified name of Math class, java.lang.Math in this case.

My solution:



The result is not what I want

Compilation error
Main.java:7: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
           for (Method m : Class.forName(s).getMethods()) {
                                        ^
Main.java:8: error: unreported exception ClassNotFoundException; must be caught or declared to be thrown
               if (m.getName().equals(methodName)) return Class.forName(s).getName();
                                                                       ^
2 errors
 
Marshal
Posts: 70718
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Who asked you to do that task; reflection isn't a technique for routine programming?
The Class.forName() method declares that it might throw those Exceptions; you must either handle them in this method or declare you are throwing them (throws XYZException). Aren't there methods to find a method directly from its name? Look on the Class class.
 
Campbell Ritchie
Marshal
Posts: 70718
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you get getting the Class<?> object with forName(), you already have its fully‑qualified name. So why do you want getName()? How do you know there is only one class with that method?
 
Master Rancher
Posts: 3704
44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Aren't there methods to find a method directly from its name? Look on the Class class.



Not with just the name, no.  There's a more direct method where you specify the method name and all the parameter types.  But if you don't know the parameter types and are trying to find a method using only its name, you need use getMethods(), I think.

I do agree with your other points.  Assuming there's a valid need for this method, it would probably be more useful to return a collection or array, not a single class name.
 
Campbell Ritchie
Marshal
Posts: 70718
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike Simmons wrote:. . . There's a more direct method where you specify the method name and all the parameter types. . . .

In the case of Math#abs(), which is overloaded several times, that isn't going to be easy to use.
There is a little program in (I think) Bruce Eckel's book Thinking in Java (2006 edition) which prints all the method names in a class matching addXxxListener(). That example can be useful, yes.
 
Campbell Ritchie
Marshal
Posts: 70718
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, with a little guesswork and a mistake and an exception, I managed to find abs() on JShell.The exception handling would make that method awkward to use for Kirill Shilov's purposes.
 
Saloon Keeper
Posts: 22678
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that for practical purposes, one would not restrict oneself to the introspection resources that come with the core JRE. Applications that have a serious need to find methods such as JavaServer Faces and Spring Framework almost always use Apache BeanUtils.
 
Kirill Shilov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh thank you for your responses!
I am now learnin Java by JetBrains online course and the task was one from Reflection topic. So it's not something real in some project, it's a task for 1 topic.

Thank you Campbell Ritchie for provided method, but I don't understand yet most of your code, sorry, just not enough knoledge yet

So I going again to IDE to try solve the task

added: and at this point I don't understand where would I need Reflection at all
 
Campbell Ritchie
Marshal
Posts: 70718
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kirill Shilov wrote:. . . Thank you Campbell Ritchie

That's a pleasure

. . . I don't understand yet most of your code . . .

I hope you aren't going to use Class.forName("java.lang.Math").newInstance(); that is one of the old ways to create an instance but it requires an accessible no‑arguments constructor. It will crash with some sort of exception because Math only has private constructors (I think) and as the JLS (=Java┬« Language Specification) will tell you, that precludes us from creating instances. You should no longer use newInstance().
There are three ways to get a Class<T> object:-
  • 1: Class.forName()
  • 2: ClassName.class, which is called a class literal. (JLS link.)
  • 3: The getClass() method. This is an instance method, so it can only be called on an object, and you can't create objects from the Math class. I suspect this technique is used more frequently than the other two: myObject.getClass().
  • So in the first line I declared clazz of type Class<Math>; I knew in advance what Class object I would require. That creates a Class (big C) object.I then tried to find the abs() method. I had to write the name of the method, and Class<T> objects for each parameter. You can try int.class, and the Integer class has a field called TYPE which represents the primitive int.I used var as its declared type because I was too lazy to import Method You can use var to mean, “automatically the same type as on the right of the = sign,” but you can't use var for fields. It is usually easiest to declare local variables as type var.

    As well as int.class, there is a class int[].class representing an array type.
     
    Mike Simmons
    Master Rancher
    Posts: 3704
    44
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Mike Simmons wrote:. . . There's a more direct method where you specify the method name and all the parameter types. . . .

    In the case of Math#abs(), which is overloaded several times, that isn't going to be easy to use.


    I'm not sure what's difficult to use about it.  The more relevant issue is in Kiril's case, he doesn't have any parameter types to include; he's looking for a more general match.

    Campbell Ritchie wrote:The exception handling would make that method awkward to use for Kirill Shilov's purposes.


    Which was the point of his original question, I believe.  
     
    Ranch Hand
    Posts: 536
    10
    Android Open BSD Slackware
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Reflection what a great feature! as far as I know is quite slow as counterside because the compiler cannot optimise all the operation
     
    Campbell Ritchie
    Marshal
    Posts: 70718
    288
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good to see you back, GM.
     
    30 seconds to difuse a loaf of bread ... here, use this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic