package jp.igapyon.jcfa;\r
\r
+import java.io.File;\r
import java.io.IOException;\r
\r
-import jp.igapyon.jcfa.util.JcfaUtil;\r
-\r
-import org.apache.bcel.Constants;\r
-/**\r
- * Apache Jakarta BCELを用いたクラスファイル(バイトコード)解析サンプル\r
- */\r
-import org.apache.bcel.classfile.ClassFormatException;\r
-import org.apache.bcel.classfile.ClassParser;\r
-import org.apache.bcel.classfile.Code;\r
-import org.apache.bcel.classfile.Field;\r
-import org.apache.bcel.classfile.JavaClass;\r
-import org.apache.bcel.classfile.Method;\r
+import jp.igapyon.jcfa.util.JcfaWriteUtil;\r
+import jp.igapyon.jcfa.vo.JcfaUnit;\r
\r
public class JavaClassFileAnalyzer {\r
- private static final String CLASS_MODULE = "./bin/test/TestJavaClass002.class";\r
+ public static final void main(final String[] args) throws IOException {\r
+ System.err.println("JavaClassFileAnalyzer ("\r
+ + JcfaVersionConstants.getVersion() + ")");\r
\r
- public static final void main(final String[] args) {\r
- new JavaClassFileAnalyzer().process();\r
+ new JavaClassFileAnalyzer().parseDir(new File("./bin/test"));\r
+ new JavaClassFileAnalyzer().parseDir(new File(\r
+ "./bin/jp/igapyon/jcfa/vo"));\r
}\r
\r
- private final void process() {\r
- try {\r
- final JavaClass jc = new ClassParser(CLASS_MODULE).parse();\r
- System.out.println("Classname : " + jc.getClassName());\r
- System.out.println("Parent class: " + jc.getSuperclassName());\r
-\r
- final org.apache.bcel.classfile.Field[] fields = jc.getFields();\r
- for (int indexField = 0; indexField < fields.length; indexField++) {\r
- final Field field = fields[indexField];\r
- System.out.println(" Field:" + field.getName());\r
- String access = "";\r
- access += field.isPublic() ? "public " : "";\r
- access += field.isAbstract() ? "abstract " : "";\r
- access += field.isStatic() ? "static " : "";\r
- access += field.isFinal() ? "final " : "";\r
- System.out.println(" " + access);\r
- System.out.println(" " + field.getSignature());\r
+ private void parseDir(final File dir) throws IOException {\r
+ final File[] files = dir.listFiles();\r
+ if (files == null) {\r
+ return;\r
+ }\r
+ for (File file : files) {\r
+ if (file.isDirectory()) {\r
+ parseDir(file);\r
}\r
-\r
- final org.apache.bcel.classfile.Method[] methods = jc.getMethods();\r
- for (int indexMethod = 0; indexMethod < methods.length; indexMethod++) {\r
- final Method method = methods[indexMethod];\r
- System.out.println(" Method:" + method.getName());\r
- final Code code = method.getCode();\r
- if (code == null) {\r
- continue;\r
- }\r
-\r
- final byte[] codes = code.getCode();\r
- for (int pc = 0; pc < codes.length; pc++) {\r
- final short opcode = JcfaUtil.byte2UnsignedByte(codes[pc]);\r
- short operands = Constants.NO_OF_OPERANDS[opcode];\r
- if (operands < 0) {\r
- System.out.println(" TODO negative value:"\r
- + Constants.OPCODE_NAMES[opcode] + ": "\r
- + operands);\r
- // break;\r
- }\r
-\r
- switch (opcode) {\r
- case Constants.RETURN: {\r
- System.out.println(" " + pc + ": "\r
- + Constants.OPCODE_NAMES[opcode]);\r
- break;\r
- }\r
- case Constants.GETSTATIC: {\r
- System.out.println(" "\r
- + pc\r
- + ": "\r
- + Constants.OPCODE_NAMES[opcode]\r
- + ": "\r
- + JcfaUtil.getConstantFieldrefString(jc,\r
- codes[pc + 1], codes[pc + 2]));\r
- break;\r
- }\r
- case Constants.LDC: {\r
- System.out\r
- .println(" "\r
- + pc\r
- + ": "\r
- + Constants.OPCODE_NAMES[opcode]\r
- + ": "\r
- + JcfaUtil.getConstantString(jc,\r
- codes[pc + 1]));\r
- }\r
- break;\r
- case Constants.INVOKEVIRTUAL:\r
- case Constants.INVOKESPECIAL: {\r
- final int operand = JcfaUtil.byte2UnsignedShort(\r
- codes[pc + 1], codes[pc + 2]);\r
- System.out.println(" "\r
- + pc\r
- + ": "\r
- + Constants.OPCODE_NAMES[opcode]\r
- + ": "\r
- + JcfaUtil.getConstantMethodRefString(jc,\r
- operand));\r
- }\r
- break;\r
- case Constants.LOOKUPSWITCH:\r
- int result = JcfaUtil.byte2Int(codes[pc + 1],\r
- codes[pc + 2], codes[pc + 3], codes[pc + 4]);\r
- System.out\r
- .println(" TODO skipping bytes: " + (result));\r
-\r
- int lookupOp = pc + 5;\r
-\r
- short diff = JcfaUtil\r
- .byte2UnsignedByte(codes[lookupOp++]);\r
- System.out.println(" TODO skipping bytes: " + (diff));\r
-\r
- int loopCount = JcfaUtil.byte2Int(codes[lookupOp++],\r
- codes[lookupOp++], codes[lookupOp++],\r
- codes[lookupOp++]);\r
- for (int index = 0; index < loopCount; index++) {\r
- System.out.println(" "\r
- + JcfaUtil.byte2Int(codes[lookupOp++],\r
- codes[lookupOp++],\r
- codes[lookupOp++],\r
- codes[lookupOp++])\r
- + ":"\r
- + (JcfaUtil.byte2Int(codes[lookupOp++],\r
- codes[lookupOp++],\r
- codes[lookupOp++],\r
- codes[lookupOp++]) + pc));\r
- }\r
-\r
- short diff2 = JcfaUtil\r
- .byte2UnsignedByte(codes[lookupOp++]);\r
- System.out.println(" TODO skipping bytes: " + (diff2));\r
-\r
- operands += (lookupOp - pc);\r
-\r
- break;\r
- default: {\r
- System.out.println(" " + pc + ": "\r
- + Constants.OPCODE_NAMES[opcode] + " ("\r
- + operands + ")");\r
- }\r
- break;\r
- }\r
- pc += operands;\r
+ if (file.isFile()) {\r
+ if (file.getName().endsWith(".class")) {\r
+ new JavaClassFileAnalyzer().process(file, new File(\r
+ "./testJavaClass/output"));\r
}\r
}\r
- } catch (ClassFormatException e) {\r
- e.printStackTrace();\r
- } catch (IOException e) {\r
- e.printStackTrace();\r
}\r
}\r
+\r
+ private final void process(final File inputFile, final File outputDir)\r
+ throws IOException {\r
+ final JcfaUnit jcfaUnit = new JcfaParser().parseUnit(inputFile,\r
+ outputDir);\r
+\r
+ JcfaWriteUtil.writeToFile(jcfaUnit);\r
+ }\r
}
\ No newline at end of file