OSDN Git Service

update
authortama3 <tama3@acee48c3-7b26-0410-bdac-b3d0e5314bbc>
Mon, 26 Apr 2010 10:42:01 +0000 (10:42 +0000)
committertama3 <tama3@acee48c3-7b26-0410-bdac-b3d0e5314bbc>
Mon, 26 Apr 2010 10:42:01 +0000 (10:42 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/stigmata/plugins/trunk@390 acee48c3-7b26-0410-bdac-b3d0e5314bbc

wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/CurrentDepth.java
wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/OpcodeWeightCalculatePreprocessor.java
wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/StackPatternBasedBirthmarkElement.java
wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/StackPatternBasedBirthmarkExtractor.java
wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/WSPOpcode.java [new file with mode: 0644]
wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/CurrentDepthTest.java
wsp/src/test/java/jp/sourceforge/stigmata/birthmarks/wsp/StackPatternBasedBirthmarkElementTest.java

index a3ac359..7446b28 100644 (file)
@@ -4,18 +4,16 @@ package jp.sourceforge.stigmata.birthmarks.wsp;
  * $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;
     }
@@ -24,7 +22,7 @@ public class CurrentDepth{
         return depth;
     }
 
-    public Opcode getOpcode(){
+    public WSPOpcode getOpcode(){
         return opcode;
     }
 
index 7791ea8..5eb3b99 100644 (file)
@@ -15,10 +15,10 @@ import java.util.Set;
 
 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;
index 4be7002..29c0d0e 100644 (file)
@@ -10,8 +10,8 @@ import java.util.Iterator;
 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;
 
 /**
  *
@@ -41,8 +41,7 @@ public class StackPatternBasedBirthmarkElement extends BirthmarkElement implemen
                 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);
                 }
index fd722fc..68cfeb9 100644 (file)
@@ -15,16 +15,15 @@ import jp.sourceforge.stigmata.BirthmarkContext;
 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;
 
 /**
  *
@@ -38,7 +37,7 @@ public class StackPatternBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
 
     @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
@@ -48,79 +47,56 @@ public class StackPatternBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
         };
     }
 
-    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;
         }
     };
-
-
 }
diff --git a/wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/WSPOpcode.java b/wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/WSPOpcode.java
new file mode 100644 (file)
index 0000000..89886c4
--- /dev/null
@@ -0,0 +1,55 @@
+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
index ec60f36..0977418 100644 (file)
@@ -4,7 +4,7 @@ package jp.sourceforge.stigmata.birthmarks.wsp;
  * $Id$
  */
 
-import jp.sourceforge.stigmata.plugins.Opcode;
+import jp.sourceforge.stigmata.birthmarks.Opcode;
 
 import org.junit.Assert;
 import org.junit.Before;
@@ -17,11 +17,11 @@ import org.junit.Test;
  */
 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);
     }
 
index b63ee6d..061baca 100644 (file)
@@ -4,7 +4,7 @@ package jp.sourceforge.stigmata.birthmarks.wsp;
  * $Id$
  */
 
-import jp.sourceforge.stigmata.plugins.Opcode;
+import jp.sourceforge.stigmata.birthmarks.Opcode;
 
 import org.junit.Assert;
 import org.junit.Before;
@@ -22,13 +22,13 @@ public class StackPatternBasedBirthmarkElementTest{
     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);
     }
@@ -66,16 +66,16 @@ public class StackPatternBasedBirthmarkElementTest{
     @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));