32
32
33
33
import org .apache .logging .log4j .Logger ;
34
34
import org .eclipse .steady .Construct ;
35
+ import org .eclipse .steady .backend .BackendConnector ;
35
36
import org .eclipse .steady .core .util .CoreConfiguration ;
36
37
import org .eclipse .steady .goals .GoalConfigurationException ;
37
38
import org .eclipse .steady .goals .GoalExecutionException ;
38
39
import org .eclipse .steady .java .JarAnalyzer ;
39
40
import org .eclipse .steady .shared .enums .GoalClient ;
40
41
import org .eclipse .steady .shared .enums .ProgrammingLanguage ;
42
+ import org .eclipse .steady .shared .json .model .ConstructId ;
43
+ import org .eclipse .steady .shared .json .model .Dependency ;
44
+ import org .eclipse .steady .shared .json .model .Trace ;
41
45
import org .eclipse .steady .shared .util .StringList ;
42
46
import org .eclipse .steady .shared .util .StringUtil ;
43
47
import org .eclipse .steady .shared .util .VulasConfiguration ;
@@ -56,10 +60,10 @@ public class JavaDebloatTask extends AbstractTask implements DebloatTask {
56
60
57
61
private static final String [] EXT_FILTER = new String [] {"jar" , "war" , "class" , "java" , "aar" };
58
62
59
- private String [] appPrefixes = null ;
60
-
61
- private StringList appJarNames = null ;
62
-
63
+ private Set < ConstructId > traces = null ;
64
+
65
+ private Set < Dependency > reachableConstructIds = null ;
66
+
63
67
private static final List <GoalClient > pluginGoalClients =
64
68
Arrays .asList (GoalClient .MAVEN_PLUGIN , GoalClient .GRADLE_PLUGIN );
65
69
@@ -70,72 +74,6 @@ public Set<ProgrammingLanguage> getLanguage() {
70
74
Arrays .asList (new ProgrammingLanguage [] {ProgrammingLanguage .JAVA }));
71
75
}
72
76
73
- /**
74
- * Returns true if the configuration setting {@link CoreConfiguration#APP_PREFIXES} shall be considered, false otherwise.
75
- */
76
- private final boolean useAppPrefixes () {
77
- return this .appPrefixes != null && !this .isOneOfGoalClients (pluginGoalClients );
78
- }
79
-
80
- /**
81
- * Returns true if the configuration setting {@link CoreConfiguration#APP_PREFIXES} shall be considered, false otherwise.
82
- */
83
- private final boolean useAppJarNames () {
84
- return this .appJarNames != null && !this .isOneOfGoalClients (pluginGoalClients );
85
- }
86
-
87
- /** {@inheritDoc} */
88
- @ Override
89
- public void configure (VulasConfiguration _cfg ) throws GoalConfigurationException {
90
- super .configure (_cfg );
91
-
92
- // App constructs identified using package prefixes
93
- this .appPrefixes = _cfg .getStringArray (CoreConfiguration .APP_PREFIXES , null );
94
-
95
- // Print warning message in case the setting is used as part of the Maven plugin
96
- if (this .appPrefixes != null && this .isOneOfGoalClients (pluginGoalClients )) {
97
- log .warn (
98
- "Configuration setting ["
99
- + CoreConfiguration .APP_PREFIXES
100
- + "] ignored when running the goal as Maven plugin" );
101
- this .appPrefixes = null ;
102
- }
103
-
104
- // App constructs identified using JAR file name patterns (regex)
105
- final String [] app_jar_names = _cfg .getStringArray (CoreConfiguration .APP_JAR_NAMES , null );
106
- if (app_jar_names != null ) {
107
- // Print warning message in case the setting is used as part of the Maven plugin
108
- if (this .isOneOfGoalClients (pluginGoalClients )) {
109
- log .warn (
110
- "Configuration setting ["
111
- + CoreConfiguration .APP_JAR_NAMES
112
- + "] ignored when running the goal as Maven plugin" );
113
- this .appJarNames = null ;
114
- } else {
115
- this .appJarNames = new StringList ();
116
- this .appJarNames .addAll (app_jar_names );
117
- }
118
- }
119
-
120
- // CLI: Only one of appPrefixes and appJarNames can be used
121
- if (!this .isOneOfGoalClients (pluginGoalClients )) {
122
- if (this .appPrefixes != null && this .appJarNames != null ) {
123
- throw new GoalConfigurationException (
124
- "Exactly one of the configuration settings ["
125
- + CoreConfiguration .APP_PREFIXES
126
- + "] and ["
127
- + CoreConfiguration .APP_JAR_NAMES
128
- + "] must be set" );
129
- } else if (this .appPrefixes == null && this .appJarNames == null ) {
130
- throw new GoalConfigurationException (
131
- "Exactly one of the configuration settings ["
132
- + CoreConfiguration .APP_PREFIXES
133
- + "] and ["
134
- + CoreConfiguration .APP_JAR_NAMES
135
- + "] must be set" );
136
- }
137
- }
138
- }
139
77
140
78
/** {@inheritDoc} */
141
79
@ Override
@@ -170,6 +108,26 @@ public void execute() throws GoalExecutionException {
170
108
e .printStackTrace ();
171
109
}
172
110
111
+ log .info ("App classpathUnit [" + app .size () +"]" );
112
+ for (ConstructId t : traces ) {
113
+ Clazz c = cp .getClazz (t .getQname ().split ("(" )[0 ]);
114
+ app .addAll (c .getClazzpathUnits ());
115
+ }
116
+ log .info ("App classpathUnit with traces [" + app .size () +"]" );
117
+
118
+ for (Dependency d : reachableConstructIds ) {
119
+ if (d .getReachableConstructIds ()!=null ) {
120
+ for (ConstructId c : d .getReachableConstructIds ()) {
121
+ Clazz cl = cp .getClazz (c .getQname ().split ("(" )[0 ]);
122
+ app .addAll (cl .getClazzpathUnits ());
123
+ }
124
+ }
125
+ }
126
+ log .info ("App classpathUnit with reachable constructs [" + app .size () +"]" );
127
+
128
+ log .info ("Classpath classpathUnits [" + cp .getUnits ().length +"]" );
129
+
130
+
173
131
final Set <Clazz > needed = new HashSet <Clazz >();
174
132
final Set <Clazz > removable = cp .getClazzes ();
175
133
for (ClazzpathUnit u : app ) {
@@ -178,20 +136,43 @@ public void execute() throws GoalExecutionException {
178
136
needed .addAll (u .getClazzes ());
179
137
needed .addAll (u .getTransitiveDependencies ());
180
138
}
139
+
140
+
181
141
182
142
log .info ("Needed [" + needed .size ()+"] classes" );
183
143
log .info ("Removable [" + removable .size ()+"] classes" );
184
- for (Clazz clazz : removable ) {
185
- System .out .println ("class " + clazz + " is not required" );
186
- }
144
+
145
+ try {
146
+ FileWriter writer =new FileWriter ("removable.txt" );
147
+ for (Clazz clazz : removable ) {
148
+ writer .write (clazz + System .lineSeparator ());
149
+ }
150
+ writer .close ();
151
+ } catch (IOException e ){// TODO Auto-generated catch block
152
+ e .printStackTrace ();
153
+ }
154
+
187
155
try {
188
156
FileWriter writer =new FileWriter ("needed.txt" );
189
157
for (Clazz c : needed ) {
190
158
writer .write (c + System .lineSeparator ());
191
159
}
192
160
writer .close ();
193
161
} catch (IOException e ){// TODO Auto-generated catch block
194
- e .printStackTrace ();}
162
+ e .printStackTrace ();
163
+ }
164
+ }
165
+
166
+ /** {@inheritDoc} */
167
+ @ Override
168
+ public void setTraces (Set <ConstructId > _traces ){
169
+ this .traces = _traces ;
170
+ }
171
+
172
+ /** {@inheritDoc} */
173
+ @ Override
174
+ public void setReachableConstructIds (Set <Dependency > _deps ){
175
+ this .reachableConstructIds = _deps ;
195
176
}
196
177
197
178
}
0 commit comments