separate JSDT stuff

This commit is contained in:
dolby 2012-08-21 15:53:16 -04:00
parent 72e8dc9098
commit a54707f22e
21 changed files with 325 additions and 1042 deletions

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="source"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>com.ibm.wala.ide.jsdt</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,7 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6

View File

@ -0,0 +1,25 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Jsdt
Bundle-SymbolicName: com.ibm.wala.ide.jsdt
Bundle-Version: 1.0.0.qualifier
Bundle-Activator: com.ibm.wala.ide.jsdt.Activator
Bundle-Vendor: IBM
Require-Bundle: org.eclipse.ui,
org.eclipse.core.runtime,
com.ibm.wala.cast;bundle-version="1.0.0",
com.ibm.wala.cast.js;bundle-version="1.0.0",
com.ibm.wala.cast.js.rhino;bundle-version="1.0.0",
com.ibm.wala.core;bundle-version="1.1.3",
com.ibm.wala.ide;bundle-version="1.1.3",
com.ibm.wala.shrike;bundle-version="1.3.1",
com.ibm.wala.util;bundle-version="2.0.0",
org.eclipse.wst.jsdt.core;bundle-version="1.1.201",
org.eclipse.wst.jsdt.ui;bundle-version="1.1.201",
org.eclipse.core.resources;bundle-version="3.8.0",
org.eclipse.pde.core;bundle-version="3.8.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: com.ibm.wala.cast.js.client,
com.ibm.wala.ide.jsdt,
com.ibm.wala.ide.util

View File

@ -0,0 +1,4 @@
source.. = source/
output.. = bin/
bin.includes = META-INF/,\
.

View File

