* $Id$
*/
-import jp.sourceforge.stigmata.plugins.Opcode;
-
/**
*
* @author Haruaki Tamada
* @version $Revision$
*/
public class CurrentDepth{
- private Opcode opcode;
+ private WSPOpcode opcode;
private int depth;
- public CurrentDepth(int depth, Opcode opcode){
+ public CurrentDepth(int depth, WSPOpcode opcode){
this.depth = depth;
this.opcode = opcode;
}
return depth;
}
- public Opcode getOpcode(){
+ public WSPOpcode getOpcode(){
return opcode;
}
import jp.sourceforge.stigmata.BirthmarkContext;
import jp.sourceforge.stigmata.birthmarks.AbstractBirthmarkPreprocessor;
+import jp.sourceforge.stigmata.birthmarks.Opcode;
+import jp.sourceforge.stigmata.birthmarks.OpcodeExtractMethodVisitor;
import jp.sourceforge.stigmata.digger.ClassFileArchive;
import jp.sourceforge.stigmata.digger.ClassFileEntry;
-import jp.sourceforge.stigmata.plugins.Opcode;
-import jp.sourceforge.stigmata.plugins.OpcodeExtractMethodVisitor;
import jp.sourceforge.stigmata.spi.BirthmarkSpi;
import org.objectweb.asm.ClassAdapter;
import java.util.List;
import jp.sourceforge.stigmata.BirthmarkElement;
-import jp.sourceforge.stigmata.plugins.Opcode;
-import jp.sourceforge.stigmata.plugins.OpcodeManager;
+import jp.sourceforge.stigmata.birthmarks.Opcode;
+import jp.sourceforge.stigmata.birthmarks.OpcodeManager;
/**
*
int weight = Integer.parseInt(depthStringArray[2]);
int act = Integer.parseInt(depthStringArray[3]);
- Opcode o = new Opcode(OpcodeManager.getInstance().getOpcode(opcode));
- o.setWeight(weight);
+ WSPOpcode o = new WSPOpcode(OpcodeManager.getInstance().getOpcode(opcode), weight);
if(o.getCategory() == Opcode.Category.OBJECT || o.getCategory() == Opcode.Category.INVOKE){
o.setAct(act);
}
import jp.sourceforge.stigmata.BirthmarkElement;
import jp.sourceforge.stigmata.ExtractionUnit;
import jp.sourceforge.stigmata.birthmarks.ASMBirthmarkExtractor;
+import jp.sourceforge.stigmata.birthmarks.BirthmarkElementBuilder;
import jp.sourceforge.stigmata.birthmarks.BirthmarkExtractVisitor;
-import jp.sourceforge.stigmata.plugins.LabelOpcode;
-import jp.sourceforge.stigmata.plugins.Opcode;
-import jp.sourceforge.stigmata.plugins.OpcodeExtractMethodVisitor;
+import jp.sourceforge.stigmata.birthmarks.LabelOpcode;
+import jp.sourceforge.stigmata.birthmarks.Opcode;
+import jp.sourceforge.stigmata.birthmarks.OpcodeExtractVisitor;
import jp.sourceforge.stigmata.spi.BirthmarkSpi;
-import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
-import org.objectweb.asm.MethodVisitor;
/**
*
@Override
public BirthmarkExtractVisitor createExtractVisitor(ClassWriter writer, Birthmark birthmark, BirthmarkContext context){
- return new Visitor(writer, birthmark, context);
+ return new OpcodeExtractVisitor(writer, birthmark, context, new WSPBirthmarkElementBuilder());
}
@Override
};
}
- private BirthmarkElement[] buildElement(List<Opcode> opcodes, BirthmarkContext context){
- List<CurrentDepth> pattern = buildStackPattern(opcodes, context);
- List<BirthmarkElement> elements = new ArrayList<BirthmarkElement>();
-
- List<CurrentDepth> subPattern = new ArrayList<CurrentDepth>();
- for(CurrentDepth depth: pattern){
- subPattern.add(depth);
- if(depth.getDepth() == 0){
- elements.add(new StackPatternBasedBirthmarkElement(subPattern.toArray(new CurrentDepth[subPattern.size()])));
- subPattern.clear();
+ private static class WSPBirthmarkElementBuilder implements BirthmarkElementBuilder{
+ public BirthmarkElement[] buildElements(List<Opcode> opcodes, BirthmarkContext context){
+ List<CurrentDepth> pattern = buildStackPattern(opcodes, context);
+ List<BirthmarkElement> elements = new ArrayList<BirthmarkElement>();
+
+ List<CurrentDepth> subPattern = new ArrayList<CurrentDepth>();
+ for(CurrentDepth depth: pattern){
+ subPattern.add(depth);
+ if(depth.getDepth() == 0){
+ elements.add(new StackPatternBasedBirthmarkElement(subPattern.toArray(new CurrentDepth[subPattern.size()])));
+ subPattern.clear();
+ }
}
- }
- elements.add(new StackPatternBasedBirthmarkElement(subPattern.toArray(new CurrentDepth[subPattern.size()])));
+ elements.add(new StackPatternBasedBirthmarkElement(subPattern.toArray(new CurrentDepth[subPattern.size()])));
- return elements.toArray(new BirthmarkElement[elements.size()]);
- }
-
- @SuppressWarnings("unchecked")
- private List<CurrentDepth> buildStackPattern(List<Opcode> opcodes, BirthmarkContext context){
- Map<Label, Integer> tableMap = new HashMap<Label, Integer>();
- List<CurrentDepth> pattern = new ArrayList<CurrentDepth>();
- Map<Integer, Integer> weights = (Map<Integer, Integer>)context.getProperty("birthmarks.wsp.weights");
+ return elements.toArray(new BirthmarkElement[elements.size()]);
+ }
- int currentDepth = 0;
- Integer forwardedStatus = null;
- for(Opcode opcode: opcodes){
- if(opcode.getCategory() == Opcode.Category.TARGETER){
- forwardedStatus = tableMap.get(((LabelOpcode)opcode).getLabel());
- }
- else{
- opcode.setWeight(weights.get(opcode.getOpcode()));
- if(forwardedStatus == null){
- currentDepth += opcode.getAct();
+ @SuppressWarnings("unchecked")
+ private List<CurrentDepth> buildStackPattern(List<Opcode> opcodes, BirthmarkContext context){
+ Map<Label, Integer> tableMap = new HashMap<Label, Integer>();
+ List<CurrentDepth> pattern = new ArrayList<CurrentDepth>();
+ Map<Integer, Integer> weights = (Map<Integer, Integer>)context.getProperty("birthmarks.wsp.weights");
+
+ int currentDepth = 0;
+ Integer forwardedStatus = null;
+ for(Opcode opcode: opcodes){
+ if(opcode.getCategory() == Opcode.Category.TARGETER){
+ forwardedStatus = tableMap.get(((LabelOpcode)opcode).getLabel());
}
else{
- currentDepth = forwardedStatus + opcode.getAct();
- }
- forwardedStatus = null;
-
- pattern.add(new CurrentDepth(currentDepth, opcode));
- if(opcode.getCategory() == Opcode.Category.BRANCH){
- for(Iterator<Label> i = opcode.labels(); i.hasNext(); ){
- Label label = i.next();
- tableMap.put(label, currentDepth);
+ WSPOpcode wspOpcode = new WSPOpcode(opcode, weights.get(opcode.getOpcode()));
+ if(forwardedStatus == null){
+ currentDepth += opcode.getAct();
+ }
+ else{
+ currentDepth = forwardedStatus + opcode.getAct();
+ }
+ forwardedStatus = null;
+
+ pattern.add(new CurrentDepth(currentDepth, wspOpcode));
+ if(opcode.getCategory() == Opcode.Category.BRANCH){
+ for(Iterator<Label> i = opcode.labels(); i.hasNext(); ){
+ Label label = i.next();
+ tableMap.put(label, currentDepth);
+ }
}
}
}
- }
- return pattern;
- }
-
- private class Visitor extends BirthmarkExtractVisitor{
- private List<Opcode> opcodeList = new ArrayList<Opcode>();
-
- public Visitor(ClassVisitor visitor, Birthmark birthmark, BirthmarkContext context){
- super(visitor, birthmark, context);
- }
-
- public void visitEnd(){
- BirthmarkElement[] elements = buildElement(opcodeList, getContext());
- for(BirthmarkElement element: elements){
- addElement(element);
- }
- }
-
- @Override
- public MethodVisitor visitMethod(int arg0, String arg1, String arg2, String arg3, String[] arg4){
- MethodVisitor visitor = super.visitMethod(arg0, arg1, arg2, arg3, arg4);
- OpcodeExtractMethodVisitor opcodeVisitor = new OpcodeExtractMethodVisitor(visitor, opcodeList);
-
- return opcodeVisitor;
+ return pattern;
}
};
-
-
}
--- /dev/null
+package jp.sourceforge.stigmata.birthmarks.wsp;\r
+\r
+/*\r
+ * $Id$\r
+ */\r
+\r
+import jp.sourceforge.stigmata.birthmarks.Opcode;\r
+\r
+/**\r
+ *\r
+ *\r
+ * @author tamada\r
+ * @version $Revision$\r
+ */\r
+public class WSPOpcode extends Opcode {\r
+ private static final long serialVersionUID = 31469629831901737L;\r
+\r
+ private int weight;\r
+\r
+ public WSPOpcode(int opcode, String name, int argumentCount, int act, Category category) {\r
+ super(opcode, name, argumentCount, act, category);\r
+ }\r
+\r
+ public WSPOpcode(int opcode, String name, int argumentCount, int act, String category) {\r
+ super(opcode, name, argumentCount, act, category);\r
+ }\r
+\r
+ public WSPOpcode(Opcode opcode) {\r
+ super(opcode);\r
+ }\r
+\r
+ public WSPOpcode(int opcode, String name, int argumentCount, int act, Category category, int weight){\r
+ this(opcode, name, argumentCount, act, category);\r
+\r
+ setWeight(weight);\r
+ }\r
+\r
+ public WSPOpcode(Opcode opcode, int weight){\r
+ super(opcode);\r
+\r
+ setWeight(weight);\r
+ }\r
+\r
+ public void setWeight(int weight){\r
+ this.weight = weight;\r
+ }\r
+\r
+ public int getWeight(){\r
+ return weight;\r
+ }\r
+\r
+ public String toString(){\r
+ return String.format("%d:%s:%d:%f(%s)", getOpcode(), getName(), getWeight(), getAct(), getCategory());\r
+ }\r
+}\r
* $Id$
*/
-import jp.sourceforge.stigmata.plugins.Opcode;
+import jp.sourceforge.stigmata.birthmarks.Opcode;
import org.junit.Assert;
import org.junit.Before;
*/
public class CurrentDepthTest{
private CurrentDepth depth;
- private Opcode opcode;
+ private WSPOpcode opcode;
@Before
public void setup(){
- opcode = new Opcode(4, "iconst_1", 0, 1, Opcode.Category.NORMAL);
+ opcode = new WSPOpcode(4, "iconst_1", 0, 1, Opcode.Category.NORMAL);
depth = new CurrentDepth(3, opcode);
}
* $Id$
*/
-import jp.sourceforge.stigmata.plugins.Opcode;
+import jp.sourceforge.stigmata.birthmarks.Opcode;
import org.junit.Assert;
import org.junit.Before;
public void setup(){
CurrentDepth[] depthList = new CurrentDepth[7];
- depthList[0] = new CurrentDepth(1, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
- depthList[1] = new CurrentDepth(2, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
- depthList[2] = new CurrentDepth(3, new Opcode( 4, "iconst_1", 0, 1, Opcode.Category.NORMAL, 1));
- depthList[3] = new CurrentDepth(2, new Opcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4));
- depthList[4] = new CurrentDepth(2, new Opcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1));
- depthList[5] = new CurrentDepth(1, new Opcode(104, "imul", 0, -1, Opcode.Category.NORMAL, 6));
- depthList[6] = new CurrentDepth(0, new Opcode(172, "ireturn", 0, -1, Opcode.Category.NORMAL, 2));
+ depthList[0] = new CurrentDepth(1, new WSPOpcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
+ depthList[1] = new CurrentDepth(2, new WSPOpcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
+ depthList[2] = new CurrentDepth(3, new WSPOpcode( 4, "iconst_1", 0, 1, Opcode.Category.NORMAL, 1));
+ depthList[3] = new CurrentDepth(2, new WSPOpcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4));
+ depthList[4] = new CurrentDepth(2, new WSPOpcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1));
+ depthList[5] = new CurrentDepth(1, new WSPOpcode(104, "imul", 0, -1, Opcode.Category.NORMAL, 6));
+ depthList[6] = new CurrentDepth(0, new WSPOpcode(172, "ireturn", 0, -1, Opcode.Category.NORMAL, 2));
element = new StackPatternBasedBirthmarkElement(depthList);
}
@Test
public void testCalculateWeightedCommonSubsequence(){
CurrentDepth[] depthList = new CurrentDepth[10];
- depthList[0] = new CurrentDepth(1, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
- depthList[1] = new CurrentDepth(2, new Opcode( 4, "iconst_1", 0, 1, Opcode.Category.NORMAL, 1));
- depthList[2] = new CurrentDepth(1, new Opcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4));
- depthList[3] = new CurrentDepth(1, new Opcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1));
- depthList[4] = new CurrentDepth(2, new Opcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
- depthList[5] = new CurrentDepth(3, new Opcode( 5, "iconst_2", 0, 1, Opcode.Category.NORMAL, 1));
- depthList[6] = new CurrentDepth(2, new Opcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4));
- depthList[7] = new CurrentDepth(2, new Opcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1));
- depthList[8] = new CurrentDepth(1, new Opcode( 96, "iadd", 0, -1, Opcode.Category.NORMAL, 3));
- depthList[9] = new CurrentDepth(0, new Opcode(172, "ireturn", 0, -1, Opcode.Category.NORMAL, 2));
+ depthList[0] = new CurrentDepth(1, new WSPOpcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
+ depthList[1] = new CurrentDepth(2, new WSPOpcode( 4, "iconst_1", 0, 1, Opcode.Category.NORMAL, 1));
+ depthList[2] = new CurrentDepth(1, new WSPOpcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4));
+ depthList[3] = new CurrentDepth(1, new WSPOpcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1));
+ depthList[4] = new CurrentDepth(2, new WSPOpcode( 26, "iload_0", 0, 1, Opcode.Category.NORMAL, 7));
+ depthList[5] = new CurrentDepth(3, new WSPOpcode( 5, "iconst_2", 0, 1, Opcode.Category.NORMAL, 1));
+ depthList[6] = new CurrentDepth(2, new WSPOpcode(100, "isub", 0, -1, Opcode.Category.NORMAL, 4));
+ depthList[7] = new CurrentDepth(2, new WSPOpcode(184, "invokestatic", 2, 0, Opcode.Category.INVOKE, 1));
+ depthList[8] = new CurrentDepth(1, new WSPOpcode( 96, "iadd", 0, -1, Opcode.Category.NORMAL, 3));
+ depthList[9] = new CurrentDepth(0, new WSPOpcode(172, "ireturn", 0, -1, Opcode.Category.NORMAL, 2));
StackPatternBasedBirthmarkElement pattern2 = new StackPatternBasedBirthmarkElement(depthList);
Assert.assertEquals(21, element.getWeight(pattern2));