@Override public void convertImpl() throws CannotCompileException, NotFoundException, IOException { CtClass ctClass = getCtClass(); ClassPool pool = getClassPool(); CtClass jobInfoIf = pool.get(MapReduceTaskAccessor.class.getName()); ctClass.addInterface(jobInfoIf); CtField mapReduceJobId = CtField.make("java.lang.String mapReduceJobId_;", ctClass); ctClass.addField(mapReduceJobId); CtField mapReduceTaskIdMap = CtField.make( "java.util.Map mapReduceTaskIdMap_ = " + "new " + ConcurrentHashMap.class.getName() + "();", ctClass); ctClass.addField(mapReduceTaskIdMap); CtMethod getJobId = CtNewMethod.make("public String getJobId() {return mapReduceJobId_;}", ctClass); ctClass.addMethod(getJobId); CtMethod setJobId = CtNewMethod.make("public void setJobId(String jobId){mapReduceJobId_ = jobId;}", ctClass); ctClass.addMethod(setJobId); CtMethod getMapperName = CtNewMethod.make( "public String getMapperName(){" + "return mapper.getClass().getName();}", ctClass); ctClass.addMethod(getMapperName); CtMethod putTaskId = CtNewMethod.make( "public void putTaskId(String address, String taskId){" + "mapReduceTaskIdMap_.put(address, taskId);}", ctClass); ctClass.addMethod(putTaskId); CtMethod getSizeOfTaskIdMap = CtNewMethod.make( "public int getSizeOfTaskIdMap(){" + "return mapReduceTaskIdMap_.size();}", ctClass); ctClass.addMethod(getSizeOfTaskIdMap); List<CtBehavior> behaviorList = getMatcheDeclaredBehavior(); for (CtBehavior ctBehavior : behaviorList) { convertMethod(ctBehavior); } setNewClassfileBuffer(ctClass.toBytecode()); }
/** * Statementのメソッドに対して計測コードを埋め込む。 * * @param pool Statementを含むプール。 * @param ctClass Statementを実装するクラス * @return 変換結果 */ public static CtClass convertConnection(final ClassPool pool, final CtClass ctClass) { CtClass jvnConnction; try { jvnConnction = pool.get(JdbcJavelinConnection.class.getCanonicalName()); boolean hasInterface = hasInterface(ctClass, jvnConnction); if (hasInterface == false) { ctClass.addInterface(jvnConnction); } // すでにメソッドを追加している場合は処理を行わない if (hasBehavior(ctClass, "getJdbcJavelinProcessor")) { return ctClass; } CtField procField = CtField.make( "private " + DBProcessor.class.getCanonicalName() + " dbProcessor_;", ctClass); ctClass.addField(procField); CtMethod procGetMethod = CtMethod.make( "public " + DBProcessor.class.getCanonicalName() + " getJdbcJavelinProcessor(){ return dbProcessor_; }", ctClass); ctClass.addMethod(procGetMethod); CtMethod procSetMethod = CtMethod.make( "public void setJdbcJavelinProcessor(" + DBProcessor.class.getCanonicalName() + " dbProcessor){ dbProcessor_ = dbProcessor; }", ctClass); ctClass.addMethod(procSetMethod); CtField urlField = CtField.make("private String jdbcUrl_;", ctClass); ctClass.addField(urlField); CtMethod urlMethod = CtMethod.make("public String getJdbcUrl(){ return jdbcUrl_; }", ctClass); ctClass.addMethod(urlMethod); CtMethod urlSetMethod = CtMethod.make( "public void " + "setJdbcUrl(String jdbcUrl)" + "{ jdbcUrl_ = jdbcUrl; }", ctClass); ctClass.addMethod(urlSetMethod); } catch (NotFoundException ex) { SystemLogger.getInstance().warn(ex); return null; } catch (CannotCompileException ex) { SystemLogger.getInstance().warn(ex); return null; } CtBehavior[] behaviors = ctClass.getDeclaredBehaviors(); for (int index = 0; index < behaviors.length; index++) { CtBehavior method = behaviors[index]; // メソッドの定義がない場合、あるいはpublicでない // (->インターフェースに定義されていない)場合は実行しない。 final int MODIFIER = method.getModifiers(); if (Modifier.isAbstract(MODIFIER) || !Modifier.isPublic(MODIFIER)) { continue; } // BCI対象クラス「java.sql.Connection」に対して、コード転換を行う String methodName = method.getName(); try { if ("prepareStatement".equals(methodName)) { addSqlToFieldCon(ctClass, method); } else if ("prepareCall".equals(methodName)) { addSqlToFieldCon(ctClass, method); } } catch (CannotCompileException ex) { SystemLogger.getInstance().warn(ex); } } try { return ctClass; } catch (Exception ex) { SystemLogger.getInstance().warn(ex); return null; } }