Skip to content

Commit d99dd13

Browse files
committed
Add interprocedural source file look up to possible type inference.
1 parent 2d5cce5 commit d99dd13

File tree

1 file changed

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

1 file changed

+56
-56
lines changed

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

+56-56
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.cuny.hunter.streamrefactoring.core.analysis;
22

3+
import java.io.UTFDataFormatException;
34
import java.lang.reflect.Modifier;
45
import java.util.ArrayList;
56
import java.util.Collection;
@@ -12,13 +13,16 @@
1213
import java.util.logging.Logger;
1314
import java.util.stream.BaseStream;
1415

15-
import org.eclipse.jdt.core.ITypeRoot;
16+
import org.eclipse.jdt.core.IJavaProject;
17+
import org.eclipse.jdt.core.IType;
18+
import org.eclipse.jdt.core.JavaModelException;
1619
import org.eclipse.jdt.core.dom.ASTNode;
1720
import org.eclipse.jdt.core.dom.ASTVisitor;
1821
import org.eclipse.jdt.core.dom.CompilationUnit;
1922
import org.eclipse.jdt.core.dom.ITypeBinding;
2023
import org.eclipse.jdt.core.dom.MethodInvocation;
2124
import org.eclipse.jdt.core.dom.SimpleName;
25+
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
2226

2327
import com.ibm.wala.analysis.typeInference.PointType;
2428
import com.ibm.wala.analysis.typeInference.TypeAbstraction;
@@ -61,6 +65,7 @@
6165
import edu.cuny.hunter.streamrefactoring.core.wala.AnalysisUtils;
6266
import edu.cuny.hunter.streamrefactoring.core.wala.CallStringWithReceivers;
6367

