středa 12. listopadu 2014

BCEL - výpis

Následující kód ukazuje možnost výpisu všech zajímavých informací o třídě z .class souboru pomocí BCEL knihovny.

Získat pomocí BCEL importované balíky je možné po prozkoumání kódu a nalezení volání funkcí, které se nenachází v dané třídě a následné vyhledání těchto metod v balících.


1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
package test_bcel;

import org.apache.bcel.Repository;
import org.apache.bcel.classfile.*;
import org.apache.bcel.generic.Type;

public class test {

 public static void main(String[] args) {

  try {
   JavaClass java_class;
         String name = "java/lang/String";
         //String name = "/home/greg/workspace/starshipmap/bin/ssView/Policko.class";
   //String name = "/home/greg/workspace/test-procyon/bin/test.class";
         
         try{
          // pro zabudovane tridy
          // napr: String name = "java/lang/String";
          java_class = Repository.lookupClass(name);
         }catch(ClassNotFoundException cnfe){
          // pro vlastni tridy
          // napr: String name = "/home/greg/workspace/starshipmap/bin/ssView/Policko.class";
          java_class = new ClassParser(name).parse();
         }
         int x = 0;
         printf(x+"**");x++;
         printf("getClassName: "+java_class.getClassName());
         printf(x+"**");x++;
         printf("getFileName: "+java_class.getFileName());
         printf(x+"**");x++;
         printf("getPackageName: "+java_class.getPackageName());
         printf(x+"**");x++;
         printf("getSourceFileName: "+java_class.getSourceFileName());
         printf(x+"**");x++;
         printf("getSuperclassName: "+java_class.getSuperclassName());
         printf(x+"**");x++;
         printf("getAccessFlags: "+java_class.getAccessFlags()+"");
         printf(x+"**");x++;
         JavaClass [] allinterfaces = java_class.getAllInterfaces();
         printf("***ALL INTERFACES");
         for (int i = 0 ; i < allinterfaces.length ; i++){
          printf("getClassName: "+allinterfaces[i].getClassName());
         }
         printf(x+"**");x++;
         JavaClass [] interfaces = java_class.getInterfaces();
         printf("***INTERFACES");
         for (int i = 0 ; i < interfaces.length ; i++){
          printf("getClassName: "+interfaces[i].getClassName());
         }
         printf(x+"**");x++;
         printf("***SUPER CLASSES");
         JavaClass [] superclases = java_class.getSuperClasses();
         for (int i = 0 ; i < superclases.length ; i++){
          printf("getClassName: "+superclases[i].getClassName());
         }
         printf(x+"**");x++;
         Attribute [] atributes = java_class.getAttributes();
         printf("***ATTRIBUTES");
         for (int i = 0 ; i < atributes.length ; i++){
          printf("atributes["+i+"].toString(): "+atributes[i].toString());
         }
         printf(x+"**");x++;
         Field [] fields = java_class.getFields();
         printf("***FIELDS");
         for (int i = 0 ; i < fields.length ; i++){
          printf("fields["+i+"].getName(): "+fields[i].getName());
          printf("fields["+i+"].getType(): "+fields[i].getType().toString());
          printf("fields["+i+"].getSignature(): "+fields[i].getSignature());
         }
         printf(x+"**");x++;
         Method [] methods = java_class.getMethods();
         for (int i = 0 ; i < methods.length ; i++){
          printf("***METODA");
          printf("methods["+i+"].getName(): "+methods[i].getName());
          printf("methods["+i+"].getSignature(): "+methods[i].getSignature());
          printf("methods["+i+"].getReturnType().toString(): "+methods[i].getReturnType().toString());
          printf("methods["+i+"].getReturnType().getSignature(): "+methods[i].getReturnType().getSignature());
          /*printf("***ATRIBUTY"); // nezajimave
          Attribute [] atributesmeth = methods[i].getAttributes();
          for (int y = 0 ; y < atributesmeth.length ; y++){
           printf("atributesmeth[y].toString() "+atributesmeth[y].toString());
          }*/
          printf("***TYPY");
          Type [] types = methods[i].getArgumentTypes();
          for (int y = 0 ; y < types.length ; y++){
           printf("types["+y+"].toString(): "+types[y].toString());
          }
         }
         printf(x+"**");x++;
         
         printf("getMajor: "+java_class.getMajor()+"");
         printf(x+"**");x++;
         printf("getMinor: "+java_class.getMinor()+"");
  } catch (Exception e) {
   e.printStackTrace();
  }
    }
 
 public static void printf(String string){
  System.out.println(string);
 }


}

Zdroje:
http://commons.apache.org/proper/commons-bcel/manual.html
http://commons.apache.org/proper/commons-bcel/apidocs/org/apache/bcel/classfile/package-summary.html
vztahy mezi třídama v .jar http://stackoverflow.com/questions/26760153/parse-jar-file-and-find-relationships-between-classes

Žádné komentáře:

Okomentovat