2007-02-02 17:17:13 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright (c) 2002 - 2006 IBM Corporation.
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* IBM Corporation - initial API and implementation
|
|
|
|
*****************************************************************************/
|
|
|
|
package com.ibm.wala.cast.ipa.callgraph;
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import com.ibm.wala.classLoader.CallSiteReference;
|
|
|
|
import com.ibm.wala.classLoader.IClass;
|
2009-03-26 15:11:45 +00:00
|
|
|
import com.ibm.wala.classLoader.IClassLoader;
|
2007-02-02 17:17:13 +00:00
|
|
|
import com.ibm.wala.classLoader.IMethod;
|
2009-03-26 15:11:45 +00:00
|
|
|
import com.ibm.wala.classLoader.Language;
|
2007-02-02 17:17:13 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.CGNode;
|
|
|
|
import com.ibm.wala.ipa.callgraph.Context;
|
|
|
|
import com.ibm.wala.ipa.callgraph.ContextSelector;
|
|
|
|
import com.ibm.wala.ipa.callgraph.propagation.InstanceKey;
|
2007-06-01 03:31:35 +00:00
|
|
|
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
2007-07-06 22:09:29 +00:00
|
|
|
import com.ibm.wala.types.ClassLoaderReference;
|
|
|
|
import com.ibm.wala.types.Descriptor;
|
|
|
|
import com.ibm.wala.types.MethodReference;
|
|
|
|
import com.ibm.wala.types.TypeName;
|
|
|
|
import com.ibm.wala.types.TypeReference;
|
2007-07-11 21:07:32 +00:00
|
|
|
import com.ibm.wala.util.collections.HashSetFactory;
|
2007-07-06 22:09:29 +00:00
|
|
|
import com.ibm.wala.util.debug.Assertions;
|
2011-04-27 13:58:56 +00:00
|
|
|
import com.ibm.wala.util.intset.IntSet;
|
2008-01-24 22:05:51 +00:00
|
|
|
import com.ibm.wala.util.strings.Atom;
|
2007-02-02 17:17:13 +00:00
|
|
|
|
2007-07-06 22:09:29 +00:00
|
|
|
public class MiscellaneousHacksContextSelector implements ContextSelector {
|
2007-02-02 17:17:13 +00:00
|
|
|
|
2007-02-08 20:45:07 +00:00
|
|
|
private final Set<MethodReference> methodsToSpecialize;
|
2007-02-02 17:17:13 +00:00
|
|
|
|
|
|
|
private final ContextSelector specialPolicy;
|
2007-07-06 22:09:29 +00:00
|
|
|
|
2007-02-02 17:17:13 +00:00
|
|
|
private final ContextSelector basePolicy;
|
|
|
|
|
2007-07-06 22:09:29 +00:00
|
|
|
public MiscellaneousHacksContextSelector(ContextSelector special, ContextSelector base, IClassHierarchy cha,
|
|
|
|
String[][] descriptors) {
|
2007-02-02 17:17:13 +00:00
|
|
|
basePolicy = base;
|
|
|
|
specialPolicy = special;
|
2007-07-11 21:07:32 +00:00
|
|
|
methodsToSpecialize = HashSetFactory.make();
|
2017-11-28 20:26:09 +00:00
|
|
|
for (String[] descr : descriptors) {
|
2007-02-02 17:17:13 +00:00
|
|
|
switch (descr.length) {
|
|
|
|
|
2007-06-01 03:31:35 +00:00
|
|
|
// loader name, loader language, classname, method name, method descr
|
|
|
|
case 5: {
|
2009-03-26 15:11:45 +00:00
|
|
|
ClassLoaderReference clr = cha.getScope().getLoader(Atom.findOrCreateUnicodeAtom(descr[0]));
|
|
|
|
IClassLoader loader = cha.getLoader(clr);
|
|
|
|
MethodReference ref =
|
|
|
|
MethodReference.findOrCreate(
|
|
|
|
TypeReference.findOrCreate(clr, TypeName.string2TypeName(descr[2])),
|
|
|
|
Atom.findOrCreateUnicodeAtom(descr[3]),
|
|
|
|
Descriptor.findOrCreateUTF8(loader.getLanguage(), descr[4]));
|
2007-07-06 22:09:29 +00:00
|
|
|
|
|
|
|
if (cha.resolveMethod(ref) != null) {
|
|
|
|
methodsToSpecialize.add(cha.resolveMethod(ref).getReference());
|
|
|
|
} else {
|
|
|
|
methodsToSpecialize.add(ref);
|
|
|
|
}
|
|
|
|
break;
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2007-07-06 22:09:29 +00:00
|
|
|
// classname, method name, method descr
|
2007-02-02 17:17:13 +00:00
|
|
|
case 3: {
|
2009-03-13 14:20:35 +00:00
|
|
|
MethodReference ref = MethodReference.findOrCreate(TypeReference.findOrCreate(ClassLoaderReference.Application, TypeName.string2TypeName(descr[0])), Atom
|
2009-03-26 15:11:45 +00:00
|
|
|
.findOrCreateUnicodeAtom(descr[1]), Descriptor.findOrCreateUTF8(Language.JAVA, descr[2]));
|
2007-07-06 22:09:29 +00:00
|
|
|
|
|
|
|
methodsToSpecialize.add(cha.resolveMethod(ref).getReference());
|
|
|
|
break;
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2007-07-06 22:09:29 +00:00
|
|
|
// loader name, classname, meaning all methods of that class
|
2007-02-02 17:17:13 +00:00
|
|
|
case 2: {
|
2007-07-06 22:09:29 +00:00
|
|
|
IClass klass = cha.lookupClass(TypeReference.findOrCreate(new ClassLoaderReference(Atom.findOrCreateUnicodeAtom(descr[0]),
|
2009-03-13 14:20:35 +00:00
|
|
|
ClassLoaderReference.Java, null), TypeName.string2TypeName(descr[1])));
|
2007-07-06 22:09:29 +00:00
|
|
|
|
Fix nearly all Eclipse warnings about using raw types
Along the way, I also converted many "for (;;)" loops into modern
"for (:)" loops. I didn't systematically look for all opportunities
to do this, though. I merely made this change where I was already
converting raw Iterator uses into modern Iterator<...> uses.
Better use of generics also allowed many casts to become statically
redundant. I have removed all such redundant casts.
Only three raw-types warnings remain after this batch of fixes. All
three involve raw uses of CallGraphBuilder. I've tried to fix these
too, but it quickly snowballs into a cascade of changes that may or
may not eventually reach a statically-type-save fixed point. I may
give these last few problem areas another go in the future. For now,
though, the hundreds of other fixes seem worth keeping even if there
are a few stragglers.
This commit may change some public APIs, but only by making weaker
type signatures stronger by replacing raw types with generic types.
For example, we may change something like "Set" into "Set<String>",
but we're not adding new arguments, changing any
underlying (post-generics-erasure) types, etc.
2017-07-09 18:38:35 +00:00
|
|
|
for (IMethod M : klass.getDeclaredMethods()) {
|
|
|
|
methodsToSpecialize.add(M.getReference());
|
2007-07-06 22:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2007-07-06 22:09:29 +00:00
|
|
|
// classname, meaning all methods of that class
|
2007-02-02 17:17:13 +00:00
|
|
|
case 1: {
|
2009-03-13 14:20:35 +00:00
|
|
|
IClass klass = cha.lookupClass(TypeReference.findOrCreate(ClassLoaderReference.Application, TypeName.string2TypeName(descr[0])));
|
2007-07-06 22:09:29 +00:00
|
|
|
|
Fix nearly all Eclipse warnings about using raw types
Along the way, I also converted many "for (;;)" loops into modern
"for (:)" loops. I didn't systematically look for all opportunities
to do this, though. I merely made this change where I was already
converting raw Iterator uses into modern Iterator<...> uses.
Better use of generics also allowed many casts to become statically
redundant. I have removed all such redundant casts.
Only three raw-types warnings remain after this batch of fixes. All
three involve raw uses of CallGraphBuilder. I've tried to fix these
too, but it quickly snowballs into a cascade of changes that may or
may not eventually reach a statically-type-save fixed point. I may
give these last few problem areas another go in the future. For now,
though, the hundreds of other fixes seem worth keeping even if there
are a few stragglers.
This commit may change some public APIs, but only by making weaker
type signatures stronger by replacing raw types with generic types.
For example, we may change something like "Set" into "Set<String>",
but we're not adding new arguments, changing any
underlying (post-generics-erasure) types, etc.
2017-07-09 18:38:35 +00:00
|
|
|
for (IMethod M : klass.getDeclaredMethods()) {
|
|
|
|
methodsToSpecialize.add(M.getReference());
|
2007-07-06 22:09:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2007-07-06 22:09:29 +00:00
|
|
|
default:
|
|
|
|
Assertions.UNREACHABLE();
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-28 14:30:15 +00:00
|
|
|
|
2009-04-09 20:31:14 +00:00
|
|
|
System.err.println(("hacking context selector for methods " + methodsToSpecialize));
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2011-04-27 13:58:56 +00:00
|
|
|
public Context getCalleeTarget(CGNode caller, CallSiteReference site, IMethod callee, InstanceKey[] receiver) {
|
2007-07-06 22:09:29 +00:00
|
|
|
if (methodsToSpecialize.contains(site.getDeclaredTarget()) || methodsToSpecialize.contains(callee.getReference())) {
|
2007-02-02 17:17:13 +00:00
|
|
|
return specialPolicy.getCalleeTarget(caller, site, callee, receiver);
|
2007-06-28 14:30:15 +00:00
|
|
|
} else {
|
2007-02-02 17:17:13 +00:00
|
|
|
return basePolicy.getCalleeTarget(caller, site, callee, receiver);
|
2007-06-28 14:30:15 +00:00
|
|
|
}
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2011-04-27 13:58:56 +00:00
|
|
|
public IntSet getRelevantParameters(CGNode caller, CallSiteReference site) {
|
|
|
|
return specialPolicy.getRelevantParameters(caller, site).union(basePolicy.getRelevantParameters(caller, site));
|
2007-02-02 17:17:13 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 13:58:56 +00:00
|
|
|
}
|