Skip to content

Commit 53b2215

Browse files
committedJul 26, 2021
added traces and reachable constructs to debloat task
1 parent 553ecf1 commit 53b2215

File tree

3 files changed

+74
-75
lines changed

3 files changed

+74
-75
lines changed
 

‎lang-java/src/main/java/org/eclipse/steady/java/tasks/JavaDebloatTask.java

+55-74
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@
3232

3333
import org.apache.logging.log4j.Logger;
3434
import org.eclipse.steady.Construct;
35+
import org.eclipse.steady.backend.BackendConnector;
3536
import org.eclipse.steady.core.util.CoreConfiguration;
3637
import org.eclipse.steady.goals.GoalConfigurationException;
3738
import org.eclipse.steady.goals.GoalExecutionException;
3839
import org.eclipse.steady.java.JarAnalyzer;
3940
import org.eclipse.steady.shared.enums.GoalClient;
4041
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;
4145
import org.eclipse.steady.shared.util.StringList;
4246
import org.eclipse.steady.shared.util.StringUtil;
4347
import 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
}

‎lang/src/main/java/org/eclipse/steady/goals/DebloatGoal.java

+3
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ protected void executeTasks() throws Exception {
6969
final ServiceLoader<DebloatTask> loader = ServiceLoader.load(DebloatTask.class);
7070
for (DebloatTask t : loader) {
7171
try {
72+
7273
// Configure
7374
t.setApplication(a);
75+
t.setTraces(BackendConnector.getInstance().getAppTraces(this.getGoalContext(), a));
76+
t.setReachableConstructIds(BackendConnector.getInstance().getAppDependencies(this.getGoalContext(), a));
7477
t.setSearchPaths(this.getAppPaths());
7578
t.setGoalClient(this.getGoalClient());
7679
t.setKnownDependencies(this.getKnownDependencies());

‎lang/src/main/java/org/eclipse/steady/tasks/DebloatTask.java

+16-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
import java.util.Set;
2222

23-
import org.eclipse.steady.Construct;
23+
import org.eclipse.steady.shared.json.model.ConstructId;
24+
import org.eclipse.steady.shared.json.model.Dependency;
2425

2526
/**
2627
* Methods required to
@@ -35,6 +36,20 @@ public interface DebloatTask extends Task {
3536
// * @return a {@link org.eclipse.steady.shared.json.model.Application} object.
3637
// */
3738
// public Set<Construct> getNeededConstructs();
39+
40+
/**
41+
* Sets the traced constructs to be used as starting point for debloating the {@link Application}
42+
* (traces resulting from the dynamic analysis)
43+
*
44+
*/
45+
public void setTraces(Set<ConstructId> _traces);
46+
47+
/**
48+
* Sets the reachable constructs to be used as starting point for debloating the {@link Application}
49+
* (resulting from the static analysis)
50+
*
51+
*/
52+
public void setReachableConstructIds(Set<Dependency> _deps);
3853

3954

4055
}

0 commit comments

Comments
 (0)
Please sign in to comment.