OSDN Git Service

move test case to new opcode extraction library
[stigmata/stigmata-plugins.git] / wsp / src / main / java / jp / sourceforge / stigmata / birthmarks / wsp / StackPatternBasedBirthmarkExtractor.java
index 183306f..fd722fc 100644 (file)
@@ -11,11 +11,14 @@ import java.util.List;
 import java.util.Map;
 
 import jp.sourceforge.stigmata.Birthmark;
+import jp.sourceforge.stigmata.BirthmarkContext;
 import jp.sourceforge.stigmata.BirthmarkElement;
-import jp.sourceforge.stigmata.BirthmarkEnvironment;
 import jp.sourceforge.stigmata.ExtractionUnit;
 import jp.sourceforge.stigmata.birthmarks.ASMBirthmarkExtractor;
 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.spi.BirthmarkSpi;
 
 import org.objectweb.asm.ClassVisitor;
@@ -24,7 +27,7 @@ import org.objectweb.asm.Label;
 import org.objectweb.asm.MethodVisitor;
 
 /**
- * 
+ *
  * @author Haruaki Tamada
  * @version $Revision$
  */
@@ -34,37 +37,39 @@ public class StackPatternBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
     }
 
     @Override
-    public BirthmarkExtractVisitor createExtractVisitor(ClassWriter writer, Birthmark birthmark, BirthmarkEnvironment environment){
-        return new Visitor(writer, birthmark, environment);
+    public BirthmarkExtractVisitor createExtractVisitor(ClassWriter writer, Birthmark birthmark, BirthmarkContext context){
+        return new Visitor(writer, birthmark, context);
     }
 
     @Override
     public ExtractionUnit[] getAcceptableUnits(){
         return new ExtractionUnit[] {
-            ExtractionUnit.ARCHIVE, ExtractionUnit.PACKAGE, ExtractionUnit.CLASS,
+            ExtractionUnit.CLASS,
         };
     }
 
-    private BirthmarkElement[] buildElement(List<Opcode> opcodes){
-        StackPattern pattern = buildStackPattern(opcodes);
+    private BirthmarkElement[] buildElement(List<Opcode> opcodes, BirthmarkContext context){
+        List<CurrentDepth> pattern = buildStackPattern(opcodes, context);
         List<BirthmarkElement> elements = new ArrayList<BirthmarkElement>();
 
-        StackPattern subpattern = new StackPattern();
+        List<CurrentDepth> subPattern = new ArrayList<CurrentDepth>();
         for(CurrentDepth depth: pattern){
-            subpattern.update(depth);
+            subPattern.add(depth);
             if(depth.getDepth() == 0){
-                elements.add(new StackPatternBasedBirthmarkElement(subpattern));
-                subpattern = new StackPattern();
+                elements.add(new StackPatternBasedBirthmarkElement(subPattern.toArray(new CurrentDepth[subPattern.size()])));
+                subPattern.clear();
             }
         }
-        elements.add(new StackPatternBasedBirthmarkElement(subpattern));
+        elements.add(new StackPatternBasedBirthmarkElement(subPattern.toArray(new CurrentDepth[subPattern.size()])));
 
         return elements.toArray(new BirthmarkElement[elements.size()]);
     }
 
-    private StackPattern buildStackPattern(List<Opcode> opcodes){ 
+    @SuppressWarnings("unchecked")
+    private List<CurrentDepth> buildStackPattern(List<Opcode> opcodes, BirthmarkContext context){
         Map<Label, Integer> tableMap = new HashMap<Label, Integer>();
-        StackPattern pattern = new StackPattern();
+        List<CurrentDepth> pattern = new ArrayList<CurrentDepth>();
+        Map<Integer, Integer> weights = (Map<Integer, Integer>)context.getProperty("birthmarks.wsp.weights");
 
         int currentDepth = 0;
         Integer forwardedStatus = null;
@@ -73,15 +78,16 @@ public class StackPatternBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
                 forwardedStatus = tableMap.get(((LabelOpcode)opcode).getLabel());
             }
             else{
+                opcode.setWeight(weights.get(opcode.getOpcode()));
                 if(forwardedStatus == null){
                     currentDepth += opcode.getAct();
                 }
                 else{
-                    currentDepth = forwardedStatus + opcode.getAct(); 
+                    currentDepth = forwardedStatus + opcode.getAct();
                 }
                 forwardedStatus = null;
 
-                pattern.update(currentDepth, opcode);
+                pattern.add(new CurrentDepth(currentDepth, opcode));
                 if(opcode.getCategory() == Opcode.Category.BRANCH){
                     for(Iterator<Label> i = opcode.labels(); i.hasNext(); ){
                         Label label = i.next();
@@ -96,12 +102,12 @@ public class StackPatternBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
     private class Visitor extends BirthmarkExtractVisitor{
         private List<Opcode> opcodeList = new ArrayList<Opcode>();
 
-        public Visitor(ClassVisitor visitor, Birthmark birthmark, BirthmarkEnvironment environment){
-            super(visitor, birthmark, environment);
+        public Visitor(ClassVisitor visitor, Birthmark birthmark, BirthmarkContext context){
+            super(visitor, birthmark, context);
         }
 
         public void visitEnd(){
-            BirthmarkElement[] elements = buildElement(opcodeList);
+            BirthmarkElement[] elements = buildElement(opcodeList, getContext());
             for(BirthmarkElement element: elements){
                 addElement(element);
             }
@@ -110,7 +116,7 @@ public class StackPatternBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
         @Override
         public MethodVisitor visitMethod(int arg0, String arg1, String arg2, String arg3, String[] arg4){
             MethodVisitor visitor = super.visitMethod(arg0, arg1, arg2, arg3, arg4);
-            OpcodeExtractionMethodVisitor opcodeVisitor = new OpcodeExtractionMethodVisitor(visitor, opcodeList);
+            OpcodeExtractMethodVisitor opcodeVisitor = new OpcodeExtractMethodVisitor(visitor, opcodeList);
 
             return opcodeVisitor;
         }