How can we get methods name and costructors name from java class before compilation?Ple ase any body have any idea plaese share with me
methods and construtors
Collapse
X
-
Tags: None
-
[font=Verdana][size=2]question not clear at all.What are you trying to do please explain in details.[/size][/font]
Originally posted by pattanaikhrHow can we get methods name and costructors name from java class before compilation?Ple ase any body have any idea plaese share with me -
Hi ,
I have one java class like test.java
public class test{
boolean packFrame = false;
private int a,b;
public test() {
System.out.prin tln("Hi");
}
public static final void test45 (int i) {
int a=0,b=1;
if(a==b){
System.out.prin tln("test");
}
System.out.prin tln("Hi");
}
public static int test123 (int i,String s,int k) {
System.out.prin tln("Hi");
return 5;
}
public void vee(int i){
try{
i +=1;
sum( a, b);
}
catch(Exception e){
}
}
public void sum(int a,int b){
try {
int sumab=0;
sumab= a+b;
} catch (RuntimeExcepti on e) {
e.printStackTra ce();
}
}
I want this methods name and constructors name before compile this java class..
Originally posted by r035198xPlease expalin what you mean by "before compilation".Comment
-
output like
constructor :
test()
methods name : test45,test123, sum
Originally posted by pattanaikhrHi ,
I have one java class like test.java
public class test{
boolean packFrame = false;
private int a,b;
public test() {
System.out.prin tln("Hi");
}
public static final void test45 (int i) {
int a=0,b=1;
if(a==b){
System.out.prin tln("test");
}
System.out.prin tln("Hi");
}
public static int test123 (int i,String s,int k) {
System.out.prin tln("Hi");
return 5;
}
public void vee(int i){
try{
i +=1;
sum( a, b);
}
catch(Exception e){
}
}
public void sum(int a,int b){
try {
int sumab=0;
sumab= a+b;
} catch (RuntimeExcepti on e) {
e.printStackTra ce();
}
}
I want this methods name and constructors name before compile this java class..Comment
-
you are confusing us.
which method name do you want?
And where is the " main " method in your code?
where from will the code start to compile or run?
is this your first java program?just go through some examples of writing a java program first.
Originally posted by pattanaikhrHi ,
I have one java class like test.java
public class test{
boolean packFrame = false;
private int a,b;
public test() {
System.out.prin tln("Hi");
}
public static final void test45 (int i) {
int a=0,b=1;
if(a==b){
System.out.prin tln("test");
}
System.out.prin tln("Hi");
}
public static int test123 (int i,String s,int k) {
System.out.prin tln("Hi");
return 5;
}
public void vee(int i){
try{
i +=1;
sum( a, b);
}
catch(Exception e){
}
}
public void sum(int a,int b){
try {
int sumab=0;
sumab= a+b;
} catch (RuntimeExcepti on e) {
e.printStackTra ce();
}
}
I want this methods name and constructors name before compile this java class..Comment
-
suppose this test.java class has save in different location , another one xyz.java , which can read test. java , and when we will compile xyz.java , that time it'll give test.java methods name and constructors name like test,test45,tes t123,sum.
Originally posted by hirak1984you are confusing us.
which method name do you want?
And where is the " main " method in your code?
where from will the code start to compile or run?
is this your first java program?just go through some examples of writing a java program first.Comment
-
I know that through reflection it's possible , but this case we need compile first,
without compile the java class how can we get method name and constructor name?
Originally posted by r035198xI'm not sure what you are trying to do here but you can have a look at the reflection API and see if that's what you want.Comment
-
Originally posted by pattanaikhrI know that through reflection it's possible , but this case we need compile first,
without compile the java class how can we get method name and constructor name?Comment
-
Ya I too cant find any other way.But why do you need to get the names before compilation?Not clear!Originally posted by r035198xWell you should realize then that the jvm won't have anything to do with it until you have created a class file. So you have to manipulate it as you would a text file. You'd have to open it using FileReader or Scanner and try to extract the function names. The constructors are easy to get but the method names require a bit more analysis.Comment
-
Yes that is right , but if any method is available in java API which is providing get method and constructors name before compile.Because if we'll think about ur suggestion for generic, then it's too difficult , the different programmer have different coding style. So getting method name is also difficult
You told that constructor is get too easy but I'm giving one example here
Suppose Test.java is class
public class Test{
test(){ --------------->constructor
System.out.prin tln("Hi");
}
public void test(){ --------> Method
System.out.prin tln("Hi");
}
Then how can we will differentiate here?
Originally posted by r035198xWell you should realize then that the jvm won't have anything to do with it until you have created a class file. So you have to manipulate it as you would a text file. You'd have to open it using FileReader or Scanner and try to extract the function names. The constructors are easy to get but the method names require a bit more analysis.Comment
-
Because my requirement like that
Originally posted by hirak1984Ya I too cant find any other way.But why do you need to get the names before compilation?Not clear!Comment
-
Originally posted by pattanaikhrYes that is right , but if any method is available in java API which is providing get method and constructors name before compile.Because if we'll think about ur suggestion for generic, then it's too difficult , the different programmer have different coding style. So getting method name is also difficult
You told that constructor is get too easy but I'm giving one example here
Suppose Test.java is class
public class Test{
test(){ --------------->constructor
System.out.prin tln("Hi");
}
public void test(){ --------> Method
System.out.prin tln("Hi");
}
Then how can we will differentiate here?
public class Test{
test(){ --------------->constructor
Wrong!
test() is not a constructor. Java is case sensitive.
To find the constructor you have to find where the name exactly matches the name of the class and then you would have to see what modifier was used for it e.g
Code:public class Test{ Test(){ --------------->constructor } void Test() {//-------->not a constructor }
Comment
-
Yes that is right java is case sensitive ,
ok Test(){---------> constructor
}
public void Test(){-------->method.
}
void Test(){------>
}
then how can we differentiate method and constructor
Originally posted by r035198xpublic class Test{
test(){ --------------->constructor
Wrong!
test() is not a constructor. Java is case sensitive.
To find the constructor you have to find where the name exactly matches the name of the class and then you would have to see what modifier was used for it e.g
Code:public class Test{ Test(){ --------------->constructor } void Test() {//-------->not a constructor }
Comment
Comment