11package de .fraunhofer .iem .swan .features .code ;
22
3- import java .util .Arrays ;
4- import java .util .HashMap ;
5- import java .util .HashSet ;
6- import java .util .Map ;
7- import java .util .Set ;
8-
3+ import java .io .File ;
4+ import java .util .*;
95import de .fraunhofer .iem .swan .data .Category ;
106import de .fraunhofer .iem .swan .features .code .type .*;
117import de .fraunhofer .iem .swan .features .code .type .MethodClassModifierFeature .ClassModifier ;
1915 * @author Lisa Nguyen Quang Do, Goran Piskachev
2016 */
2117
22- public class FeatureHandler {
18+ public class CodeFeatureHandler {
2319
2420 private final String cp ;
2521 private Map <Category , Set <IFeature >> featuresMap ;
26- private static final Logger logger = LoggerFactory .getLogger (FeatureHandler .class );
27-
28-
29- public FeatureHandler (String cp ) {
30- this .cp = cp ;
31- }
32-
22+ private static final Logger logger = LoggerFactory .getLogger (CodeFeatureHandler .class );
3323 public Map <Category , Set <IFeature >> features () {
3424 return featuresMap ;
3525 }
3626
37- private void addFeature (IFeature feature ,
38- Set <Category > categoriesForFeature ) {
27+ public CodeFeatureHandler (String trainClasspath , String testClasspath ) {
28+ StringJoiner joiner = new StringJoiner (File .pathSeparator );
29+ joiner .add (trainClasspath ).add (testClasspath );
30+ this .cp = joiner .toString ();
31+ }
32+
33+ private void addFeature (IFeature feature , Set <Category > categoriesForFeature ) {
3934 for (Category category : categoriesForFeature ) {
4035 Set <IFeature > typeFeatures = featuresMap .get (category );
4136 typeFeatures .add (feature );
@@ -48,9 +43,11 @@ private void addFeature(IFeature feature,
4843 *
4944 */
5045 public void initializeFeatures () {
51- featuresMap = new HashMap <Category , Set <IFeature >>();
46+
47+ featuresMap = new HashMap <>();
48+
5249 for (Category category : Category .values ())
53- featuresMap .put (category , new HashSet <IFeature >());
50+ featuresMap .put (category , new HashSet <>());
5451
5552 // Implicit method.
5653 IFeature isImplicitMethod = new IsImplicitMethod ();
@@ -59,16 +56,17 @@ public void initializeFeatures() {
5956 new HashSet <>(Arrays .asList (Category .SOURCE , Category .SINK ,
6057 Category .SANITIZER , Category .AUTHENTICATION_NEUTRAL ,
6158 Category .AUTHENTICATION_TO_HIGH , Category .AUTHENTICATION_TO_LOW ,
62- Category .CWE089 , Category .CWE862 , Category .CWE863 , Category .CWE078 , Category .CWE306 , Category . CWE079 , Category . CWE601 ,
63- Category .NONE , Category .RELEVANT )));
59+ Category .CWE089 , Category .CWE862 , Category .CWE863 , Category .CWE078 , Category .CWE306 ,
60+ Category .CWE079 , Category . CWE601 , Category . NONE , Category .RELEVANT )));
6461
6562 // Method in anonymous class.
6663 IFeature anonymousClass = new MethodAnonymousClassFeature (true );
6764 ((WeightedFeature ) anonymousClass ).setWeight (8 );
6865 addFeature (anonymousClass ,
69- new HashSet <>(Arrays .asList (Category .SOURCE , Category .SINK , Category .NONE , Category .RELEVANT , Category .CWE078 , Category .CWE079 , Category .CWE089 , Category .CWE306 , Category .CWE862 , Category .CWE863 , Category .CWE601 )));
66+ new HashSet <>(Arrays .asList (Category .SOURCE , Category .SINK , Category .NONE , Category .RELEVANT ,
67+ Category .CWE078 , Category .CWE079 , Category .CWE089 , Category .CWE306 , Category .CWE862 ,
68+ Category .CWE863 , Category .CWE601 )));
7069
71-
7270 IFeature classNameContainsSaniti = new MethodClassContainsNameFeature (
7371 "Saniti" );
7472 ((WeightedFeature ) classNameContainsSaniti ).setWeight (2 );
@@ -87,7 +85,6 @@ public void initializeFeatures() {
8785 addFeature (classNameContainsEscape ,
8886 new HashSet <>(Arrays .asList (Category .SANITIZER , Category .NONE , Category .RELEVANT )));
8987
90-
9188 IFeature classNameContainsValid = new MethodClassContainsNameFeature (
9289 "Valid" );
9390 ((WeightedFeature ) classNameContainsValid ).setWeight (-13 );
@@ -157,7 +154,8 @@ public void initializeFeatures() {
157154 IFeature classNameContainsWeb = new MethodClassContainsNameFeature ("web" );
158155 ((WeightedFeature ) classNameContainsWeb ).setWeight (15 );
159156 addFeature (classNameContainsWeb , new HashSet <>(
160- Arrays .asList (Category .SOURCE , Category .SINK , Category .NONE , Category .RELEVANT , Category .CWE079 , Category .CWE601 )));
157+ Arrays .asList (Category .SOURCE , Category .SINK , Category .NONE , Category .RELEVANT , Category .CWE079 ,
158+ Category .CWE601 )));
161159
162160 IFeature classNameContainsNet = new MethodClassContainsNameFeature (".net." );
163161 ((WeightedFeature ) classNameContainsNet ).setWeight (9 );
@@ -186,7 +184,8 @@ public void initializeFeatures() {
186184 "Input" );
187185 ((WeightedFeature ) classNameContainsInput ).setWeight (5 );
188186 addFeature (classNameContainsInput ,
189- new HashSet <>(Arrays .asList (Category .SINK , Category .CWE079 , Category .CWE078 , Category .CWE089 , Category .NONE , Category .RELEVANT )));
187+ new HashSet <>(Arrays .asList (Category .SINK , Category .CWE079 , Category .CWE078 , Category .CWE089 ,
188+ Category .NONE , Category .RELEVANT )));
190189
191190 IFeature classNameContainsDatabase = new MethodClassContainsNameFeature (
192191 "database" );
@@ -217,7 +216,6 @@ public void initializeFeatures() {
217216 ((WeightedFeature ) classNameContainsProcess ).setWeight (13 );
218217 addFeature (classNameContainsProcess ,
219218 new HashSet <>(Arrays .asList (Category .CWE078 , Category .NONE , Category .RELEVANT )));
220-
221219
222220 IFeature classNameContainsRuntime = new MethodClassContainsNameFeature (
223221 "runtime" );
@@ -235,7 +233,6 @@ public void initializeFeatures() {
235233 addFeature (classNameContainsJdbc ,
236234 new HashSet <>(Arrays .asList (Category .SINK ,
237235 Category .CWE089 , Category .NONE , Category .RELEVANT )));
238-
239236
240237 IFeature classNameContainsHtml = new MethodClassContainsNameFeature (
241238 "Html" );
@@ -258,7 +255,6 @@ public void initializeFeatures() {
258255 ((WeightedFeature ) classNameContainsHttp ).setWeight (-8 );
259256 addFeature (classNameContainsHttp , new HashSet <>(
260257 Arrays .asList (Category .CWE601 , Category .NONE , Category .RELEVANT )));
261-
262258
263259 IFeature classNameContainsUrl = new MethodClassContainsNameFeature (
264260 "url" );
@@ -777,7 +773,6 @@ public void initializeFeatures() {
777773 ((WeightedFeature ) methodNameContainsSaniti ).setWeight (-3 );
778774 addFeature (methodNameContainsSaniti ,
779775 new HashSet <>(Arrays .asList (Category .SANITIZER , Category .CWE078 , Category .CWE079 , Category .CWE089 , Category .NONE , Category .RELEVANT )));
780-
781776
782777 IFeature methodNameContainsEscape = new MethodNameContainsFeature ("escape" ,
783778 "unescape" );
@@ -909,7 +904,6 @@ public void initializeFeatures() {
909904 addFeature (nameContainsRequest ,
910905 new HashSet <>(Arrays .asList (Category .SOURCE , Category .NONE , Category .RELEVANT )));
911906
912-
913907 IFeature nameContainsCreate = new MethodNameContainsFeature ("creat" );
914908 ((WeightedFeature ) nameContainsCreate ).setWeight (6 );
915909 addFeature (nameContainsCreate ,
@@ -1118,7 +1112,6 @@ public void initializeFeatures() {
11181112 new HashSet <>(Arrays .asList (Category .SANITIZER , Category .NONE , Category .RELEVANT )));
11191113
11201114 // Parameter to sink.
1121-
11221115 IFeature paramToSinkSetWrit = new ParameterToSinkFeature (cp , "writ" );
11231116 ((WeightedFeature ) paramToSinkSetWrit ).setWeight (-4 );
11241117 addFeature (paramToSinkSetWrit ,
@@ -1149,7 +1142,6 @@ public void initializeFeatures() {
11491142 addFeature (paramToSinkSetPut ,
11501143 new HashSet <>(Arrays .asList (Category .SINK , Category .NONE , Category .RELEVANT )));
11511144
1152-
11531145 IFeature paramToSinkSetAdd = new ParameterToSinkFeature (cp , "log" );
11541146 ((WeightedFeature ) paramToSinkSetAdd ).setWeight (-11 );
11551147 addFeature (paramToSinkSetAdd ,
@@ -1204,7 +1196,6 @@ public void initializeFeatures() {
12041196 addFeature (returnContainsNode ,
12051197 new HashSet <>(Arrays .asList (Category .SOURCE , Category .CWE079 , Category .NONE , Category .RELEVANT )));
12061198
1207-
12081199 IFeature returnContainsUser = new ReturnTypeContainsNameFeature (cp , "User" );
12091200 ((WeightedFeature ) returnContainsUser ).setWeight (-17 );
12101201 addFeature (returnContainsUser ,
@@ -1298,7 +1289,7 @@ public void initializeFeatures() {
12981289 getFeaturesSize ();
12991290 }
13001291
1301- private int getFeaturesSize () {
1292+ private void getFeaturesSize () {
13021293 int count = 0 ;
13031294
13041295 HashMap <String , Integer > featuresCount = new HashMap ();
@@ -1308,7 +1299,6 @@ private int getFeaturesSize() {
13081299 featuresCount .put (c .toString (), features );
13091300 count += features ;
13101301 }
1311- logger .info ("{} Features, distribution={}" , count , featuresCount );
1312- return count ;
1302+ logger .info ("{} Features, distribution={}" , count , featuresCount );
13131303 }
13141304}
0 commit comments