diff --git a/TracerOnJavassist/src/tracer/Tracer.java b/TracerOnJavassist/src/tracer/Tracer.java index a7fa5d9..3e468b3 100644 --- a/TracerOnJavassist/src/tracer/Tracer.java +++ b/TracerOnJavassist/src/tracer/Tracer.java @@ -37,11 +37,19 @@ private static final String EXCEPT_FOR_METHODS = "java.lang.Thread.currentThread..|java.lang.Thread.getId.."; private static final String STANDARD_LIB = "java."; private static OutputStatementsGenerator outputStatementsGenerator = null; - private static CodeConverter conv = new CodeConverter(); + private static ClassPool cp = null; + private static CodeConverter conv = null; public static void main(String[] args) { initialize(new OutputStatementsGenerator(new JSONTraceGenerator())); // �����ŏo�̓t�H�[�}�b�g���w�肷�� - packageInstrumentation("worstCase"); // �w�肵���p�b�P�[�W�����̑S�N���X�ɃC���X�g�D�������e�[�V�������s�� + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + URL resource = loader.getResource(""); + try { + String classPath = URLDecoder.decode(resource.getPath(), "UTF-8"); + packageInstrumentation("_arraySample.", classPath); // �w�肵���p�b�P�[�W���̑S�N���X�ɃC���X�g�D�������e�[�V�������s�� + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } } /** @@ -50,14 +58,23 @@ * @param outputStatementsGenerator �o�͕�������(�o�̓t�H�[�}�b�g���w��ł���) */ public static void initialize(OutputStatementsGenerator outputStatementsGenerator) { - // �z��ւ̃A�N�Z�X�̌��o + initialize(outputStatementsGenerator, ClassPool.getDefault()); + } + + /** + * �o�͕���������w�肵�ăC���X�g�D�������e�[�V�����̏��������s�� + * + * @param outputStatementsGenerator �o�͕�������(�o�̓t�H�[�}�b�g���w��ł���) + * @param cp Javassist�̃N���X�v�[�� + */ + public static void initialize(OutputStatementsGenerator outputStatementsGenerator, ClassPool cp) { + // �z��ւ̃A�N�Z�X�̌��o] + Tracer.cp = cp; Tracer.outputStatementsGenerator = outputStatementsGenerator; // �����ŏo�̓t�H�[�}�b�g���w�肷�� + Tracer.conv = new CodeConverter(); if (!(Tracer.outputStatementsGenerator.getGenerator() instanceof PlainTextTraceGenerator)) { - // - ClassPool cp = ClassPool.getDefault(); - CtClass cc; try { - cc = cp.get(TRACER + "JSONArrayAdvisor"); // JSON�̏ꍇ�̂ݔz��A�N�Z�X���o�͂��� + CtClass cc = cp.get(TRACER + "JSONArrayAdvisor"); // JSON�̏ꍇ�̂ݔz��A�N�Z�X���o�͂��� conv.replaceArrayAccess(cc, new CodeConverter.DefaultArrayAccessReplacementMethodNames()); } catch (NotFoundException e1) { e1.printStackTrace(); @@ -70,28 +87,25 @@ * @return �������ς�: true, ����ȊO: false */ public static boolean isInitialized() { - return (outputStatementsGenerator != null); + return (outputStatementsGenerator != null) && (cp != null) && (conv != null); } /** * �w�肵���p�b�P�[�W���̃N���X�ɃC���X�g�D�������e�[�V�������s�� * * @param packageName �p�b�P�[�W�� + * @param classPath �o�͂���N���X�t�@�C���̃N���X�p�X(null �̏ꍇ�͏o�͂��Ȃ�) */ - public static void packageInstrumentation(String packageName) { - ClassLoader loader = Thread.currentThread().getContextClassLoader(); - URL resource = loader.getResource(packageName); + public static void packageInstrumentation(String packageName, String classPath) { File dir; - try { - dir = new File(URLDecoder.decode(resource.getPath(), "UTF-8")); - for (String file : dir.list()) { - if (file.endsWith(".class")) { - String className = packageName + "." + file.replace(".class", ""); - classInstrumentation(className); - } + dir = new File(classPath + packageName.replace(".", "/")); + for (File file : dir.listFiles()) { + if (file.isFile() && file.getName().endsWith(".class")) { + String className = packageName + file.getName().replace(".class", ""); + classInstrumentation(className, classPath); + } else if (file.isDirectory()) { + packageInstrumentation(packageName + file.getName() + ".", classPath); } - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); } } @@ -99,13 +113,12 @@ * �w�肵���N���X�ɃC���X�g�D�������e�[�V�������s�� * * @param className �N���X�� + * @param classPath �o�͂���N���X�t�@�C���̃N���X�p�X(null �̏ꍇ�͏o�͂��Ȃ�) */ - public static void classInstrumentation(String className) { - ClassPool cp = ClassPool.getDefault(); - CtClass cc; + public static void classInstrumentation(String className, String classPath) { try { - cc = cp.get(className); - classInstrumentation(cc); + CtClass cc = cp.get(className); + classInstrumentation(cc, classPath); } catch (NotFoundException | BadBytecode | CannotCompileException | IOException e) { e.printStackTrace(); } @@ -115,8 +128,9 @@ * �w�肵���N���X�ɃC���X�g�D�������e�[�V�������s�� * * @param cc Javassist�̃N���X�I�u�W�F�N�g + * @param classPath �o�͂���N���X�t�@�C���̃N���X�p�X(null �̏ꍇ�͏o�͂��Ȃ�) */ - public static void classInstrumentation(CtClass cc) throws BadBytecode, NotFoundException, CannotCompileException, IOException { + public static void classInstrumentation(CtClass cc, String classPath) throws BadBytecode, NotFoundException, CannotCompileException, IOException { classInitializerInstrumentation(cc, cc.getClassInitializer()); for (final CtConstructor c : cc.getDeclaredConstructors()) { @@ -126,7 +140,7 @@ methodInstrumentation(cc, m); } cc.instrument(conv); - cc.writeFile("bin"); + if (classPath != null) cc.writeFile(classPath); } private static void classInitializerInstrumentation(CtClass cc, CtConstructor classInitializer) throws BadBytecode, NotFoundException, CannotCompileException {