Skip to content

Commit 51741bf

Browse files
committed
Add static initializer and ctor for classes with entry points.
1 parent ce4d107 commit 51741bf

File tree

1 file changed

+49
-1
lines changed
  • edu.cuny.hunter.streamrefactoring.core/src/edu/cuny/hunter/streamrefactoring/core/analysis

1 file changed

+49
-1
lines changed

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.function.Predicate;
1414
import java.util.logging.Logger;
1515
import java.util.stream.BaseStream;
16+
import java.util.stream.Collectors;
1617

1718
import org.eclipse.jdt.core.IJavaProject;
1819
import org.eclipse.jdt.core.IType;
@@ -215,7 +216,10 @@ public static Set<Entrypoint> findEntryPoints(IClassHierarchy classHierarchy) {
215216
final Set<Entrypoint> result = new HashSet<>();
216217

217218
for (IClass klass : classHierarchy)
218-
if (!AnalysisUtils.isJDKClass(klass))
219+
if (!AnalysisUtils.isJDKClass(klass)) {
220+
boolean entryPointClass = false;
221+
boolean addedInstanceMethod = false;
222+
219223
// iterate over all declared methods
220224
for (com.ibm.wala.classLoader.IMethod method : klass.getDeclaredMethods()) {
221225
// if method has an annotation
@@ -226,6 +230,12 @@ public static Set<Entrypoint> findEntryPoints(IClassHierarchy classHierarchy) {
226230
for (Annotation annotation : ((ShrikeCTMethod) method).getAnnotations(true))
227231
if (isEntryPointClass(annotation.getType().getName())) {
228232
addEntryPoint(result, method, classHierarchy);
233+
entryPointClass = true;
234+
235+
// if the method is an instance method.
236+
if (!method.isStatic())
237+
addedInstanceMethod = true;
238+
229239
break;
230240
}
231241
} catch (InvalidClassFileException e) {
@@ -234,9 +244,47 @@ public static Set<Entrypoint> findEntryPoints(IClassHierarchy classHierarchy) {
234244
}
235245
}
236246

247+
if (entryPointClass) {
248+
// add any static initializers since the class will be loaded.
249+
addEntryPoint(result, klass.getClassInitializer(), classHierarchy);
250+
251+
if (addedInstanceMethod) {
252+
// add a constructor.
253+
IMethod ctor = getUniqueConstructor(klass);
254+
addEntryPoint(result, ctor, classHierarchy);
255+
}
256+
}
257+
}
258+
237259
return result;
238260
}
239261

262+
/**
263+
* A null get value means that there is no unique ctor.
264+
*
265+
* @param klass
266+
* The {@link IClass} to find a unique ctor.
267+
* @return The one and only ctor for klass and <code>null</code> if it doesn't
268+
* exist.
269+
*/
270+
private static IMethod getUniqueConstructor(IClass klass) {
271+
// try to find the default ctor.
272+
IMethod ctor = klass.getMethod(MethodReference.initSelector);
273+
274+
// if not found, get all constructors.
275+
if (ctor == null) {
276+
Set<IMethod> allDeclaredConstructors = klass.getDeclaredMethods().stream()
277+
.filter(m -> m.getName().startsWith(MethodReference.initAtom)).collect(Collectors.toSet());
278+
279+
// if there is a unique one.
280+
if (allDeclaredConstructors.size() == 1)
281+
// use that.
282+
ctor = allDeclaredConstructors.iterator().next();
283+
}
284+
285+
return ctor;
286+
}
287+
240288
static Set<ITypeBinding> getAllInterfaces(ITypeBinding type) {
241289
Set<ITypeBinding> ret = new HashSet<>();
242290
ITypeBinding[] interfaces = type.getInterfaces();

0 commit comments

Comments
 (0)