复制代码
- //:ShowMethods.java
- //UsingJava1.1reflectiontoshowallthe
- //methodsofaclass,evenifthemethodsare
- //definedinthebaseclass.
- importjava.lang.reflect.*;
- publicclassShowMethods{
- staticfinalStringusage=
- "usage:
- "+
- "ShowMethodsqualified.class.name
- "+
- "Toshowallmethodsinclassor:
- "+
- "ShowMethodsqualified.class.nameword
- "+
- "Tosearchformethodsinvolvingword";
- publicstaticvoidmain(String[]args){
- if(args.length<1){
- System.out.println(usage);
- System.exit(0);
- }
- try{
- Classc=Class.forName(args[0]);
- Method[]m=c.getMethods();
- Constructor[]ctor=c.getConstructors();
- if(args.length==1){
- for(inti=0;i<m.length;i++)
- System.out.println(m[i].toString());
- for(inti=0;i<ctor.length;i++)
- System.out.println(ctor[i].toString());
- }
- else{
- for(inti=0;i<m.length;i++)
- if(m[i].toString()
- .indexOf(args[1])!=-1)
- System.out.println(m[i].toString());
- for(inti=0;i<ctor.length;i++)
- if(ctor[i].toString()
- .indexOf(args[1])!=-1)
- System.out.println(ctor[i].toString());
- }
- }catch(ClassNotFoundExceptione){
- System.out.println("Nosuchclass:"+e);
- }
- }
- }///:~
复制代码
- publicboolean
- java.lang.String.startsWith(java.lang.String,int)
- publicboolean
- java.lang.String.startsWith(java.lang.String)
- publicboolean
- java.lang.String.endsWith(java.lang.String)
复制代码
- //:ShowMethodsClean.java
- //ShowMethodswiththequalifiersstripped
- //tomaketheresultseasiertoread
- importjava.lang.reflect.*;
- importjava.io.*;
- publicclassShowMethodsClean{
- staticfinalStringusage=
- "usage:
- "+
- "ShowMethodsCleanqualified.class.name
- "+
- "Toshowallmethodsinclassor:
- "+
- "ShowMethodsCleanqualif.class.nameword
- "+
- "Tosearchformethodsinvolvingword";
- publicstaticvoidmain(String[]args){
- if(args.length<1){
- System.out.println(usage);
- System.exit(0);
- }
- try{
- Classc=Class.forName(args[0]);
- Method[]m=c.getMethods();
- Constructor[]ctor=c.getConstructors();
- //ConverttoanarrayofcleanedStrings:
- String[]n=
- newString[m.length+ctor.length];
- for(inti=0;i<m.length;i++){
- Strings=m[i].toString();
- n[i]=StripQualifiers.strip(s);
- }
- for(inti=0;i<ctor.length;i++){
- Strings=ctor[i].toString();
- n[i+m.length]=
- StripQualifiers.strip(s);
- }
- if(args.length==1)
- for(inti=0;i<n.length;i++)
- System.out.println(n[i]);
- else
- for(inti=0;i<n.length;i++)
- if(n[i].indexOf(args[1])!=-1)
- System.out.println(n[i]);
- }catch(ClassNotFoundExceptione){
- System.out.println("Nosuchclass:"+e);
- }
- }
- }
- classStripQualifiers{
- privateStreamTokenizerst;
- publicStripQualifiers(Stringqualified){
- st=newStreamTokenizer(
- newStringReader(qualified));
- st.ordinaryChar();//Keepthespaces
- }
- publicStringgetNext(){
- Strings=null;
- try{
- if(st.nextToken()!=
- StreamTokenizer.TT_EOF){
- switch(st.ttype){
- caseStreamTokenizer.TT_EOL:
- s=null;
- break;
- caseStreamTokenizer.TT_NUMBER:
- s=Double.toString(st.nval);
- break;
- caseStreamTokenizer.TT_WORD:
- s=newString(st.sval);
- break;
- default://singlecharacterinttype
- s=String.valueOf((char)st.ttype);
- }
- }
- }catch(IOExceptione){
- System.out.println(e);
- }
- returns;
- }
- publicstaticStringstrip(Stringqualified){
- StripQualifierssq=
- newStripQualifiers(qualified);
- Strings="",si;
- while((si=sq.getNext())!=null){
- intlastDot=si.lastIndexOf(.);
- if(lastDot!=-1)
- si=si.substring(lastDot+1);
- s+=si;
- }
- returns;
- }
- }///:~
欢迎光临 仓酷云 (http://ckuyun.com/) | Powered by Discuz! X3.2 |