68+
@SuppressWarnings("restriction")
6469
public final class Util {
6570

6671
private static final Logger LOGGER = Logger.getLogger(LoggerNames.LOGGER_NAME);
@@ -202,7 +207,8 @@ static String getBinaryName(TypeReference typeReference) {
202207

203208
public static Collection<TypeAbstraction> getPossibleTypesInterprocedurally(CGNode node, int valueNumber,
204209
HeapModel heapModel, PointerAnalysis<InstanceKey> pointerAnalysis, Stream stream)
205-
throws NoniterableException, NoninstantiableException, CannotExtractSpliteratorException {
210+
throws NoniterableException, NoninstantiableException, CannotExtractSpliteratorException,
211+
UTFDataFormatException, JavaModelException {
206212
Collection<TypeAbstraction> ret = new HashSet<>();
207213

208214
PointerKey valueKey = heapModel.getPointerKeyForLocal(node, valueNumber);
@@ -262,64 +268,58 @@ public static Collection<TypeAbstraction> getPossibleTypesInterprocedurally(CGNo
262268
e);
263269
}
264270

265-
// ensure that the file names are the same.
266-
// FIXME: Do we need to worry about paths? Maybe it would
267-
// suffice to check packages.
268-
CompilationUnit unit = stream.getEnclosingCompilationUnit();
269-
ITypeRoot typeRoot = unit.getTypeRoot();
270-
String typeRootFileName = typeRoot.getElementName();
271-
String sourcePositionFileName = sourcePosition.getFileName();
272-
273-
if (typeRootFileName.equals(sourcePositionFileName)) {
274-
// same file.
275-
// we have the CompilationUnit corresponding to the
276-
// instruction's file.
277-
// can we correlate the instruction to the method
278-
// invocation in the AST?
279-
MethodInvocation correspondingInvocation = findCorrespondingMethodInvocation(unit,
280-
sourcePosition, def.getCallSite().getDeclaredTarget());
281-
282-
// what does the method return?
283-
ITypeBinding genericReturnType = correspondingInvocation.resolveMethodBinding().getReturnType();
284-
285-
// Is it compatible with the concrete type we got from
286-
// WALA?
287-
// But first, we'll need to translate the Eclipse JDT
288-
// type over to a IClass.
289-
TypeReference genericTypeRef = getJDTIdentifyMapper(correspondingInvocation)
290-
.getTypeRef(genericReturnType);
291-
IClass genericClass = node.getClassHierarchy().lookupClass(genericTypeRef);
292-
293-
boolean assignableFrom = node.getClassHierarchy().isAssignableFrom(genericClass, concreteClass);
294-
295-
// if it's assignable.
296-
if (assignableFrom) {
297-
// would the ordering be consistent?
298-
if (wouldOrderingBeConsistent(Collections.unmodifiableCollection(ret), concreteType,
271+
// let's assume that the source file is in the same project.
272+
IJavaProject enclosingProject = stream.getEnclosingEclipseMethod().getJavaProject();
273+
274+
String fqn = method.getDeclaringClass().getName().getPackage().toUnicodeString() + "."
275+
+ method.getDeclaringClass().getName().getClassName().toUnicodeString();
276+
IType type = enclosingProject.findType(fqn);
277+
CompilationUnit unit = RefactoringASTParser.parseWithASTProvider(type.getTypeRoot(), true, null);
278+
279+
// we have the CompilationUnit corresponding to the
280+
// instruction's file.
281+
// can we correlate the instruction to the method
282+
// invocation in the AST?
283+
MethodInvocation correspondingInvocation = findCorrespondingMethodInvocation(unit, sourcePosition,
284+
def.getCallSite().getDeclaredTarget());
285+
286+
// what does the method return?
287+
ITypeBinding genericReturnType = correspondingInvocation.resolveMethodBinding().getReturnType();
288+
289+
// Is it compatible with the concrete type we got from
290+
// WALA?
291+
// But first, we'll need to translate the Eclipse JDT
292+
// type over to a IClass.
293+
TypeReference genericTypeRef = getJDTIdentifyMapper(correspondingInvocation)
294+
.getTypeRef(genericReturnType);
295+
IClass genericClass = node.getClassHierarchy().lookupClass(genericTypeRef);
296+
297+
boolean assignableFrom = node.getClassHierarchy().isAssignableFrom(genericClass, concreteClass);
298+
299+
// if it's assignable.
300+
if (assignableFrom) {
301+
// would the ordering be consistent?
302+
if (wouldOrderingBeConsistent(Collections.unmodifiableCollection(ret), concreteType,
303+
stream.getOrderingInference())) {
304+
// if so, add it.
305+
LOGGER.info("Add type straight up: " + concreteType);
306+
ret.add(concreteType);
307+
} else {
308+
// otherwise, would the generic type cause the
309+
// ordering to be inconsistent?
310+
PointType genericType = new PointType(genericClass);
311+
312+
if (wouldOrderingBeConsistent(Collections.unmodifiableCollection(ret), genericType,
299313
stream.getOrderingInference())) {
300-
// if so, add it.
301-
LOGGER.info("Add type straight up: " + concreteType);
302-
ret.add(concreteType);
314+
LOGGER.info("Defaulting to generic type: " + genericType);
315+
ret.add(genericType);
303316
} else {
304-
// otherwise, would the generic type cause the
305-
// ordering to be inconsistent?
306-
PointType genericType = new PointType(genericClass);
307-
308-
if (wouldOrderingBeConsistent(Collections.unmodifiableCollection(ret), genericType,
309-
stream.getOrderingInference())) {
310-
LOGGER.info("Defaulting to generic type: " + genericType);
311-
ret.add(genericType);
312-
} else {
313-
// fall back to the concrete type.
314-
LOGGER.info("Defaulting to concrete type eventhough it isn't consistent: "
315-
+ concreteType);
316-
ret.add(concreteType);
317-
}
317+
// fall back to the concrete type.
318+
LOGGER.info(
319+
"Defaulting to concrete type eventhough it isn't consistent: " + concreteType);
320+
ret.add(concreteType);
318321
}
319322
}
320-
} else {
321-
// FIXME: Interprocedural?
322-
throw new IllegalStateException("Can't find corresponding file.");
323323
}
324324
} else {
325325
// just add it.

0 commit comments

Comments
 (0)