Skip to content

Commit 79d87a6

Browse files
committed
Merge branch 'master_add_entry_point' of https://github.com/saledouble/Java-8-Stream-Refactoring into saledouble-master_add_entry_point
2 parents e352a39 + e8dfd9e commit 79d87a6

File tree

5 files changed

+97
-13
lines changed

5 files changed

+97
-13
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ private static void addImplicitEntryPoints(Collection<Entrypoint> target, Iterab
5858
private boolean findImplicitEntryPoints = true;
5959

6060
private boolean findImplicitTestEntryPoints;
61+
62+
private boolean findImplicitBenchmarkEntryPoints;
6163

6264
private Set<Stream> streamSet = new HashSet<>();
6365

@@ -74,9 +76,11 @@ public StreamAnalyzer(boolean visitDocTags, boolean findImplicitEntryPoints) {
7476
this.findImplicitEntryPoints = findImplicitEntryPoints;
7577
}
7678

77-
public StreamAnalyzer(boolean visitDocTags, boolean findImplicitEntryPoints, boolean findImplicitTestEntryPoints) {
79+
public StreamAnalyzer(boolean visitDocTags, boolean findImplicitEntryPoints, boolean findImplicitTestEntryPoints,
80+
boolean findImplicitBenchmarkEntryPoints) {
7881
this(visitDocTags, findImplicitEntryPoints);
7982
this.findImplicitTestEntryPoints = findImplicitTestEntryPoints;
83+
this.findImplicitBenchmarkEntryPoints = findImplicitBenchmarkEntryPoints;
8084
}
8185

8286
/**
@@ -201,6 +205,14 @@ protected Collection<Entrypoint> buildCallGraph(EclipseProjectAnalysisEngine<Ins
201205
// add them as well.
202206
addImplicitEntryPoints(entryPoints, jUnitEntryPoints);
203207
}
208+
209+
if (this.findImplicitBenchmarkEntryPoints) {
210+
// try to find benchmark entry points.
211+
Set<Entrypoint> benchmarkEntryPoints = Util.findBenchmarkEntryPoints(engine.getClassHierarchy());
212+
213+
// add them as well.
214+
addImplicitEntryPoints(entryPoints, benchmarkEntryPoints);
215+
}
204216

205217
if (entryPoints.isEmpty()) {
206218
LOGGER.warning(() -> "Project: " + engine.getProject().getElementName() + " has no entry points.");

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public final class Util {
7474

7575
private static final Logger LOGGER = Logger.getLogger(LoggerNames.LOGGER_NAME);
7676
private static final String ENTRYPOINT = "edu.cuny.hunter.streamrefactoring.annotations.EntryPoint";
77+
private static final String BENCHMARK = "org.openjdk.jmh.annotations.Benchmark";
78+
private static final String SETUP = "org.openjdk.jmh.annotations.Setup";
7779

7880
private static final class CorrespondingASTVisitor extends ASTVisitor {
7981
private CompilationUnit unit;
@@ -410,6 +412,14 @@ private static boolean isEntryPointClass(TypeName typeName) {
410412
return AnalysisUtils.walaTypeNameToJavaName(typeName).equals(ENTRYPOINT);
411413
}
412414

415+
private static boolean isBenchmark(TypeName typeName) {
416+
return AnalysisUtils.walaTypeNameToJavaName(typeName).equals(BENCHMARK);
417+
}
418+
419+
private static boolean isSetup(TypeName typeName) {
420+
return AnalysisUtils.walaTypeNameToJavaName(typeName).equals(SETUP);
421+
}
422+
413423
/**
414424
* Find all annotations in test cases and check whether they are "entry point".
415425
* If yes, call DefaultEntrypoint to get entry point, then, add it into the
@@ -434,9 +444,51 @@ public static Set<Entrypoint> findEntryPoints(IClassHierarchy classHierarchy) {
434444
break;
435445
}
436446
} catch (InvalidClassFileException e) {
437-
throw new IllegalArgumentException("Failed to find entry points using class hierarchy: " + classHierarchy + ".", e);
447+
throw new IllegalArgumentException(
448+
"Failed to find entry points using class hierarchy: " + classHierarchy + ".", e);
449+
}
450+
}
451+
}
452+
453+
return result;
454+
}
455+
456+
public static Set<Entrypoint> findBenchmarkEntryPoints(IClassHierarchy classHierarchy) {
457+
final Set<Entrypoint> result = new HashSet<>();
458+
459+
for (Iterator<IClass> classIterator = classHierarchy.iterator(); classIterator.hasNext();) {
460+
IClass klass = classIterator.next();
461+
if (!AnalysisUtils.isJDKClass(klass)) {
462+
boolean isBenchmarkClass = false;
463+
// iterate over all declared methods
464+
for (com.ibm.wala.classLoader.IMethod method : klass.getDeclaredMethods()) {
465+
// if method has an annotation
466+
if (!(method instanceof ShrikeCTMethod))
467+
throw new IllegalArgumentException("@EntryPoint only works for byte code.");
468+
469+
for (Annotation annotation : ((ShrikeCTMethod) method).getAnnotations()) {
470+
TypeName annotationName = annotation.getType().getName();
471+
472+
if (isBenchmark(annotationName) || isSetup(annotationName)) {
473+
result.add(new DefaultEntrypoint(method, classHierarchy));
474+
isBenchmarkClass = true;
475+
break;
476+
}
438477
}
439478
}
479+
if (isBenchmarkClass) {
480+
IMethod classInitializer = klass.getClassInitializer();
481+
482+
if (classInitializer != null)
483+
result.add(new DefaultEntrypoint(classInitializer, classHierarchy));
484+
485+
IMethod ctor = klass.getMethod(MethodReference.initSelector);
486+
487+
if (ctor != null)
488+
result.add(new DefaultEntrypoint(ctor, classHierarchy));
489+
490+
}
491+
}
440492
}
441493

442494
return result;

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,25 +139,29 @@ public static void setLoggingLevel(int level) {
139139
private boolean useImplicitEntrypoints = true;
140140

141141
private boolean useImplicitTestEntrypoints = false;
142+
143+
private boolean useImplicitBenchmarkEntrypoints = false;
142144

143145
public ConvertToParallelStreamRefactoringProcessor() throws JavaModelException {
144-
this(null, null, false, true, false, Optional.empty());
146+
this(null, null, false, true, false, false, Optional.empty());
145147
}
146148

147149
public ConvertToParallelStreamRefactoringProcessor(final CodeGenerationSettings settings,
148150
Optional<IProgressMonitor> monitor) throws JavaModelException {
149-
this(null, settings, false, true, false, monitor);
151+
this(null, settings, false, true, false, false, monitor);
150152
}
151153

152154
public ConvertToParallelStreamRefactoringProcessor(IJavaProject[] javaProjects,
153155
final CodeGenerationSettings settings, boolean layer, boolean useImplicitEntrypoints,
154-
boolean useImplicitTestEntrypoints, Optional<IProgressMonitor> monitor) throws JavaModelException {
156+
boolean useImplicitTestEntrypoints, boolean useImplicitBenchmarkEntrypoints,
157+
Optional<IProgressMonitor> monitor) throws JavaModelException {
155158
try {
156159
this.javaProjects = javaProjects;
157160
this.settings = settings;
158161
this.layer = layer;
159162
this.useImplicitEntrypoints = useImplicitEntrypoints;
160163
this.useImplicitTestEntrypoints = useImplicitTestEntrypoints;
164+
this.useImplicitBenchmarkEntrypoints = useImplicitBenchmarkEntrypoints;
161165
} finally {
162166
monitor.ifPresent(IProgressMonitor::done);
163167
}
@@ -166,16 +170,16 @@ public ConvertToParallelStreamRefactoringProcessor(IJavaProject[] javaProjects,
166170
public ConvertToParallelStreamRefactoringProcessor(IJavaProject[] javaProjects,
167171
final CodeGenerationSettings settings, boolean useImplicitJoinpoints, Optional<IProgressMonitor> monitor)
168172
throws JavaModelException {
169-
this(javaProjects, settings, false, useImplicitJoinpoints, false, monitor);
173+
this(javaProjects, settings, false, useImplicitJoinpoints, false, false, monitor);
170174
}
171175

172176
public ConvertToParallelStreamRefactoringProcessor(IJavaProject[] javaProjects,
173177
final CodeGenerationSettings settings, Optional<IProgressMonitor> monitor) throws JavaModelException {
174-
this(javaProjects, settings, false, true, false, monitor);
178+
this(javaProjects, settings, false, true, false, false, monitor);
175179
}
176180

177181
public ConvertToParallelStreamRefactoringProcessor(Optional<IProgressMonitor> monitor) throws JavaModelException {
178-
this(null, null, false, true, false, monitor);
182+
this(null, null, false, true, false, false, monitor);
179183
}
180184

181185
private RefactoringStatus checkExistence(IMember member, PreconditionFailure failure) {
@@ -193,7 +197,7 @@ public RefactoringStatus checkFinalConditions(final IProgressMonitor monitor, fi
193197
this.getJavaProjects().length * 1000);
194198
final RefactoringStatus status = new RefactoringStatus();
195199
StreamAnalyzer analyzer = new StreamAnalyzer(false, this.getUseImplicitEntrypoints(),
196-
this.getUseImplicitTestEntrypoints());
200+
this.getUseImplicitTestEntrypoints(), this.getUseImplicitBenchmarkEntrypoints());
197201
this.setStreamSet(analyzer.getStreamSet());
198202

199203
for (IJavaProject jproj : this.getJavaProjects()) {
@@ -456,6 +460,10 @@ private boolean getUseImplicitEntrypoints() {
456460
private boolean getUseImplicitTestEntrypoints() {
457461
return this.useImplicitTestEntrypoints;
458462
}
463+
464+
private boolean getUseImplicitBenchmarkEntrypoints() {
465+
return this.useImplicitBenchmarkEntrypoints;
466+
}
459467

460468
@Override
461469
public boolean isApplicable() throws CoreException {
@@ -491,4 +499,4 @@ private void manageCompilationUnit(final TextEditBasedChangeManager manager, Com
491499
protected void setStreamSet(Set<Stream> streamSet) {
492500
this.streamSet = streamSet;
493501
}
494-
}
502+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ public static ConvertToParallelStreamRefactoringProcessor createConvertToParalle
5353

5454
public static ConvertToParallelStreamRefactoringProcessor createConvertToParallelStreamRefactoringProcessor(
5555
IJavaProject[] projects, boolean useImplicitEntrypoints, boolean useImplicitTestEntrypoints,
56-
Optional<IProgressMonitor> monitor) throws JavaModelException {
56+
boolean useImplicitBenchmarkEntrypoints, Optional<IProgressMonitor> monitor) throws JavaModelException {
5757
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(projects[0]);
5858
ConvertToParallelStreamRefactoringProcessor processor = new ConvertToParallelStreamRefactoringProcessor(
59-
projects, settings, false, useImplicitEntrypoints, useImplicitTestEntrypoints, monitor);
59+
projects, settings, false, useImplicitEntrypoints, useImplicitTestEntrypoints,
60+
useImplicitBenchmarkEntrypoints, monitor);
6061
return processor;
6162
}
6263

edu.cuny.hunter.streamrefactoring.eval/src/edu/cuny/hunter/streamrefactoring/eval/handlers/EvaluateConvertToParallelStreamRefactoringHandler.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public class EvaluateConvertToParallelStreamRefactoringHandler extends AbstractH
7878
private static final String FIND_IMPLICIT_TEST_ENTRYPOINTS_PROPERTY_KEY = "edu.cuny.hunter.streamrefactoring.eval.findImplicitTestEntrypoints";
7979
private static final boolean FIND_IMPLICIT_ENTRYPOINTS_DEFAULT = true;
8080
private static final String FIND_IMPLICIT_ENTRYPOINTS_PROPERTY_KEY = "edu.cuny.hunter.streamrefactoring.eval.findImplicitEntrypoints";
81+
private static final boolean FIND_IMPLICIT_BENCHMARK_ENTRYPOINTS_DEFAULT = false;
82+
private static final String FIND_IMPLICIT_BENCHMARK_ENTRYPOINTS_PROPERTY_KEY = "edu.cuny.hunter.streamrefactoring.eval.findImplicitBenchmarkEntrypoints";
8183
private static final int LOGGING_LEVEL = IStatus.INFO;
8284
private static final boolean PERFORM_CHANGE_DEFAULT = false;
8385
private static final String PERFORM_CHANGE_PROPERTY_KEY = "edu.cuny.hunter.streamrefactoring.eval.performChange";
@@ -245,7 +247,7 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
245247
resultsTimeCollector.start();
246248
processor = createConvertToParallelStreamRefactoringProcessor(new IJavaProject[] { javaProject },
247249
this.shouldFindImplicitEntrypoints(), this.shouldFindImplicitTestEntrypoints(),
248-
Optional.of(monitor));
250+
this.shouldFindImplicitBenchmarkEntrypoints(), Optional.of(monitor));
249251
resultsTimeCollector.stop();
250252
ConvertToParallelStreamRefactoringProcessor.setLoggingLevel(LOGGING_LEVEL);
251253

@@ -490,6 +492,15 @@ private boolean shouldFindImplicitTestEntrypoints() {
490492
else
491493
return Boolean.valueOf(findImplicitTestEntrypoints);
492494
}
495+
496+
private boolean shouldFindImplicitBenchmarkEntrypoints() {
497+
String findImplicitBenchmarkEntrypoints = System.getenv(FIND_IMPLICIT_BENCHMARK_ENTRYPOINTS_PROPERTY_KEY);
498+
499+
if (findImplicitBenchmarkEntrypoints == null)
500+
return FIND_IMPLICIT_BENCHMARK_ENTRYPOINTS_DEFAULT;
501+
else
502+
return Boolean.valueOf(findImplicitBenchmarkEntrypoints);
503+
}
493504

494505
private boolean shouldPerformChange() {
495506
String performChangePropertyValue = System.getenv(PERFORM_CHANGE_PROPERTY_KEY);

0 commit comments

Comments
 (0)