This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Java reflection to get method name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java reflection to get method name" Watch "Java reflection to get method name" New topic
Author

Java reflection to get method name

Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
Hi,
here is my class

package com;



The method1() is not a part of the op. Please explain!!

Thanks
Faisal Syed

[See UseCodeTags]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18756
    
  40

Faisal syed wrote:Hi,
here is my class



The method1() is not a part of the op. Please explain!!


The Class.forName() method returns a Class instance for the class requested. Calling the getClass() method on that class object, will get you the Class instance for the Class class. And according to the JavaDoc, the java.lang.Class class doesn't have a method1() method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
When we speak of a singleton, I read in an article that an attacker may use the reflection API to gain unauthorised access to the members of your class. How can he gain access to the constructor < Singleton() >, or say any method not defined in the class Class?? Pleasae help!!!


Panagiotis Kalogeropoulos
Rancher

Joined: May 27, 2011
Posts: 99

Check the class AccessibleObject. You may find something useful there...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18756
    
  40

Faisal syed wrote:When we speak of a singleton, I read in an article that an attacker may use the reflection API to gain unauthorised access to the members of your class. How can he gain access to the constructor < Singleton() >, or say any method not defined in the class Class?? Pleasae help!!!






IMO, I think there are two distinct questions here...

For the question on how to access private components of the class, basically, the Field and Method classes have a setAccessible() method. If the method is called with true, the program will be able to access private fields and methods.

For the question on how to prevent access, these calls go through the security manager. And hence, not allowed for applets, RMI calls, etc. If you have a situation where you don't trust certain code, you need to configure your security manager appropriately.

Henry
 
wood burning stoves
 
subject: Java reflection to get method name