@ -0,0 +1,51 @@
package com.ibm.wala.cast.js.client;
import java.io.IOException;
import java.util.Collections;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.wst.jsdt.core.IJavaScriptProject;
import com.ibm.wala.cast.ipa.callgraph.CAstAnalysisScope;
import com.ibm.wala.cast.js.loader.JavaScriptLoader;
import com.ibm.wala.cast.js.loader.JavaScriptLoaderFactory;
import com.ibm.wala.cast.js.translator.CAstRhinoTranslatorFactory;
import com.ibm.wala.cast.js.types.JavaScriptTypes;
import com.ibm.wala.ide.client.EclipseProjectSourceAnalysisEngine;
import com.ibm.wala.ide.util.JavaScriptEclipseProjectPath;
import com.ibm.wala.ipa.callgraph.AnalysisCache;
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.callgraph.CallGraphBuilder;
import com.ibm.wala.ipa.cha.IClassHierarchy;
import com.ibm.wala.types.ClassLoaderReference;
public class EclipseJavaScriptAnalysisEngine extends EclipseProjectSourceAnalysisEngine<IJavaScriptProject> {
public EclipseJavaScriptAnalysisEngine(IJavaScriptProject project) throws IOException, CoreException {
super(project, "js");
}
@Override
protected AnalysisScope makeSourceAnalysisScope() {
return new CAstAnalysisScope(new JavaScriptLoaderFactory(new CAstRhinoTranslatorFactory()), Collections.singleton(JavaScriptLoader.JS));
}
@Override
protected JavaScriptEclipseProjectPath createProjectPath(IJavaScriptProject project) throws IOException, CoreException {
return JavaScriptEclipseProjectPath.make(project);
}
@Override
protected ClassLoaderReference getSourceLoader() {
return JavaScriptTypes.jsLoader;
}
@Override
protected CallGraphBuilder getCallGraphBuilder(IClassHierarchy cha,
AnalysisOptions options, AnalysisCache cache) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -0,0 +1,50 @@
package com.ibm.wala.ide.jsdt;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
/**
* The activator class controls the plug-in life cycle
*/
public class Activator extends AbstractUIPlugin {
// The plug-in ID
public static final String PLUGIN_ID = "com.ibm.wala.ide.jsdt"; //$NON-NLS-1$
// The shared instance
private static Activator plugin;
/**
* The constructor
*/
public Activator() {
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
}
/*
* (non-Javadoc)
* @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}
/**
* Returns the shared instance
*
* @return the shared instance
*/
public static Activator getDefault() {
return plugin;
}
}

View File

@ -0,0 +1,66 @@
package com.ibm.wala.ide.util;
import java.io.IOException;
import java.util.Arrays;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.wst.jsdt.core.IIncludePathEntry;
import org.eclipse.wst.jsdt.core.IJavaScriptProject;
import org.eclipse.wst.jsdt.core.JavaScriptCore;
import org.eclipse.wst.jsdt.core.JavaScriptModelException;
public class JavaScriptEclipseProjectPath extends EclipseProjectPath<IIncludePathEntry, IJavaScriptProject> {
protected JavaScriptEclipseProjectPath(IJavaScriptProject p) throws IOException,
CoreException {
super(AnalysisScopeType.SOURCE_FOR_PROJ_AND_LINKED_PROJS);
// TODO Auto-generated constructor stub
}
public static JavaScriptEclipseProjectPath make(IJavaScriptProject p) throws IOException, CoreException {
return new JavaScriptEclipseProjectPath(p);
}
@Override
protected IJavaScriptProject makeProject(IProject p) {
try {
if (p.hasNature(JavaScriptCore.NATURE_ID)) {
return JavaScriptCore.create(p);
} else {
return null;
}
} catch (CoreException e) {
return null;
}
}
@Override
protected IIncludePathEntry resolve(IIncludePathEntry entry) {
return JavaScriptCore.getResolvedIncludepathEntry(entry);
}
@Override
protected void resolveClasspathEntry(IJavaScriptProject project,
IIncludePathEntry entry,
com.ibm.wala.ide.util.EclipseProjectPath.Loader loader,
boolean includeSource, boolean cpeFromMainProject) {
IIncludePathEntry e = JavaScriptCore.getResolvedIncludepathEntry(entry);
switch (e.getEntryKind()) {
case IIncludePathEntry.CPE_SOURCE:
resolveSourcePathEntry(Loader.JAVASCRIPT, true, cpeFromMainProject, e.getPath(), null, "js");
}
}
@Override
protected void resolveProjectClasspathEntries(IJavaScriptProject project,
boolean includeSource) {
try {
resolveClasspathEntries(project, Arrays.asList(project.getRawIncludepath()), Loader.EXTENSION, includeSource, true);
} catch (JavaScriptModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,31 @@
package com.ibm.wala.ide.util;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.wst.jsdt.core.IJavaScriptProject;
import org.eclipse.wst.jsdt.core.JavaScriptCore;
import com.ibm.wala.util.functions.Function;
public class JavaScriptHeadlessUtil extends HeadlessUtil {
public static IJavaScriptProject getJavaScriptProjectFromWorkspace(final String projectName) {
IJavaScriptProject jp = getProjectFromWorkspace(new Function<IProject, IJavaScriptProject>() {
public IJavaScriptProject apply(IProject p) {
try {
if (p.hasNature(JavaScriptCore.NATURE_ID)) {
IJavaScriptProject jp = JavaScriptCore.create(p);
if (jp != null && jp.getElementName().equals(projectName)) {
return jp;
}
}
} catch (CoreException e) {
}
// failed to match
return null;
}
});
return jp;
}
}

View File

@ -55,9 +55,9 @@ public class JsdtUtil {
};
public static Set<ModuleEntry> getJavaScriptCodeFromProject(String project) throws IOException, CoreException {
IJavaScriptProject p = HeadlessUtil.getJavaScriptProjectFromWorkspace(project);
IJavaScriptProject p = JavaScriptHeadlessUtil.getJavaScriptProjectFromWorkspace(project);
JSCallGraphUtil.setTranslatorFactory(new CAstRhinoTranslatorFactory());
AnalysisScope s = EclipseProjectPath.make(p).toAnalysisScope(new CAstAnalysisScope(JSCallGraphUtil.makeLoaders(), Collections.singleton(JavaScriptLoader.JS)));
AnalysisScope s = JavaScriptEclipseProjectPath.make(p).toAnalysisScope(new CAstAnalysisScope(JSCallGraphUtil.makeLoaders(), Collections.singleton(JavaScriptLoader.JS)));
List<Module> modules = s.getModules(JavaScriptTypes.jsLoader);
Set<ModuleEntry> mes = HashSetFactory.make();

View File

@ -19,7 +19,9 @@ Require-Bundle: com.ibm.wala.shrike,
com.ibm.wala.cast;bundle-version="1.0.0",
com.ibm.wala.cast.js;bundle-version="1.0.0",
com.ibm.wala.cast.js.rhino;bundle-version="1.0.0",
org.eclipse.wst.jsdt.ui;bundle-version="1.1.4"
org.eclipse.wst.jsdt.ui;bundle-version="1.1.4",
com.ibm.wala.ide.jdt;bundle-version="1.0.0",
com.ibm.wala.ide.jsdt;bundle-version="1.0.0"
Bundle-Localization: plugin
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Bundle-ActivationPolicy: lazy

View File

@ -16,7 +16,8 @@ import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jdt.core.IJavaProject;
import com.ibm.wala.ide.util.EclipseProjectPath;
import com.ibm.wala.ide.util.EclipseProjectPath.AnalysisScopeType;
import com.ibm.wala.ide.util.JavaEclipseProjectPath;
import com.ibm.wala.ide.util.JdtUtil;
/**
@ -33,7 +34,7 @@ public class Main implements IApplication {
Collection<IJavaProject> jp = JdtUtil.getWorkspaceJavaProjects();
for (IJavaProject p : jp) {
System.out.println(p);
EclipseProjectPath path = EclipseProjectPath.make(p);
JavaEclipseProjectPath path = JavaEclipseProjectPath.make(p, AnalysisScopeType.SOURCE_FOR_PROJ_AND_LINKED_PROJS);
System.out.println("Path: " + path);
}
return null;

View File

@ -19,8 +19,8 @@ import com.ibm.wala.cast.js.test.JavaScriptTestPlugin;
import com.ibm.wala.cast.js.translator.CAstRhinoTranslatorFactory;
import com.ibm.wala.classLoader.ModuleEntry;
import com.ibm.wala.ide.tests.util.EclipseTestUtil;
import com.ibm.wala.ide.util.EclipseProjectPath;
import com.ibm.wala.ide.util.HeadlessUtil;
import com.ibm.wala.ide.util.JavaScriptEclipseProjectPath;
import com.ibm.wala.ide.util.JavaScriptHeadlessUtil;
import com.ibm.wala.ide.util.JsdtUtil;
import com.ibm.wala.ide.util.JsdtUtil.CGInfo;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
@ -42,16 +42,16 @@ public class JSProjectScopeTest {
@Test
public void testOpenProject() {
IJavaScriptProject p = HeadlessUtil.getJavaScriptProjectFromWorkspace(jsTestDataProject);
IJavaScriptProject p = JavaScriptHeadlessUtil.getJavaScriptProjectFromWorkspace(jsTestDataProject);
System.err.println(p);
Assert.assertTrue("cannot find project", p != null);
}
@Test
public void testProjectScope() throws IOException, CoreException {
IJavaScriptProject p = HeadlessUtil.getJavaScriptProjectFromWorkspace(jsTestDataProject);
IJavaScriptProject p = JavaScriptHeadlessUtil.getJavaScriptProjectFromWorkspace(jsTestDataProject);
JSCallGraphUtil.setTranslatorFactory(new CAstRhinoTranslatorFactory());
AnalysisScope s = EclipseProjectPath.make(p).toAnalysisScope(new CAstAnalysisScope(JSCallGraphUtil.makeLoaders(), Collections.singleton(JavaScriptLoader.JS)));
AnalysisScope s = JavaScriptEclipseProjectPath.make(p).toAnalysisScope(new CAstAnalysisScope(JSCallGraphUtil.makeLoaders(), Collections.singleton(JavaScriptLoader.JS)));
System.err.println(s);
Assert.assertTrue("cannot make scope", s != null);
}

View File

@ -9,24 +9,18 @@ Bundle-Vendor: %providerName
Bundle-Localization: plugin
Require-Bundle: com.ibm.wala.cast.js.rhino;bundle-version="1.0.0",
com.ibm.wala.cast.js;bundle-version="1.0.0",
com.ibm.wala.cast.java.polyglot;bundle-version="1.0.0",
com.ibm.wala.cast.java,
com.ibm.wala.cast;bundle-version="1.0.0",
com.ibm.wala.core,
com.ibm.wala.shrike,
org.eclipse.wst.jsdt.ui;bundle-version="1.1.201",
org.eclipse.wst.jsdt.core;bundle-version="1.1.4",
org.eclipse.pde.core,
org.eclipse.jdt.core,
org.eclipse.jface,
org.eclipse.ui,
org.eclipse.core.resources,
org.eclipse.core.runtime
Bundle-ActivationPolicy: lazy
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Export-Package: com.ibm.wala.cast.java.client.polyglot,
com.ibm.wala.ide,
com.ibm.wala.ide.classloader,
Export-Package: com.ibm.wala.ide.classloader,
com.ibm.wala.ide.client,
com.ibm.wala.ide.plugin,
com.ibm.wala.ide.ui,
com.ibm.wala.ide.util

View File

@ -1,32 +0,0 @@
package com.ibm.wala.cast.java.client.polyglot;
import java.io.IOException;
import java.util.Collections;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.wst.jsdt.core.IJavaScriptProject;
import com.ibm.wala.cast.ipa.callgraph.CAstAnalysisScope;
import com.ibm.wala.cast.js.loader.JavaScriptLoader;
import com.ibm.wala.cast.js.loader.JavaScriptLoaderFactory;
import com.ibm.wala.cast.js.translator.CAstRhinoTranslatorFactory;
import com.ibm.wala.ide.util.EclipseProjectPath;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
public class EclipseJavaScriptAnalysisEngine extends EclipseProjectSourceAnalysisEngine<IJavaScriptProject> {
public EclipseJavaScriptAnalysisEngine(IJavaScriptProject project) throws IOException, CoreException {
super(project, "js");
}
@Override
protected AnalysisScope makeSourceAnalysisScope() {
return new CAstAnalysisScope(new JavaScriptLoaderFactory(new CAstRhinoTranslatorFactory()), Collections.singleton(JavaScriptLoader.JS));
}
@Override
protected EclipseProjectPath createProjectPath(IJavaScriptProject project) throws IOException, CoreException {
return EclipseProjectPath.make(project);
}
}

View File

@ -1,217 +0,0 @@
/*******************************************************************************
* 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.ide;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.progress.IProgressService;
import com.ibm.wala.classLoader.Module;
import com.ibm.wala.ide.util.EclipseProjectPath;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.util.collections.HashSetFactory;
import com.ibm.wala.util.debug.Assertions;
/**
* An Eclipse action that analyzes a Java selection
*/
public abstract class AbstractJavaAnalysisAction implements IObjectActionDelegate, IRunnableWithProgress {
/**
* The current {@link ISelection} highlighted in the Eclipse workspace
*/
private ISelection currentSelection;
public AbstractJavaAnalysisAction() {
super();
}
/*
* @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
*/
public void setActivePart(IAction action, IWorkbenchPart targetPart) {
}
/**
* Compute an analysis scope for the current selection
*/
public static AnalysisScope computeScope(IStructuredSelection selection) throws IOException {
return computeScope(selection, EclipseProjectPath.AnalysisScopeType.NO_SOURCE);
}
/**
* Compute an analysis scope for the current selection
*
* @param scopeType should analysis use the source files in the Eclipse projects rather than the class files.
*/
public static AnalysisScope computeScope(final IStructuredSelection selection,
final EclipseProjectPath.AnalysisScopeType scopeType) throws IOException
{
if (selection == null) {
throw new IllegalArgumentException("null selection");
}
final Collection<EclipseProjectPath> projectPaths = new LinkedList<EclipseProjectPath>();
Job job = new Job("Compute project paths") {
@SuppressWarnings("unchecked")
@Override
protected IStatus run(IProgressMonitor monitor) {
for (Iterator it = selection.iterator(); it.hasNext();) {
Object object = it.next();
if (object instanceof IJavaElement) {
IJavaElement e = (IJavaElement) object;
IJavaProject jp = e.getJavaProject();
try {
projectPaths.add(EclipseProjectPath.make(jp, scopeType));
} catch (CoreException e1) {
e1.printStackTrace();
// skip and continue
} catch (IOException e2) {
e2.printStackTrace();
return new Status(IStatus.ERROR, "", 0, "", e2);
}
} else {
Assertions.UNREACHABLE(object.getClass());
}
}
return Status.OK_STATUS;
}
};
// lock the whole workspace
job.setRule(ResourcesPlugin.getWorkspace().getRoot());
job.schedule();
try {
job.join();
IStatus result = job.getResult();
if (result.getSeverity() == IStatus.ERROR) {
Throwable exception = result.getException();
if (exception instanceof IOException) {
throw (IOException) exception;
} else if (exception instanceof RuntimeException) {
throw (RuntimeException) exception;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
assert false;
}
AnalysisScope scope = mergeProjectPaths(projectPaths);
return scope;
}
/**
* compute the java projects represented by the current selection
*/
@SuppressWarnings("unchecked")
protected Collection<IJavaProject> computeJavaProjects() {
IStructuredSelection selection = (IStructuredSelection) currentSelection;
Collection<IJavaProject> projects = HashSetFactory.make();
for (Iterator it = selection.iterator(); it.hasNext();) {
Object object = it.next();
if (object instanceof IJavaElement) {
IJavaElement e = (IJavaElement) object;
IJavaProject jp = e.getJavaProject();
projects.add(jp);
} else {
Assertions.UNREACHABLE(object.getClass());
}
}
return projects;
}
/**
* create an analysis scope as the union of a bunch of EclipseProjectPath
*/
private static AnalysisScope mergeProjectPaths(Collection<EclipseProjectPath> projectPaths) throws IOException {
AnalysisScope scope = AnalysisScope.createJavaAnalysisScope();
Collection<Module> seen = HashSetFactory.make();
// to avoid duplicates, we first add all application modules, then
// extension
// modules, then primordial
buildScope(ClassLoaderReference.Application, projectPaths, scope, seen);
buildScope(ClassLoaderReference.Extension, projectPaths, scope, seen);
buildScope(ClassLoaderReference.Primordial, projectPaths, scope, seen);
return scope;
}
/**
* Enhance an {@link AnalysisScope} to include in a particular loader, elements from a set of Eclipse projects
*
* @param loader the class loader in which new {@link Module}s will live
* @param projectPaths Eclipse project paths to add to the analysis scope
* @param scope the {@link AnalysisScope} under construction. This will be mutated.
* @param seen set of {@link Module}s which have already been seen, and should not be added to the analysis scope
*/
private static void buildScope(ClassLoaderReference loader, Collection<EclipseProjectPath> projectPaths, AnalysisScope scope,
Collection<Module> seen) throws IOException {
for (EclipseProjectPath path : projectPaths) {
AnalysisScope pScope = path.toAnalysisScope((File) null);
for (Module m : pScope.getModules(loader)) {
if (!seen.contains(m)) {
seen.add(m);
scope.addToScope(loader, m);
}
}
}
}
/*
* @see IActionDelegate#run(IAction)
*/
public void run(IAction action) {
IProgressService progressService = PlatformUI.getWorkbench().getProgressService();
try {
progressService.busyCursorWhile(this);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/*
* @see IActionDelegate#selectionChanged(IAction, ISelection)
*/
public void selectionChanged(IAction action, ISelection selection) {
currentSelection = selection;
}
/**
* @return The current {@link ISelection} highlighted in the Eclipse workspace
*/
public ISelection getCurrentSelection() {
return currentSelection;
}
}

View File

@ -8,7 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.cast.java.client.polyglot;
package com.ibm.wala.ide.client;
import java.io.File;
import java.io.IOException;
@ -17,7 +17,6 @@ import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import com.ibm.wala.cast.java.client.impl.ZeroCFABuilderFactory;
import com.ibm.wala.client.AbstractAnalysisEngine;
import com.ibm.wala.ide.util.EclipseProjectPath;
import com.ibm.wala.ipa.callgraph.AnalysisCache;
@ -42,6 +41,8 @@ abstract public class EclipseProjectAnalysisEngine<P> extends AbstractAnalysisEn
abstract protected EclipseProjectPath createProjectPath(P project) throws IOException, CoreException;
abstract protected CallGraphBuilder getCallGraphBuilder(IClassHierarchy cha, AnalysisOptions options, AnalysisCache cache);
@Override
public void buildAnalysisScope() throws IOException {
super.scope = ePath.toAnalysisScope(new File(getExclusionsFile()));
@ -51,8 +52,4 @@ abstract public class EclipseProjectAnalysisEngine<P> extends AbstractAnalysisEn
return ePath;
}
@Override
protected CallGraphBuilder getCallGraphBuilder(IClassHierarchy cha, AnalysisOptions options, AnalysisCache cache) {
return new ZeroCFABuilderFactory().make(options, cache, cha, scope, false);
}
}

View File

@ -8,7 +8,7 @@
* Contributors:
* IBM Corporation - initial API and implementation
*****************************************************************************/
package com.ibm.wala.cast.java.client.polyglot;
package com.ibm.wala.ide.client;
import java.io.File;
import java.io.IOException;
@ -16,7 +16,6 @@ import java.io.IOException;
import org.eclipse.core.runtime.CoreException;
import com.ibm.wala.cast.ir.ssa.AstIRFactory;
import com.ibm.wala.cast.java.ipa.callgraph.JavaSourceAnalysisScope;
import com.ibm.wala.classLoader.BinaryDirectoryTreeModule;
import com.ibm.wala.classLoader.Module;
import com.ibm.wala.classLoader.SourceDirectoryTreeModule;
@ -75,13 +74,13 @@ abstract public class EclipseProjectSourceAnalysisEngine<P> extends EclipseProje
if (getExclusionsFile() != null) {
scope.setExclusions(FileOfClasses.createFileOfClasses(new File(getExclusionsFile())));
}
EclipseProjectPath epath = getEclipseProjectPath();
EclipseProjectPath<?,?> epath = getEclipseProjectPath();
for (Module m : epath.getModules(Loader.PRIMORDIAL, true)) {
scope.addToScope(scope.getPrimordialLoader(), m);
}
ClassLoaderReference app = scope.getApplicationLoader();
ClassLoaderReference src = ((JavaSourceAnalysisScope) scope).getSourceLoader();
ClassLoaderReference src = getSourceLoader();
for (Module m : epath.getModules(Loader.APPLICATION, true)) {
if (m instanceof SourceDirectoryTreeModule) {
scope.addToScope(src, m);
@ -103,6 +102,8 @@ abstract public class EclipseProjectSourceAnalysisEngine<P> extends EclipseProje
}
}
protected abstract ClassLoaderReference getSourceLoader();
@Override
public AnalysisOptions getDefaultOptions(Iterable<Entrypoint> entrypoints) {
AnalysisOptions options = new AnalysisOptions(getScope(), entrypoints);

View File

@ -30,20 +30,11 @@ import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IClasspathContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.osgi.service.resolver.BundleDescription;
import org.eclipse.pde.core.plugin.IPluginModelBase;
import org.eclipse.pde.internal.core.ClasspathUtilCore;
import org.eclipse.pde.internal.core.PDECore;
import org.eclipse.pde.internal.core.PDEStateHelper;
import org.eclipse.wst.jsdt.core.IIncludePathEntry;
import org.eclipse.wst.jsdt.core.IJavaScriptProject;
import org.eclipse.wst.jsdt.core.JavaScriptCore;
import org.eclipse.wst.jsdt.core.JavaScriptModelException;
import com.ibm.wala.cast.js.types.JavaScriptTypes;
import com.ibm.wala.classLoader.BinaryDirectoryTreeModule;
@ -71,7 +62,16 @@ import com.ibm.wala.util.debug.Assertions;
* </ul>
*/
@SuppressWarnings("restriction")
public class EclipseProjectPath {
public abstract class EclipseProjectPath<E, P> {
protected abstract P makeProject(IProject p);
protected abstract E resolve(E entry);
protected abstract void resolveClasspathEntry(P project, E entry, Loader loader, boolean includeSource, boolean cpeFromMainProject);
protected abstract void resolveProjectClasspathEntries(P project, boolean includeSource);
/**
* Eclipse projects are modeled with 3 loaders, as described above.
@ -98,12 +98,12 @@ public class EclipseProjectPath {
// SJF: Intentionally do not use HashMapFactory, since the Loader keys in the following must use
// identityHashCode. TODO: fix this source of non-determinism?
private final Map<Loader, List<Module>> modules = new HashMap<Loader, List<Module>>();
protected final Map<Loader, List<Module>> modules = new HashMap<Loader, List<Module>>();
/**
* Classpath entries that have already been resolved and added to the scope.
*/
private final Collection<IClasspathEntry> alreadyResolved = HashSetFactory.make();
protected final Collection<E> alreadyResolved = HashSetFactory.make();
/**
* Which source files, if any, should be included in the analysis scope.
@ -117,7 +117,7 @@ public class EclipseProjectPath {
}
}
protected EclipseProjectPath(IJavaProject project, AnalysisScopeType scopeType) throws IOException, CoreException {
protected EclipseProjectPath(IProject project, AnalysisScopeType scopeType) throws IOException, CoreException {
this(scopeType);
assert project != null;
@ -126,58 +126,14 @@ public class EclipseProjectPath {
}
boolean includeSource = (scopeType != AnalysisScopeType.NO_SOURCE);
resolveJavaProjectClasspathEntries(project, includeSource);
resolveProjectClasspathEntries(makeProject(project), includeSource);
if (isPluginProject(project)) {
resolvePluginClassPath(project.getProject(), includeSource);
resolvePluginClassPath(project, includeSource);
}
}
public static EclipseProjectPath make(IJavaProject project) throws IOException, CoreException {
return make(project, AnalysisScopeType.NO_SOURCE);
}
public static EclipseProjectPath make(IJavaProject project, AnalysisScopeType scopeType) throws IOException, CoreException {
return new EclipseProjectPath(project, scopeType);
}
protected EclipseProjectPath(IJavaScriptProject p) throws IOException, CoreException {
this(AnalysisScopeType.SOURCE_FOR_PROJ_AND_LINKED_PROJS);
resolveJavaScriptProjectClasspathEntries(p);
}
public static EclipseProjectPath make(IJavaScriptProject p) throws IOException, CoreException {
return new EclipseProjectPath(p);
}
/**
* Figure out what a classpath entry means and add it to the appropriate set of modules
*/
private void resolveJavaClasspathEntry(IJavaProject project, IClasspathEntry entry, Loader loader, boolean includeSource, boolean cpeFromMainProject)
throws JavaModelException, IOException {
IClasspathEntry e = JavaCore.getResolvedClasspathEntry(entry);
if (alreadyResolved.contains(e)) {
return;
} else {
alreadyResolved.add(e);
}
if (e.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
IClasspathContainer cont = JavaCore.getClasspathContainer(entry.getPath(), project);
IClasspathEntry[] entries = cont.getClasspathEntries();
resolveJavaClasspathEntries(project, entries, cont.getKind() == IClasspathContainer.K_APPLICATION ? loader : Loader.PRIMORDIAL,
includeSource, false);
} else if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
resolveLibraryPathEntry(loader, e.getPath());
} else if (e.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
resolveSourcePathEntry(loader, includeSource, cpeFromMainProject, e.getPath(), e.getOutputLocation(), "java");
} else if (e.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
resolveProjectPathEntry(loader, includeSource, e.getPath());
} else {
throw new RuntimeException("unexpected entry " + e);
}
}
private void resolveLibraryPathEntry(Loader loader, IPath p) throws IOException {
protected void resolveLibraryPathEntry(Loader loader, IPath p) throws IOException {
File file = makeAbsolute(p).toFile();
JarFile j;
try {
@ -195,7 +151,7 @@ public class EclipseProjectPath {
}
}
private void resolveSourcePathEntry(Loader loader, boolean includeSource, boolean cpeFromMainProject, IPath p, IPath o, String fileExtension) {
protected void resolveSourcePathEntry(Loader loader, boolean includeSource, boolean cpeFromMainProject, IPath p, IPath o, String fileExtension) {
if (includeSource) {
List<Module> s = MapUtil.findOrCreateList(modules, loader);
s.add(new EclipseSourceDirectoryTreeModule(p, fileExtension));
@ -206,29 +162,18 @@ public class EclipseProjectPath {
}
}
private void resolveProjectPathEntry(Loader loader, boolean includeSource, IPath p) throws IOException {
protected void resolveProjectPathEntry(Loader loader, boolean includeSource, IPath p) throws IOException {
IPath projectPath = makeAbsolute(p);
IWorkspace ws = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = ws.getRoot();
IProject project = (IProject) root.getContainerForLocation(projectPath);
try {
if (project.hasNature(JavaCore.NATURE_ID)) {
IJavaProject javaProject = JavaCore.create(project);
if (isPluginProject(javaProject)) {
resolvePluginClassPath(javaProject.getProject(), includeSource);
P javaProject = makeProject(project);
if (javaProject != null) {
if (isPluginProject(project)) {
resolvePluginClassPath(project, includeSource);
}
resolveJavaClasspathEntries(javaProject, javaProject.getRawClasspath(), loader,
scopeType == AnalysisScopeType.SOURCE_FOR_PROJ_AND_LINKED_PROJS ? includeSource : false, false);
File output = makeAbsolute(javaProject.getOutputLocation()).toFile();
List<Module> s = MapUtil.findOrCreateList(modules, loader);
if (!includeSource) {
if (output.exists()) {
s.add(new BinaryDirectoryTreeModule(output));
}
}
} else if (project.hasNature(JavaScriptCore.NATURE_ID)) {
IJavaScriptProject jsProject = JavaScriptCore.create(project);
resolveJavaScriptClasspathEntries(jsProject, jsProject.getRawIncludepath(), false);
resolveProjectClasspathEntries(javaProject, scopeType == AnalysisScopeType.SOURCE_FOR_PROJ_AND_LINKED_PROJS ? includeSource : false);
}
} catch (CoreException e1) {
e1.printStackTrace();
@ -268,13 +213,13 @@ public class EclipseProjectPath {
if (bd == null) {
throw new IllegalStateException("bundle description was null for " + p);
}
resolveBundleDescriptionClassPath(JavaCore.create(p), bd, Loader.APPLICATION, includeSource);
resolveBundleDescriptionClassPath(makeProject(p), bd, Loader.APPLICATION, includeSource);
}
/**
* traverse a bundle description and populate the analysis scope accordingly
*/
private void resolveBundleDescriptionClassPath(IJavaProject project, BundleDescription bd, Loader loader, boolean includeSource) throws CoreException,
private void resolveBundleDescriptionClassPath(P project, BundleDescription bd, Loader loader, boolean includeSource) throws CoreException,
IOException {
assert bd != null;
if (alreadyProcessed(bd)) {
@ -285,13 +230,7 @@ public class EclipseProjectPath {
// handle the classpath entries for bd
ArrayList l = new ArrayList();
ClasspathUtilCore.addLibraries(findModel(bd), l);
IClasspathEntry[] entries = new IClasspathEntry[l.size()];
int i = 0;
for (Object o : l) {
IClasspathEntry e = (IClasspathEntry) o;
entries[i++] = e;
}
resolveJavaClasspathEntries(project, entries, loader, includeSource, false);
resolveClasspathEntries(project, l, loader, includeSource, false);
// recurse to handle dependencies. put these in the Extension loader
for (BundleDescription b : PDEStateHelper.getImportedBundles(bd)) {
@ -315,8 +254,8 @@ public class EclipseProjectPath {
/**
* Is javaProject a plugin project?
*/
private boolean isPluginProject(IJavaProject javaProject) {
IPluginModelBase model = findModel(javaProject.getProject());
private boolean isPluginProject(IProject project) {
IPluginModelBase model = findModel(project);
if (model == null) {
return false;
}
@ -334,10 +273,9 @@ public class EclipseProjectPath {
return true;
}
protected void resolveJavaClasspathEntries(IJavaProject project, IClasspathEntry[] entries, Loader loader, boolean includeSource,
boolean entriesFromTopLevelProject) throws JavaModelException, IOException {
for (int i = 0; i < entries.length; i++) {
resolveJavaClasspathEntry(project, entries[i], loader, includeSource, entriesFromTopLevelProject);
protected void resolveClasspathEntries(P project, List l, Loader loader, boolean includeSource, boolean entriesFromTopLevelProject) {
for (int i = 0; i < l.size(); i++) {
resolveClasspathEntry(project, resolve((E)l.get(i)), loader, includeSource, entriesFromTopLevelProject);
}
}
@ -353,46 +291,7 @@ public class EclipseProjectPath {
}
return absolutePath;
}
private void resolveJavaProjectClasspathEntries(IJavaProject project, boolean includeSource) throws JavaModelException, IOException {
resolveJavaClasspathEntries(project, project.getRawClasspath(), Loader.EXTENSION, includeSource, true);
if (!includeSource) {
File dir = makeAbsolute(project.getOutputLocation()).toFile();
if (!dir.isDirectory()) {
System.err.println("PANIC: project output location is not a directory: " + dir);
} else {
MapUtil.findOrCreateList(modules, Loader.APPLICATION).add(new BinaryDirectoryTreeModule(dir));
}
}
}
private void resolveJavaScriptProjectClasspathEntries(IJavaScriptProject project) throws JavaModelException, IOException {
try {
resolveJavaScriptClasspathEntries(project, project.getRawIncludepath(), true);
} catch (JavaScriptModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void resolveJavaScriptClasspathEntries(IJavaScriptProject p, IIncludePathEntry[] entries, boolean entriesFromTopLevelProject) {
for (int i = 0; i < entries.length; i++) {
resolveJavaScriptClasspathEntry(p, entries[i], entriesFromTopLevelProject);
}
}
private void resolveJavaScriptClasspathEntry(IJavaScriptProject p, IIncludePathEntry e, boolean entriesFromTopLevelProject) {
e = JavaScriptCore.getResolvedIncludepathEntry(e);
switch (e.getEntryKind()) {
case IIncludePathEntry.CPE_SOURCE:
resolveSourcePathEntry(Loader.JAVASCRIPT, true, entriesFromTopLevelProject, e.getPath(), null, "js");
}
}
/**
* Convert this path to a WALA analysis scope
*

View File

@ -23,10 +23,6 @@ import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.wst.jsdt.core.IJavaScriptProject;
import org.eclipse.wst.jsdt.core.JavaScriptCore;
import com.ibm.wala.classLoader.ModuleEntry;
import com.ibm.wala.ide.classloader.EclipseSourceFileModule;
@ -66,59 +62,7 @@ public class HeadlessUtil {
return p;
}
/**
* compute the analysis scope for a project in the current workspace
* @throws IOException
* @throws CoreException
*/
public static AnalysisScope computeJavaScope(final String projectName) throws IOException, CoreException {
if (projectName == null) {
throw new IllegalArgumentException("null projectName");
}
IJavaProject jp = getJavaProjectFromWorkspace(projectName);
EclipseProjectPath path = EclipseProjectPath.make(jp);
return path.toAnalysisScope((File)null);
}
private static IJavaProject getJavaProjectFromWorkspace(final String projectName) {
IJavaProject jp = getProjectFromWorkspace(new Function<IProject, IJavaProject>() {
public IJavaProject apply(IProject p) {
try {
if (p.hasNature(JavaCore.NATURE_ID)) {
IJavaProject jp = JavaCore.create(p);
if (jp != null && jp.getElementName().equals(projectName)) {
return jp;
}
}
} catch (CoreException e) {
}
// failed to match
return null;
}
});
return jp;
}
public static IJavaScriptProject getJavaScriptProjectFromWorkspace(final String projectName) {
IJavaScriptProject jp = getProjectFromWorkspace(new Function<IProject, IJavaScriptProject>() {
public IJavaScriptProject apply(IProject p) {
try {
if (p.hasNature(JavaScriptCore.NATURE_ID)) {
IJavaScriptProject jp = JavaScriptCore.create(p);
if (jp != null && jp.getElementName().equals(projectName)) {
return jp;
}
}
} catch (CoreException e) {
}
// failed to match
return null;
}
});
return jp;
}
private static <X> X getProjectFromWorkspace(Function<IProject, X> pred) {
protected static <X> X getProjectFromWorkspace(Function<IProject, X> pred) {
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IPath workspaceRootPath = workspaceRoot.getLocation();
System.out.println("workspace: " + workspaceRootPath.toOSString());

View File

@ -1,575 +0,0 @@
/*******************************************************************************
* 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.ide.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaModel;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageDeclaration;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeParameter;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchParticipant;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.SearchRequestor;
import org.eclipse.jface.viewers.StructuredSelection;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.collections.HashSetFactory;
/**
* Convenience methods to get information from JDT {@link IJavaElement} model.
*/
public class JdtUtil {
public static String getFilePath(IJavaElement javaElt) {
if (javaElt == null) {
throw new IllegalArgumentException("javaElt is null");
}
try {
IPath p = javaElt.getPath();
if (p == null) {
throw new IllegalArgumentException("javaElt has null path");
}
String filePath = p.toString();
return filePath;
} catch (Exception e) {
throw new IllegalArgumentException("malformed javaElt: " + javaElt, e);
}
}
public static String getPackageName(ICompilationUnit cu) {
if (cu == null) {
throw new IllegalArgumentException("cu is null");
}
try {
IPackageDeclaration[] pkgDecl = cu.getPackageDeclarations();
// TODO: handle default package?
if (pkgDecl != null && pkgDecl.length > 0) {
String packageName = pkgDecl[0].getElementName();
return packageName;
}
} catch (JavaModelException e) {
}
return "";
}
public static String getFullyQualifiedClassName(IType type) {
if (type == null) {
throw new IllegalArgumentException("type is null");
}
ICompilationUnit cu = (ICompilationUnit) type.getParent();
String packageName = getPackageName(cu);
String className = type.getElementName();
String fullyQName = packageName + "." + className;
return fullyQName;
}
public static String getClassName(IType type) {
if (type == null) {
throw new IllegalArgumentException("type is null");
}
String className = type.getElementName();
return className;
}
/**
* Return a unique string representing the specified Java element across projects in the workspace. The returned string can be
* used as a handle to create JavaElement by 'JavaCore.create(String)'
*
* For example, suppose we have the method 'fooPackage.barPackage.FooClass.fooMethod(int)' which is in the 'FooProject' and source
* folder 'src' the handle would be '=FooProject/src<fooPackage.barPackage{FooClass.java[FooClass~fooMethod~I'
*
* @param javaElt
* @throws IllegalArgumentException if javaElt is null
*/
public static String getJdtHandleString(IJavaElement javaElt) {
if (javaElt == null) {
throw new IllegalArgumentException("javaElt is null");
}
return javaElt.getHandleIdentifier();
}
@Deprecated
public static IJavaElement createJavaElementFromJdtHandle(String jdtHandle) {
return JavaCore.create(jdtHandle);
}
public static IType[] getClasses(ICompilationUnit cu) {
if (cu == null) {
throw new IllegalArgumentException("cu is null");
}
try {
return cu.getAllTypes();
} catch (JavaModelException e) {
}
return null;
}
public static IJavaProject getProject(IJavaElement javaElt) {
if (javaElt == null) {
throw new IllegalArgumentException("javaElt is null");
}
IJavaProject javaProject = javaElt.getJavaProject();
return javaProject;
}
public static String getProjectName(IJavaProject javaProject) {
if (javaProject == null) {
throw new IllegalArgumentException("javaProject is null");
}
return javaProject.getElementName();
}
/**
* @param typeSignature Some of the type signatures examples are "QString;" (String) and "I" (int) The type signatures may be
* either unresolved (for source types) or resolved (for binary types), and either basic (for basic types) or rich (for
* parameterized types). See {@link Signature} for details.
*/
public static String getHumanReadableType(String typeSignature) {
String simpleName = Signature.getSignatureSimpleName(typeSignature);
return simpleName;
}
public static IJavaProject getJavaProject(IFile appJar) {
if (appJar == null) {
throw new IllegalArgumentException("appJar is null");
}
String projectName = appJar.getProject().getName();
return getJavaProject(projectName);
}
public static IJavaProject getJavaProject(String projectName) {
if (projectName == null) {
throw new IllegalArgumentException("null projectName");
}
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IJavaModel javaModel = JavaCore.create(workspaceRoot);
IJavaProject javaProject = javaModel.getJavaProject(projectName);
return javaProject;
}
/**
* compute the java projects in the active workspace
*/
public static Collection<IJavaProject> getWorkspaceJavaProjects() {
Collection<IJavaProject> result = HashSetFactory.make();
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
for (IProject p : workspaceRoot.getProjects()) {
try {
if (p.hasNature(JavaCore.NATURE_ID)) {
IJavaProject jp = JavaCore.create(p);
if (jp != null) {
result.add(jp);
}
}
} catch (CoreException e) {
// do nothing
}
}
return result;
}
/**
* Find the {@link IType} in the workspace corresponding to a class name.
*
* @return null if not found
* @throws IllegalArgumentException if projects == null
*/
public static IType findJavaClassInProjects(String fullyQualifiedName, Collection<IJavaProject> projects)
throws IllegalArgumentException {
if (projects == null) {
throw new IllegalArgumentException("projects == null");
}
for (IJavaProject project : projects) {
try {
IType t = project.findType(fullyQualifiedName);
if (t != null) {
return t;
}
} catch (JavaModelException e) {
e.printStackTrace();
}
}
System.err.println("failed to find " + fullyQualifiedName);
return null;
// IJavaElement[] arr = new IJavaElement[projects.size()];
// projects.toArray(arr);
// IJavaSearchScope scope = SearchEngine.createJavaSearchScope(arr, false);
//
// return searchForJavaClass(className, scope);
}
public static IType findJavaClassInResources(String className, Collection<IResource> resources) {
if (resources == null) {
throw new IllegalArgumentException("null resources");
}
Collection<IJavaProject> projects = HashSetFactory.make();
for (IResource r : resources) {
projects.add(JavaCore.create(r).getJavaProject());
}
return findJavaClassInProjects(className, projects);
}
// private static IType searchForJavaClass(String className, IJavaSearchScope scope) {
// SearchPattern p = SearchPattern.createPattern(className, IJavaSearchConstants.CLASS_AND_INTERFACE,
// IJavaSearchConstants.DECLARATIONS, SearchPattern.R_EXACT_MATCH);
// SearchEngine engine = new SearchEngine();
// final Collection<IJavaElement> kludge = HashSetFactory.make();
// SearchRequestor requestor = new SearchRequestor() {
//
// @Override
// public void acceptSearchMatch(SearchMatch match) throws CoreException {
// kludge.add((IJavaElement) match.getElement());
// }
//
// };
// try {
// engine.search(p, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, null);
// } catch (CoreException e) {
// e.printStackTrace();
// }
// if (kludge.size() == 1) {
// System.err.println("Found " + className);
// return (IType) kludge.iterator().next();
// } else {
// System.err.println("Failed to find " + className + " " + kludge.size());
// return null;
// }
// }
/**
* Find the IMethod in the workspace corresponding to a method selector.
*
* TODO: this is way too slow. figure out something better.
*
* @return null if not found
*/
public static IMethod findJavaMethodInProjects(String klass, String selector, Collection<IJavaProject> projects) {
if (klass == null) {
throw new IllegalArgumentException("null klass");
}
if (projects == null) {
throw new IllegalArgumentException("null projects");
}
IType type = null;
try {
type = findJavaClassInProjects(klass, projects);
} catch (Throwable t) {
return null;
}
if (type == null) {
return null;
}
String name = parseForName(selector, type);
String[] paramTypes = parseForParameterTypes(selector);
IMethod m = type.getMethod(name, paramTypes);
IMethod[] methods = type.findMethods(m);
if (methods != null && methods.length == 1) {
return methods[0];
} else {
// methods is null. probably got screwed by generics.
// i spent 5 hours trying to figure out how to fix this correctly
// and failed. implementing a miserable hack instead.
// Need to consult a guru to figure out how to do this.
try {
List<IMethod> matches = new ArrayList<IMethod>();
Collection<String> typeParameterNames = getTypeParameterNames(type);
METHODS: for (IMethod x : type.getMethods()) {
if (x.getElementName().equals(name)) {
if (x.getParameterTypes().length == paramTypes.length) {
for (int i = 0; i < x.getParameterTypes().length; i++) {
String s1 = Signature.getTypeErasure(Signature.getSignatureSimpleName(x.getParameterTypes()[i]));
String s2 = Signature.getTypeErasure(Signature.getSignatureSimpleName(paramTypes[i]));
if (typeParameterNames.contains(s1)) {
// s1 is a type parameter to the class. optimistically assume
// the types match.
} else {
if (!s1.equals(s2)) {
// no match
continue METHODS;
}
}
}
matches.add(x);
}
}
}
if (matches.size() == 1) {
return matches.get(0);
} else {
System.err.println("findJavaMethodInWorkspace FAILED TO MATCH " + m);
return null;
}
} catch (JavaModelException e) {
e.printStackTrace();
return null;
}
}
}
public static Collection<String> getTypeParameterNames(IType type) throws IllegalArgumentException, JavaModelException {
if (type == null) {
throw new IllegalArgumentException("type == null");
}
ITypeParameter[] tp = type.getTypeParameters();
Collection<String> typeParameterNames = HashSetFactory.make(tp.length);
for (ITypeParameter p : tp) {
typeParameterNames.add(p.getElementName());
}
return typeParameterNames;
}
public static String parseForName(String selector, IType type) {
if (selector == null) {
throw new IllegalArgumentException("selector is null");
}
try {
String result = selector.substring(0, selector.indexOf('('));
if (result.equals("<init>")) {
return type.getElementName();
} else {
return result;
}
} catch (StringIndexOutOfBoundsException e) {
throw new IllegalArgumentException("invalid selector: " + selector);
}
}
public static final String[] parseForParameterTypes(String selector) throws IllegalArgumentException {
try {
if (selector == null) {
throw new IllegalArgumentException("selector is null");
}
String d = selector.substring(selector.indexOf('('));
if (d.length() <= 2) {
throw new IllegalArgumentException("invalid descriptor: " + d);
}
if (d.charAt(0) != '(') {
throw new IllegalArgumentException("invalid descriptor: " + d);
}
ArrayList<String> sigs = new ArrayList<String>(10);
int i = 1;
while (true) {
switch (d.charAt(i++)) {
case TypeReference.VoidTypeCode:
sigs.add(TypeReference.VoidName.toString());
continue;
case TypeReference.BooleanTypeCode:
sigs.add(TypeReference.BooleanName.toString());
continue;
case TypeReference.ByteTypeCode:
sigs.add(TypeReference.ByteName.toString());
continue;
case TypeReference.ShortTypeCode:
sigs.add(TypeReference.ShortName.toString());
continue;
case TypeReference.IntTypeCode:
sigs.add(TypeReference.IntName.toString());
continue;
case TypeReference.LongTypeCode:
sigs.add(TypeReference.LongName.toString());
continue;
case TypeReference.FloatTypeCode:
sigs.add(TypeReference.FloatName.toString());
continue;
case TypeReference.DoubleTypeCode:
sigs.add(TypeReference.DoubleName.toString());
continue;
case TypeReference.CharTypeCode:
sigs.add(TypeReference.CharName.toString());
continue;
case TypeReference.ArrayTypeCode: {
int off = i - 1;
while (d.charAt(i) == TypeReference.ArrayTypeCode) {
++i;
}
if (d.charAt(i++) == TypeReference.ClassTypeCode) {
while (d.charAt(i++) != ';')
;
sigs.add(d.substring(off, i).replaceAll("/", "."));
} else {
sigs.add(d.substring(off, i));
}
continue;
}
case (byte) ')': // end of parameter list
return toArray(sigs);
default: {
// a class
int off = i - 1;
char c;
do {
c = d.charAt(i++);
} while (c != ',' && c != ')');
sigs.add("L" + d.substring(off, i - 1) + ";");
if (c == ')') {
return toArray(sigs);
}
continue;
}
}
}
} catch (StringIndexOutOfBoundsException e) {
throw new IllegalArgumentException("error parsing selector " + selector);
}
}
private static String[] toArray(ArrayList<String> sigs) {
int size = sigs.size();
if (size == 0) {
return new String[0];
}
Iterator<String> it = sigs.iterator();
String[] result = new String[size];
for (int j = 0; j < size; j++) {
result[j] = it.next();
}
return result;
}
/**
* Find the IMethod in the workspace corresponding to a method signature.
*
* This doesn't work for elements declared in inner classes. It's possible this is a 3.2 bug fixed in 3.3
*
* @return null if not found
*/
@Deprecated
public static IMethod findJavaMethodInWorkspaceBrokenForInnerClasses(String methodSig) {
// dammit ... this doesn't work for inner classes.
System.err.println("Search for " + methodSig);
SearchPattern p = SearchPattern.createPattern(methodSig, IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS,
SearchPattern.R_EXACT_MATCH);
IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
SearchEngine engine = new SearchEngine();
final Collection<IJavaElement> kludge = HashSetFactory.make();
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
kludge.add((IJavaElement) match.getElement());
}
};
try {
engine.search(p, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, null);
} catch (CoreException e) {
e.printStackTrace();
}
if (kludge.size() == 1) {
return (IMethod) kludge.iterator().next();
} else {
System.err.println("RETURNED " + kludge.size() + " " + kludge);
return null;
}
}
/**
* Use the search engine to find all methods in a java element
*/
public static Collection<IMethod> findMethods(IJavaElement elt) {
if (elt instanceof ICompilationUnit) {
Collection<IMethod> result = HashSetFactory.make();
for (IType type : getClasses((ICompilationUnit) elt)) {
try {
for (IMethod m : type.getMethods()) {
result.add(m);
}
} catch (JavaModelException e) {
e.printStackTrace();
}
}
return result;
} else {
final Collection<IMethod> result = HashSetFactory.make();
SearchPattern p = SearchPattern.createPattern("*", IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS,
SearchPattern.R_PATTERN_MATCH);
SearchPattern p2 = SearchPattern.createPattern("*", IJavaSearchConstants.CONSTRUCTOR, IJavaSearchConstants.DECLARATIONS,
SearchPattern.R_PATTERN_MATCH);
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { elt }, IJavaSearchScope.SOURCES);
SearchEngine engine = new SearchEngine();
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (match.getElement() instanceof IMethod) {
result.add((IMethod) match.getElement());
}
}
};
try {
engine.search(p, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, null);
engine.search(p2, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, null);
} catch (CoreException e) {
e.printStackTrace();
}
return result;
}
}
/**
* get a {@link StructuredSelection} corresponding to the named projects
*/
public static StructuredSelection getStructuredSelectionForProjectNames(Collection<String> projectNames) {
if (projectNames == null) {
throw new IllegalArgumentException("null projectNames");
}
Object[] projects = new Object[projectNames.size()];
int i = 0;
for (String projectName : projectNames) {
projects[i++] = getJavaProject(projectName);
}
StructuredSelection selection = new StructuredSelection(projects);
return selection;
}
public static IJavaProject getNamedProject(String projectName) {
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IJavaModel javaModel = JavaCore.create(workspaceRoot);
IJavaProject helloWorldProject = javaModel.getJavaProject(projectName);
return helloWorldProject;
}
}