wongww
asked on
read java file
i`m trying to write a program that able to read a java file
and output a result to tell how many methods in the particular file.
for example:
public class Hello
{
int x;
Hello()
{
}
public static void main(String args[ ])
{
}
public void A( )
{
}
public void B()
{
}
}
output: 4 methods
-Hello
-main
-A
-B
can anyone please tell me how to to do?
and output a result to tell how many methods in the particular file.
for example:
public class Hello
{
int x;
Hello()
{
}
public static void main(String args[ ])
{
}
public void A( )
{
}
public void B()
{
}
}
output: 4 methods
-Hello
-main
-A
-B
can anyone please tell me how to to do?
import java.lang.reflect.*;
class GetMethods {
public static void main(String args[]){
try {
Hello obj = new Hello();
Class hello = obj.getClass();
String cname = hello.getName();
System.out.println("Class Name="+cname);
Constructor constructors[] = hello.getConstructors();
System.out.println("Constr uctors="+c onstructor s.length);
for ( int i=0; i < constructors.length; i++ ) {
String cons = constructors[i].toString() ;
// if ( cons.indexOf(cname) > 0)
System.out.println("Constr uctor "+(i+1)+"="+cons);
}
Method methods[] = hello.getMethods();
for ( int i=0; i < methods.length; i++ ) {
String method = methods[i].toString();
if ( method.indexOf(cname) > 0)
System.out.println("Method "+(i+1)+"="+method);
}
}
catch ( Exception ee){
ee.printStackTrace();
}
}
}
class GetMethods {
public static void main(String args[]){
try {
Hello obj = new Hello();
Class hello = obj.getClass();
String cname = hello.getName();
System.out.println("Class Name="+cname);
Constructor constructors[] = hello.getConstructors();
System.out.println("Constr
for ( int i=0; i < constructors.length; i++ ) {
String cons = constructors[i].toString()
// if ( cons.indexOf(cname) > 0)
System.out.println("Constr
}
Method methods[] = hello.getMethods();
for ( int i=0; i < methods.length; i++ ) {
String method = methods[i].toString();
if ( method.indexOf(cname) > 0)
System.out.println("Method
}
}
catch ( Exception ee){
ee.printStackTrace();
}
}
}
Read documentation for
java.lang.Class and java.lang.reflect package
Ok.
From the output it is sure that
Java Interpreter is not treating
default constructor ( Hello () ) as a constructor.
That's why You got zero constructors
From the output it is sure that
Java Interpreter is not treating
default constructor ( Hello () ) as a constructor.
That's why You got zero constructors
ravindra76 changed the proposed answer to a comment
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i have one problem!!My program have to open ANY type of standard .java file to read it using FileDialog.
In ur program u have declare an object for the particular class that u wanted to read.How can I do that in my case??
In ur program u have declare an object for the particular class that u wanted to read.How can I do that in my case??
import java.lang.reflect.*;
import java.lang.*;
class GetMethods {
public static void main(String args[]){
try {
Class hello = Class.forName(args[0]);
String cname = hello.getName();
System.out.println("Class Name="+cname);
Constructor constructors[] = hello.getConstructors();
System.out.println("Constr uctors="+c onstructor s.length);
for ( int i=0; i < constructors.length; i++ ) {
String cons = constructors[i].toString() ;
// if ( cons.indexOf(cname) > 0)
System.out.println("Constr uctor "+(i+1)+"="+ constructors[i].getName()) ;
}
Method methods[] = hello.getMethods();
for ( int i=0; i < methods.length; i++ ) {
String method = methods[i].toString();
if ( method.indexOf(cname) > 0)
System.out.println("Method "+(i+1)+"="+methods[i].get Name());
}
}
catch ( Exception ee){
ee.printStackTrace();
}
}
}
/*
java.io.File has getName() function.
Get the file name and substitute it in the palce of args[0]
That's all
:)*/
import java.lang.*;
class GetMethods {
public static void main(String args[]){
try {
Class hello = Class.forName(args[0]);
String cname = hello.getName();
System.out.println("Class Name="+cname);
Constructor constructors[] = hello.getConstructors();
System.out.println("Constr
for ( int i=0; i < constructors.length; i++ ) {
String cons = constructors[i].toString()
// if ( cons.indexOf(cname) > 0)
System.out.println("Constr
}
Method methods[] = hello.getMethods();
for ( int i=0; i < methods.length; i++ ) {
String method = methods[i].toString();
if ( method.indexOf(cname) > 0)
System.out.println("Method
}
}
catch ( Exception ee){
ee.printStackTrace();
}
}
}
/*
java.io.File has getName() function.
Get the file name and substitute it in the palce of args[0]
That's all
:)*/
ASKER
I have follow ur code.But it is not working!!Got run time error. Can u please tell me why??
Heres my code:
import java.lang.reflect.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
public class Display extends Frame implements ActionListener
{
int num_arr;
int num_array;
String filename;
public Display() //constructor
{
super();
setSize(400,300);
setTitle("");
addWindowListener(new WindowDestroyer());
Menu mb = new Menu("File");
MenuItem m;
m = new MenuItem("Open");
m.addActionListener(this);
mb.add(m);
m = new MenuItem("Close");
m.addActionListener(this);
mb.add(m);
m = new MenuItem("Exit");
m.addActionListener(this);
mb.add(m);
MenuBar mBar = new MenuBar();
mBar.add(mb);
setMenuBar(mBar);
}
public void actionPerformed(ActionEven t e)
{
try
{
String actionCommand = e.getActionCommand();
Dialog dl;
Label l;
if(actionCommand.equals("O pen"))
{
FileDialog fd=new FileDialog(f,"Open Image file",FileDialog.LOAD);
fd.setVisible(true);
String fname=fd.getFile();
String fdir=fd.getDirectory();
String fn=fd.getName();
if((fname!=null && fname.indexOf(".JAVA")>0) || (fname!=null && fname.indexOf(".java")>0))
{
try{
Class hello = Class.forName(fn);
String cname = hello.getName();
System.out.println("Class Name="+cname);
}
catch ( Exception ee){
ee.printStackTrace();
}
}
} //end if
else if(actionCommand.equals("C lose"))
{
} //end if
else if(actionCommand.equals("E xit"))
{
System.exit(0);
} // end if
} catch (NullPointerException e1) {}
}
public static void main(String[] args)
{
f=new Display();
f.setVisible(true);
f.setTitle("DISPLAY");
}//end main
static Display f = new Display();
Panel center=new Panel();
}//end class
Heres my code:
import java.lang.reflect.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
public class Display extends Frame implements ActionListener
{
int num_arr;
int num_array;
String filename;
public Display() //constructor
{
super();
setSize(400,300);
setTitle("");
addWindowListener(new WindowDestroyer());
Menu mb = new Menu("File");
MenuItem m;
m = new MenuItem("Open");
m.addActionListener(this);
mb.add(m);
m = new MenuItem("Close");
m.addActionListener(this);
mb.add(m);
m = new MenuItem("Exit");
m.addActionListener(this);
mb.add(m);
MenuBar mBar = new MenuBar();
mBar.add(mb);
setMenuBar(mBar);
}
public void actionPerformed(ActionEven
{
try
{
String actionCommand = e.getActionCommand();
Dialog dl;
Label l;
if(actionCommand.equals("O
{
FileDialog fd=new FileDialog(f,"Open Image file",FileDialog.LOAD);
fd.setVisible(true);
String fname=fd.getFile();
String fdir=fd.getDirectory();
String fn=fd.getName();
if((fname!=null && fname.indexOf(".JAVA")>0) || (fname!=null && fname.indexOf(".java")>0))
{
try{
Class hello = Class.forName(fn);
String cname = hello.getName();
System.out.println("Class Name="+cname);
}
catch ( Exception ee){
ee.printStackTrace();
}
}
} //end if
else if(actionCommand.equals("C
{
} //end if
else if(actionCommand.equals("E
{
System.exit(0);
} // end if
} catch (NullPointerException e1) {}
}
public static void main(String[] args)
{
f=new Display();
f.setVisible(true);
f.setTitle("DISPLAY");
}//end main
static Display f = new Display();
Panel center=new Panel();
}//end class
The correct version:_
import java.lang.reflect.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
public class Display extends Frame implements ActionListener
{
int num_arr;
int num_array;
String filename;
public Display() //constructor
{
super();
setSize(400,300);
setTitle("");
//addWindowListener(new WindowDestroyer());
Menu mb = new Menu("File");
MenuItem m;
m = new MenuItem("Open");
m.addActionListener(this);
mb.add(m);
m = new MenuItem("Close");
m.addActionListener(this);
mb.add(m);
m = new MenuItem("Exit");
m.addActionListener(this);
mb.add(m);
MenuBar mBar = new MenuBar();
mBar.add(mb);
setMenuBar(mBar);
}
public void actionPerformed(ActionEven t e)
{
try
{
String actionCommand = e.getActionCommand();
Dialog dl;
Label l;
if(actionCommand.equals("O pen"))
{
FileDialog fd=new FileDialog(f,"Open Image file",FileDialog.LOAD);
fd.setVisible(true);
String fname=fd.getFile();
fname = fname.substring(0,fname.le ngth()-5);
System.out.println("File name="+fname);
if(fname!=null )
{
try{
Class hello = Class.forName(fname);
String cname = hello.getName();
System.out.println("Class Name="+cname);
}
catch ( Exception ee){
ee.printStackTrace();
}
}
} //end if
else if(actionCommand.equals("C lose"))
{
} //end if
else if(actionCommand.equals("E xit"))
{
System.exit(0);
} // end if
} catch (NullPointerException e1) {}
}
public static void main(String[] args)
{
f=new Display();
f.setVisible(true);
f.setTitle("DISPLAY");
}//end main
static Display f = new Display();
Panel center=new Panel();
}//end class
/* IMPORTANT:-The file You are selecting must be compiled before U run this program.
Because we are loading the Class object
which will execute when THe .java file is
compiled...SO be sure that , the file u are selecting from FileDialog must be compiled
and .class file should be present in same directory.:)
In the code , You selected .java file from Dialog. But be sure that .class should be present
for that .java file.
If You select .class file directly in filedialog,
Replace
fname = fname.substring(0,fname.le ngth()-5);
with
fname = fname.substring(0,fname.le ngth()-6);
*/
:)
import java.lang.reflect.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.util.*;
public class Display extends Frame implements ActionListener
{
int num_arr;
int num_array;
String filename;
public Display() //constructor
{
super();
setSize(400,300);
setTitle("");
//addWindowListener(new WindowDestroyer());
Menu mb = new Menu("File");
MenuItem m;
m = new MenuItem("Open");
m.addActionListener(this);
mb.add(m);
m = new MenuItem("Close");
m.addActionListener(this);
mb.add(m);
m = new MenuItem("Exit");
m.addActionListener(this);
mb.add(m);
MenuBar mBar = new MenuBar();
mBar.add(mb);
setMenuBar(mBar);
}
public void actionPerformed(ActionEven
{
try
{
String actionCommand = e.getActionCommand();
Dialog dl;
Label l;
if(actionCommand.equals("O
{
FileDialog fd=new FileDialog(f,"Open Image file",FileDialog.LOAD);
fd.setVisible(true);
String fname=fd.getFile();
fname = fname.substring(0,fname.le
System.out.println("File name="+fname);
if(fname!=null )
{
try{
Class hello = Class.forName(fname);
String cname = hello.getName();
System.out.println("Class Name="+cname);
}
catch ( Exception ee){
ee.printStackTrace();
}
}
} //end if
else if(actionCommand.equals("C
{
} //end if
else if(actionCommand.equals("E
{
System.exit(0);
} // end if
} catch (NullPointerException e1) {}
}
public static void main(String[] args)
{
f=new Display();
f.setVisible(true);
f.setTitle("DISPLAY");
}//end main
static Display f = new Display();
Panel center=new Panel();
}//end class
/* IMPORTANT:-The file You are selecting must be compiled before U run this program.
Because we are loading the Class object
which will execute when THe .java file is
compiled...SO be sure that , the file u are selecting from FileDialog must be compiled
and .class file should be present in same directory.:)
In the code , You selected .java file from Dialog. But be sure that .class should be present
for that .java file.
If You select .class file directly in filedialog,
Replace
fname = fname.substring(0,fname.le
with
fname = fname.substring(0,fname.le
*/
:)
ASKER
Thankz!
ASKER
Adjusted points from 100 to 150
I will send later another version
import java.lang.reflect.*;
class GetMethods {
public static void main(String args[]){
try {
Hello obj = new Hello();
Class hello = obj.getClass();
Method methods[] = hello.getMethods();
for ( int i=0; i < methods.length; i++ ) {
System.out.println("Method
}
}
catch ( Exception ee){
ee.printStackTrace();
}
}
}