Skip to content

Commit e36a5d3

Browse files
authored
Merge pull request #184 from ponder-lab/entry_points_txt
Clean up entry point file code.
2 parents 3a10355 + 6d449dd commit e36a5d3

File tree

4 files changed

+108
-43
lines changed

4 files changed

+108
-43
lines changed

edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis/StreamAnalyzer.java

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.HashSet;
1212
import java.util.Iterator;
1313
import java.util.Map;
14+
import java.util.Optional;
1415
import java.util.Scanner;
1516
import java.util.Set;
1617
import java.util.logging.Level;
@@ -42,6 +43,7 @@
4243
import com.ibm.wala.util.scope.JUnitEntryPoints;
4344

4445
import edu.cuny.hunter.streamrefactoring.core.utils.LoggerNames;
46+
import edu.cuny.hunter.streamrefactoring.core.utils.TimeCollector;
4547
import edu.cuny.hunter.streamrefactoring.core.wala.EclipseProjectAnalysisEngine;
4648

4749
@SuppressWarnings("restriction")
@@ -51,7 +53,7 @@ public class StreamAnalyzer extends ASTVisitor {
5153

5254
private static final int N_FOR_STREAMS_DEFAULT = 2;
5355

54-
private static final String ENTRY_POINT_FILE = "entry_points.txt";
56+
private static final String ENTRY_POINT_FILENAME = "entry_points.txt";
5557

5658
private static void addImplicitEntryPoints(Collection<Entrypoint> target, Iterable<Entrypoint> source) {
5759
for (Entrypoint implicitEntryPoint : source)
@@ -117,11 +119,24 @@ public StreamAnalyzer(boolean visitDocTags, int nForStreams, boolean findImplici
117119

118120
/**
119121
* Analyzes this {@link StreamAnalyzer}'s streams.
120-
*
121-
* @return {@link Map} of project's analyzed along with the entry points used.
122+
*
123+
* @return A {@link Map} of project's analyzed along with the entry points used.
124+
* @see #analyze(Optional).
122125
*/
123126
public Map<IJavaProject, Collection<Entrypoint>> analyze() throws CoreException {
124-
LOGGER.info(() -> "Using N = " + this.getNForStreams());
127+
return this.analyze(Optional.empty());
128+
}
129+
130+
/**
131+
* Analyzes this {@link StreamAnalyzer}'s streams.
132+
*
133+
* @param collector
134+
* To exclude from the time certain parts of the analysis.
135+
* @return A {@link Map} of project's analyzed along with the entry points used.
136+
* @see #analyze().
137+
*/
138+
public Map<IJavaProject, Collection<Entrypoint>> analyze(Optional<TimeCollector> collector) throws CoreException {
139+
LOGGER.info(() -> "Using N = " + this.getNForStreams() + ".");
125140

126141
Map<IJavaProject, Collection<Entrypoint>> ret = new HashMap<>();
127142

@@ -132,6 +147,9 @@ public Map<IJavaProject, Collection<Entrypoint>> analyze() throws CoreException
132147
// process each project.
133148
for (IJavaProject project : projectToStreams.keySet()) {
134149
// create the analysis engine for the project.
150+
// exclude from the analysis because the IR will be built here.
151+
152+
collector.ifPresent(TimeCollector::start);
135153
EclipseProjectAnalysisEngine<InstanceKey> engine = null;
136154
try {
137155
engine = new EclipseProjectAnalysisEngine<>(project, this.getNForStreams());
@@ -140,11 +158,12 @@ public Map<IJavaProject, Collection<Entrypoint>> analyze() throws CoreException
140158
LOGGER.log(Level.SEVERE, "Could not create analysis engine for: " + project.getElementName(), e);
141159
throw new RuntimeException(e);
142160
}
161+
collector.ifPresent(TimeCollector::stop);
143162

144163
// build the call graph for the project.
145164
Collection<Entrypoint> entryPoints = null;
146165
try {
147-
entryPoints = this.buildCallGraph(engine);
166+
entryPoints = this.buildCallGraph(engine, collector);
148167
} catch (IOException | CoreException | CancelException e) {
149168
LOGGER.log(Level.SEVERE,
150169
"Exception encountered while building call graph for: " + project.getElementName() + ".", e);
@@ -213,18 +232,27 @@ public Map<IJavaProject, Collection<Entrypoint>> analyze() throws CoreException
213232
* @param engine
214233
* The EclipseProjectAnalysisEngine for which to build the call
215234
* graph.
235+
* @param collector
236+
* A {@link TimeCollector} to exclude entry point finding.
216237
* @return The {@link Entrypoint}s used in building the {@link CallGraph}.
217238
*/
218-
protected Collection<Entrypoint> buildCallGraph(EclipseProjectAnalysisEngine<InstanceKey> engine)
239+
protected Collection<Entrypoint> buildCallGraph(EclipseProjectAnalysisEngine<InstanceKey> engine,
240+
Optional<TimeCollector> collector)
219241
throws IOException, CoreException, CallGraphBuilderCancelException, CancelException {
220242
// if we haven't built the call graph yet.
221243
if (!this.enginesWithBuiltCallGraphsToEntrypointsUsed.keySet().contains(engine)) {
222-
244+
// find entry points (but exclude it from the time).
245+
collector.ifPresent(TimeCollector::start);
223246
Set<Entrypoint> entryPoints;
247+
224248
// find the entry_points.txt in the project directory
225-
File entryPointFile = getEntryPointsFile(engine.getProject().getResource().getLocation(), ENTRY_POINT_FILE);
249+
File entryPointFile = getEntryPointsFile(engine.getProject().getResource().getLocation(),
250+
ENTRY_POINT_FILENAME);
251+
252+
// if the file was found,
226253
if (entryPointFile != null) {
227-
// find explicit entry points from entry_points.txt
254+
// find explicit entry points from entry_points.txt. Ignore the explicit
255+
// (annotation-based) entry points.
228256
entryPoints = findEntryPointsFromFile(engine.getClassHierarchy(), entryPointFile);
229257
entryPoints.forEach(ep -> LOGGER.info(() -> "Adding explicit entry point from file: " + ep));
230258
} else {
@@ -233,7 +261,7 @@ protected Collection<Entrypoint> buildCallGraph(EclipseProjectAnalysisEngine<Ins
233261
entryPoints.forEach(ep -> LOGGER.info(() -> "Adding explicit entry point: " + ep));
234262
}
235263

236-
if (this.findImplicitEntryPoints) {
264+
if (this.shouldFindImplicitEntryPoints()) {
237265
// also find implicit entry points.
238266
Iterable<Entrypoint> mainEntrypoints = makeMainEntrypoints(engine.getClassHierarchy().getScope(),
239267
engine.getClassHierarchy());
@@ -242,15 +270,15 @@ protected Collection<Entrypoint> buildCallGraph(EclipseProjectAnalysisEngine<Ins
242270
addImplicitEntryPoints(entryPoints, mainEntrypoints);
243271
}
244272

245-
if (this.findImplicitTestEntryPoints) {
273+
if (this.shouldFindImplicitTestEntryPoints()) {
246274
// try to find test entry points.
247275
Iterable<Entrypoint> jUnitEntryPoints = JUnitEntryPoints.make(engine.getClassHierarchy());
248276

249277
// add them as well.
250278
addImplicitEntryPoints(entryPoints, jUnitEntryPoints);
251279
}
252280

253-
if (this.findImplicitBenchmarkEntryPoints) {
281+
if (this.shouldFindImplicitBenchmarkEntryPoints()) {
254282
// try to find benchmark entry points.
255283
Set<Entrypoint> benchmarkEntryPoints = Util.findBenchmarkEntryPoints(engine.getClassHierarchy());
256284

@@ -263,6 +291,8 @@ protected Collection<Entrypoint> buildCallGraph(EclipseProjectAnalysisEngine<Ins
263291
return entryPoints;
264292
}
265293

294+
collector.ifPresent(TimeCollector::stop);
295+
266296
// set options.
267297
AnalysisOptions options = engine.getDefaultOptions(entryPoints);
268298
// Turn off reflection analysis.
@@ -306,20 +336,21 @@ private static Set<Entrypoint> findEntryPointsFromFile(IClassHierarchy classHier
306336
}
307337

308338
/**
309-
* Search entry_points.txt in project directory recursively
339+
* Search entry_points.txt in project directory recursively.
310340
*
311-
* @param directory:
312-
* project directory
313-
* @param fileName:
314-
* the target file
315-
* @return null: the file does not exist / file: find the file
341+
* @param directory
342+
* Project directory.
343+
* @param fileName
344+
* The target file.
345+
* @return null if the file does not exist and file if we found the file.
316346
*/
317347
private static File getEntryPointsFile(IPath directory, String fileName) {
318-
// If file does not exist, find the file in upper level
348+
// If file does not exist, find the file in upper level.
319349
Path directoryPath = Paths.get(directory.toString());
350+
320351
File file;
321352
do {
322-
file = new File(directoryPath.resolve(ENTRY_POINT_FILE).toString());
353+
file = new File(directoryPath.resolve(ENTRY_POINT_FILENAME).toString());
323354
directoryPath = directoryPath.getParent();
324355
} while (!file.exists() && directoryPath != null);
325356

@@ -337,10 +368,26 @@ public void setFindImplicitEntryPoints(boolean findImplicitEntryPoints) {
337368
this.findImplicitEntryPoints = findImplicitEntryPoints;
338369
}
339370

371+
public boolean shouldFindImplicitEntryPoints() {
372+
return findImplicitEntryPoints;
373+
}
374+
340375
public void setFindImplicitTestEntryPoints(boolean findImplicitTestEntryPoints) {
341376
this.findImplicitTestEntryPoints = findImplicitTestEntryPoints;
342377
}
343378

379+
public boolean shouldFindImplicitTestEntryPoints() {
380+
return findImplicitTestEntryPoints;
381+
}
382+
383+
public boolean shouldFindImplicitBenchmarkEntryPoints() {
384+
return findImplicitBenchmarkEntryPoints;
385+
}
386+
387+
public void setFindImplicitBenchmarkEntryPoints(boolean findImplicitBenchmarkEntryPoints) {
388+
this.findImplicitBenchmarkEntryPoints = findImplicitBenchmarkEntryPoints;
389+
}
390+
344391
/**
345392
* @see org.eclipse.jdt.core.dom.ASTVisitor#visit(org.eclipse.jdt.core.dom.MethodInvocation)
346393
*/

edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/refactorings/ConvertToParallelStreamRefactoringProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public RefactoringStatus checkFinalConditions(final IProgressMonitor monitor, fi
240240
}
241241

242242
// analyze and set entry points.
243-
this.projectToEntryPoints = analyzer.analyze();
243+
this.projectToEntryPoints = analyzer.analyze(Optional.of(this.getExcludedTimeCollector()));
244244

245245
// map empty set to unprocessed projects.
246246
for (IJavaProject project : this.getJavaProjects())

edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/utils/TimeCollector.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ public class TimeCollector {
1111

1212
private long collectedTime;
1313
private long start;
14+
private boolean started;
1415

1516
public void start() {
17+
assert !started : "Time colletor is already started.";
18+
started = true;
19+
1620
start = System.currentTimeMillis();
1721
}
1822

1923
public void stop() {
24+
assert started : "Trying to stop a time collector that isn't started.";
25+
started = false;
26+
2027
final long elapsed = System.currentTimeMillis() - start;
2128
collectedTime += elapsed;
2229
}
@@ -26,6 +33,8 @@ public long getCollectedTime() {
2633
}
2734

2835
public void clear() {
36+
assert !started : "Shouldn't clear a running time collector.";
37+
2938
collectedTime = 0;
3039
}
3140
}

edu.cuny.hunter.streamrefactoring.tests/test cases/edu/cuny/hunter/streamrefactoring/ui/tests/ConvertStreamToParallelRefactoringTest.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
*
3-
*/
41
package edu.cuny.hunter.streamrefactoring.ui.tests;
52

63
import java.io.File;
@@ -75,7 +72,7 @@ public class ConvertStreamToParallelRefactoringTest extends RefactoringTest {
7572

7673
private static final int RETRY_DELAY = 1000;
7774

78-
private static final String ENTRY_POINT_FILE = "entry_points.txt";
75+
private static final String ENTRY_POINT_FILENAME = "entry_points.txt";
7976

8077
private static final int N_TO_USE_FOR_STREAMS_DEFAULT = 2;
8178

@@ -135,49 +132,61 @@ public static Test setUpTest(Test test) {
135132
}
136133

137134
/**
138-
* @return Path: an absolute path of entry_points.txt in the project directory
135+
* @return an absolute path of entry_points.txt in the project directory.
139136
*/
140-
private Path getAbsoluteProjectPath() {
141-
return this.getAbsolutePath(this.getTestPath() + this.getName()).resolve(ENTRY_POINT_FILE);
137+
private Path getEntryPointFileProjectSourcePath() {
138+
return getAbsolutePath(this.getTestPath() + this.getName()).resolve(ENTRY_POINT_FILENAME);
142139
}
143140

144141
/**
145-
* @return Path: an absolute path of entry_points.txt in the project directory
146-
* of junit workspace
142+
* @return The {@link Path} of where the entry points file should be copied to
143+
* for the current project under test.
147144
*/
148-
private Path getDestinationProjectPath() {
149-
return getDestinationPath(this.getPackageP().getJavaProject());
145+
private Path getEntryPointFileProjectDestinationPath() {
146+
return getEntryPointFileDestinationPath(this.getPackageP().getJavaProject());
150147
}
151148

152149
/**
153-
* @return Path: an absolute path of entry_points.txt in the junit workspace
150+
* @return The {@link Path} of where the entry_points.txt file should be copied
151+
* to in the junit workspace.
154152
*/
155-
private Path getDestinationWorkSpacePath() {
156-
return getDestinationPath(this.getPackageP().getJavaProject().getParent());
153+
@SuppressWarnings("unused")
154+
private Path getDestinationWorkspacePath() {
155+
return getEntryPointFileDestinationPath(this.getPackageP().getJavaProject().getParent());
157156
}
158157

159-
private Path getDestinationPath(IJavaElement element) {
160-
return Paths.get(element.getResource().getLocation().toString() + File.separator + ENTRY_POINT_FILE);
158+
/**
159+
* Returns the path of where the entry points file should be copied relative to
160+
* the given {@link IJavaElement}.
161+
*
162+
* @param element
163+
* The {@link IJavaElement} in question.
164+
* @return The {@link Path} where the entry points file should be copied
165+
* relative to the given {@link IJavaElement}.
166+
*/
167+
private static Path getEntryPointFileDestinationPath(IJavaElement element) {
168+
return Paths.get(element.getResource().getLocation().toString() + File.separator + ENTRY_POINT_FILENAME);
161169
}
162170

163171
@Override
164172
protected void setUp() throws Exception {
165173
super.setUp();
166174

167175
// this is the source path.
168-
Path absoluteProjectPath = getAbsoluteProjectPath();
176+
Path entryPointFileProjectSourcePath = getEntryPointFileProjectSourcePath();
177+
Path entryPointFileProjectDestinationPath = getEntryPointFileProjectDestinationPath();
169178

170179
// TODO: we also need to copy entry_points.txt to workspace directory here
171180
// something like copyEntryPointFile(absoluteProjectPath,
172181
// getDestinationWorkSpacePath())
173-
if (copyEntryPointFile(absoluteProjectPath, getDestinationProjectPath()))
174-
LOGGER.info(() -> "Copy entry_points.txt successfully");
182+
if (copyEntryPointFile(entryPointFileProjectSourcePath, entryPointFileProjectDestinationPath))
183+
LOGGER.info("Copied " + ENTRY_POINT_FILENAME + " successfully.");
175184
else
176-
LOGGER.info(() -> "entry_points.txt does not exist");
185+
LOGGER.info(ENTRY_POINT_FILENAME + " does not exist.");
177186
}
178187

179188
/**
180-
* Copy entry_points.txt from cuurent directory to the corresponding directory
189+
* Copy entry_points.txt from current directory to the corresponding directory
181190
* in junit-workspace
182191
*
183192
* @return true: copy successfully / false: the source file does not exist
@@ -429,7 +438,7 @@ protected void tearDown() throws Exception {
429438
final boolean pExists = getPackageP().exists();
430439

431440
// this is destination path.
432-
Path destinationProjectPath = getDestinationProjectPath();
441+
Path destinationProjectPath = getEntryPointFileProjectDestinationPath();
433442

434443
if (getEntryPointFile(destinationProjectPath) != null)
435444
Files.delete(destinationProjectPath);

0 commit comments

Comments
 (0)