|
1 | 1 | package edu.cuny.hunter.streamrefactoring.core.analysis;
|
2 | 2 |
|
| 3 | +import java.io.UTFDataFormatException; |
3 | 4 | import java.lang.reflect.Modifier;
|
4 | 5 | import java.util.ArrayList;
|
5 | 6 | import java.util.Collection;
|
|
12 | 13 | import java.util.logging.Logger;
|
13 | 14 | import java.util.stream.BaseStream;
|
14 | 15 |
|
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; |
16 | 19 | import org.eclipse.jdt.core.dom.ASTNode;
|
17 | 20 | import org.eclipse.jdt.core.dom.ASTVisitor;
|
18 | 21 | import org.eclipse.jdt.core.dom.CompilationUnit;
|
19 | 22 | import org.eclipse.jdt.core.dom.ITypeBinding;
|
20 | 23 | import org.eclipse.jdt.core.dom.MethodInvocation;
|
21 | 24 | import org.eclipse.jdt.core.dom.SimpleName;
|
| 25 | +import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser; |
22 | 26 |
|
23 | 27 | import com.ibm.wala.analysis.typeInference.PointType;
|
24 | 28 | import com.ibm.wala.analysis.typeInference.TypeAbstraction;
|
|
61 | 65 | import edu.cuny.hunter.streamrefactoring.core.wala.AnalysisUtils;
|
62 | 66 | import edu.cuny.hunter.streamrefactoring.core.wala.CallStringWithReceivers;
|
63 | 67 |
|
| 68 | +@SuppressWarnings("restriction") |
64 | 69 | public final class Util {
|
65 | 70 |
|
66 | 71 | private static final Logger LOGGER = Logger.getLogger(LoggerNames.LOGGER_NAME);
|
@@ -202,7 +207,8 @@ static String getBinaryName(TypeReference typeReference) {
|
202 | 207 |
|
203 | 208 | public static Collection<TypeAbstraction> getPossibleTypesInterprocedurally(CGNode node, int valueNumber,
|
204 | 209 | HeapModel heapModel, PointerAnalysis<InstanceKey> pointerAnalysis, Stream stream)
|
205 |
| - throws NoniterableException, NoninstantiableException, CannotExtractSpliteratorException { |
| 210 | + throws NoniterableException, NoninstantiableException, CannotExtractSpliteratorException, |
| 211 | + UTFDataFormatException, JavaModelException { |
206 | 212 | Collection<TypeAbstraction> ret = new HashSet<>();
|
207 | 213 |
|
208 | 214 | PointerKey valueKey = heapModel.getPointerKeyForLocal(node, valueNumber);
|
@@ -262,64 +268,58 @@ public static Collection<TypeAbstraction> getPossibleTypesInterprocedurally(CGNo
|
262 | 268 | e);
|
263 | 269 | }
|
264 | 270 |
|
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, |
299 | 313 | 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); |
303 | 316 | } 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); |
318 | 321 | }
|
319 | 322 | }
|
320 |
| - } else { |
321 |
| - // FIXME: Interprocedural? |
322 |
| - throw new IllegalStateException("Can't find corresponding file."); |
323 | 323 | }
|
324 | 324 | } else {
|
325 | 325 | // just add it.
|
|
0 commit comments