OSDN Git Service

almost button up
authortama3 <tama3@acee48c3-7b26-0410-bdac-b3d0e5314bbc>
Tue, 9 Dec 2008 05:23:07 +0000 (05:23 +0000)
committertama3 <tama3@acee48c3-7b26-0410-bdac-b3d0e5314bbc>
Tue, 9 Dec 2008 05:23:07 +0000 (05:23 +0000)
git-svn-id: svn+ssh://svn.sourceforge.jp/svnroot/stigmata/plugins/trunk@344 acee48c3-7b26-0410-bdac-b3d0e5314bbc

wsp/pom.xml
wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/Opcode.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/StackPatternBasedBirthmarkService.java
wsp/src/main/java/jp/sourceforge/stigmata/birthmarks/wsp/WeightCalculator.java

index 2c37e1b..be5a638 100644 (file)
       <version>2.0.0-SNAPSHOT</version>
       <scope>provided</scope>
     </dependency>
-    <dependency>\r
-      <groupId>asm</groupId>\r
-      <artifactId>asm-all</artifactId>\r
-      <version>2.2.3</version>\r
-      <scope>compile</scope>\r
-    </dependency>\r
+    <dependency>
+      <groupId>asm</groupId>
+      <artifactId>asm-all</artifactId>
+      <version>2.2.3</version>
+      <scope>compile</scope>
+    </dependency>
   </dependencies>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-dependency-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>copy-dependencies</id>
+            <phase>compile</phase>
+            <goals>
+              <goal>copy-dependencies</goal>
+            </goals>
+            <configuration>
+              <outputDirectory>${project.build.directory}</outputDirectory>
+              <includeScope>runtime</includeScope>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
 </project>
index 29fe415..bf93352 100644 (file)
@@ -120,4 +120,8 @@ public class Opcode implements Serializable{
     public int getWeight(){
         return weight;
     }
+
+    public String toString(){
+        return String.format("%d:%s:%d:%f(%s)", getOpcode(), getName(), getAct(), getWeight(), getCategory());
+    }
 }
index f17385e..1fe5f92 100644 (file)
@@ -8,8 +8,10 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import jp.sourceforge.stigmata.BirthmarkContext;
 import jp.sourceforge.stigmata.birthmarks.AbstractBirthmarkPreprocessor;
@@ -36,7 +38,10 @@ public class OpcodeWeightCalculatePreprocessor extends AbstractBirthmarkPreproce
     public void preprocess(ClassFileArchive[] targets, BirthmarkContext context){
         Map<Integer, Integer> targetMap = new HashMap<Integer, Integer>();
 
-        int classCount = readOpcodes(targets, targetMap);
+        int classCount = 0;
+        for(ClassFileArchive archive: targets){
+            classCount += readOpcodes(archive, targetMap);
+        }
 
         Map<Integer, Integer> weights = new HashMap<Integer, Integer>();
         for(Map.Entry<Integer, Integer> entry: targetMap.entrySet()){
@@ -47,46 +52,48 @@ public class OpcodeWeightCalculatePreprocessor extends AbstractBirthmarkPreproce
                 c = count;
             }
 
-            weights.put(opcode, (int)Math.log(classCount / c));
+            weights.put(opcode, (int)Math.round(Math.log(classCount / c)));
         }
 
         context.putProperty("birthmarks.wsp.weights", weights);
     }
 
-    private int readOpcodes(ClassFileArchive[] targets, Map<Integer, Integer> targetMap){
+    private int readOpcodes(ClassFileArchive archive, Map<Integer, Integer> targetMap){
         int count = 0;
-        for(ClassFileArchive archive: targets){
-            for(ClassFileEntry entry: archive){
-                count++;
-                final List<Opcode> opcodes = new ArrayList<Opcode>();
-                try{
-                    InputStream in = entry.getLocation().openStream();
+        for(ClassFileEntry entry: archive){
+            count++;
+            final List<Opcode> opcodes = new ArrayList<Opcode>();
+            try{
+                InputStream in = entry.getLocation().openStream();
 
-                    ClassReader reader = new ClassReader(in);
-                    ClassWriter writer = new ClassWriter(false);
-                    ClassAdapter opcodeExtractVisitor = new ClassAdapter(writer){
-                        @Override
-                        public MethodVisitor visitMethod(int arg0, String arg1, String arg2, String arg3, String[] arg4){
-                            OpcodeExtractionMethodVisitor visitor =
-                                new OpcodeExtractionMethodVisitor(super.visitMethod(arg0, arg1, arg2, arg3, arg4), opcodes);
-                            return visitor;
-                        }
-                    };
-                    reader.accept(opcodeExtractVisitor, false);
+                ClassReader reader = new ClassReader(in);
+                ClassWriter writer = new ClassWriter(false);
+                ClassAdapter opcodeExtractVisitor = new ClassAdapter(writer){
+                    @Override
+                    public MethodVisitor visitMethod(int arg0, String arg1, String arg2, String arg3, String[] arg4){
+                        OpcodeExtractionMethodVisitor visitor =
+                            new OpcodeExtractionMethodVisitor(super.visitMethod(arg0, arg1, arg2, arg3, arg4), opcodes);
+                        return visitor;
+                    }
+                };
+                reader.accept(opcodeExtractVisitor, false);
 
-                    for(Opcode opcode: opcodes){
-                        if(opcode.getCategory() != Opcode.Category.TARGETER){
-                            Integer i = targetMap.get(opcode.getOpcode());
-                            if(i == null){
-                                i = 0;
-                            }
-                            i = i + 1;
-                            targetMap.put(opcode.getOpcode(), i);
-                        }
+                Set<Integer> set = new HashSet<Integer>();
+                for(Opcode opcode: opcodes){
+                    if(opcode.getCategory() != Opcode.Category.TARGETER){
+                        set.add(opcode.getOpcode());
                     }
+                }
 
-                } catch(IOException e){
+                for(Integer i: set){
+                    Integer v = targetMap.get(i);
+                    if(v == null){
+                        v = 0;
+                    }
+                    v = v + 1;
+                    targetMap.put(i, v);
                 }
+            } catch(IOException e){
             }
         }
         return count;
index 1cb7770..d055b08 100644 (file)
@@ -96,7 +96,7 @@ public class StackPatternBasedBirthmarkElement extends BirthmarkElement implemen
             }
             this.weight = w;
         }
-        return weight;        
+        return weight;
     }
 
     private static String getStringRepresentation(CurrentDepth[] depth){
index 514633e..330c938 100644 (file)
@@ -61,7 +61,7 @@ public class StackPatternBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
 
         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>();
@@ -71,11 +71,11 @@ public class StackPatternBasedBirthmarkExtractor extends ASMBirthmarkExtractor{
         int currentDepth = 0;
         Integer forwardedStatus = null;
         for(Opcode opcode: opcodes){
-            opcode.setWeight(weights.get(opcode.getOpcode()));
             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();
                 }
index 85ca634..70231af 100644 (file)
@@ -43,6 +43,11 @@ public class StackPatternBasedBirthmarkService extends AbstractBirthmarkService{
     }
 
     @Override
+    public boolean isExpert(){
+        return false;
+    }
+
+    @Override
     public String getType(){
         return "wsp";
     }
index 6af3eb1..97bdba4 100644 (file)
@@ -35,7 +35,7 @@ public class WeightCalculator{
                 for(int j = 0; j < wcs[0].length; j++){
                     availableFlag[row][j] = false;
                 }
-                weight += wcs[column][row];
+                weight += wcs[row][column];
             }
         }