3232
3333import org .apache .logging .log4j .Logger ;
3434import org .eclipse .steady .Construct ;
35+ import org .eclipse .steady .backend .BackendConnector ;
3536import org .eclipse .steady .core .util .CoreConfiguration ;
3637import org .eclipse .steady .goals .GoalConfigurationException ;
3738import org .eclipse .steady .goals .GoalExecutionException ;
3839import org .eclipse .steady .java .JarAnalyzer ;
3940import org .eclipse .steady .shared .enums .GoalClient ;
4041import 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 ;
4145import org .eclipse .steady .shared .util .StringList ;
4246import org .eclipse .steady .shared .util .StringUtil ;
4347import org .eclipse .steady .shared .util .VulasConfiguration ;
@@ -56,10 +60,10 @@ public class JavaDebloatTask extends AbstractTask implements DebloatTask {
5660
5761 private static final String [] EXT_FILTER = new String [] {"jar" , "war" , "class" , "java" , "aar" };
5862
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+
6367 private static final List <GoalClient > pluginGoalClients =
6468 Arrays .asList (GoalClient .MAVEN_PLUGIN , GoalClient .GRADLE_PLUGIN );
6569
@@ -70,72 +74,6 @@ public Set<ProgrammingLanguage> getLanguage() {
7074 Arrays .asList (new ProgrammingLanguage [] {ProgrammingLanguage .JAVA }));
7175 }
7276
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- }
13977
14078 /** {@inheritDoc} */
14179 @ Override
@@ -170,6 +108,26 @@ public void execute() throws GoalExecutionException {
170108 e .printStackTrace ();
171109 }
172110
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+
173131 final Set <Clazz > needed = new HashSet <Clazz >();
174132 final Set <Clazz > removable = cp .getClazzes ();
175133 for (ClazzpathUnit u : app ) {
@@ -178,20 +136,43 @@ public void execute() throws GoalExecutionException {
178136 needed .addAll (u .getClazzes ());
179137 needed .addAll (u .getTransitiveDependencies ());
180138 }
139+
140+
181141
182142 log .info ("Needed [" + needed .size ()+"] classes" );
183143 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+
187155 try {
188156 FileWriter writer =new FileWriter ("needed.txt" );
189157 for (Clazz c : needed ) {
190158 writer .write (c + System .lineSeparator ());
191159 }
192160 writer .close ();
193161 } 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 ;
195176 }
196177
197178}
0 commit comments