1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 14:55:41 +02:00

169668: add unit tests + fix bugs

This commit is contained in:
Andrew Ferguson 2007-03-22 18:50:26 +00:00
parent 6047a73ed7
commit 250d34926b
13 changed files with 419 additions and 51 deletions

View file

@ -0,0 +1,283 @@
/*******************************************************************************
* Copyright (c) 2007 Symbian Software Systems and others.
* 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:
* Andrew Ferguson (Symbian) - Initial implementation
*******************************************************************************/
package org.eclipse.cdt.internal.pdom.tests;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import junit.framework.Test;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.index.IIndexLocationConverter;
import org.eclipse.cdt.core.index.IIndexerStateEvent;
import org.eclipse.cdt.core.index.IIndexerStateListener;
import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.index.ResourceContainerRelativeLocationConverter;
import org.eclipse.cdt.core.index.URIRelativeLocationConverter;
import org.eclipse.cdt.core.index.export.ExternalExportProjectProvider;
import org.eclipse.cdt.core.index.export.IExportProjectProvider;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.CTestPlugin;
import org.eclipse.cdt.internal.core.index.IIndexFragment;
import org.eclipse.cdt.internal.core.pdom.WritablePDOM;
import org.eclipse.cdt.internal.core.pdom.export.GeneratePDOMApplication;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.equinox.app.IApplicationContext;
import org.osgi.framework.Bundle;
/**
* Tests the GeneratePDOMApplication
*/
public class GeneratePDOMApplicationTest extends PDOMTestBase {
private static final String SDK_VERSION = "com.acme.sdk.version";
private static final String ACME_SDK_ID= "com.acme.sdk.4.0.1";
private static List toDeleteOnTearDown= new ArrayList();
private final static String s= "resources/pdomtests/generatePDOMTests/project1";
private URI baseURI;
public static Test suite() {
return suite(GeneratePDOMApplicationTest.class);
}
protected void setUp() throws Exception {
toDeleteOnTearDown.clear();
baseURI= new URI("file:///base/"); // unimportant what the value is
}
protected void tearDown() throws Exception {
for(Iterator i= toDeleteOnTearDown.iterator(); i.hasNext(); ) {
ICProject cproject= (ICProject) i.next();
cproject.getProject().delete(true, new NullProgressMonitor());
}
}
public void testBrokenExportProjectProvider1() throws Exception {
try {
File target= File.createTempFile("test", "pdom");
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, TestProjectProvider1.class.getName()
});
fail("Expected exception - IExportProjectProvider implementation returns null for createProject");
} catch(CoreException ce) {
// correct behaviour
}
}
public void testBrokenExportProjectProvider2() throws Exception {
try {
File target= File.createTempFile("test", "pdom");
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, TestProjectProvider2.class.getName()
});
fail("Expected exception - IExportProjectProvider implementation returns null for getLocationConverter");
} catch(CoreException ce) {
// correct behaviour
}
}
public void testSimpleExportProjectProvider1() throws Exception {
File target= File.createTempFile("test", "pdom");
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, TestProjectProvider3.class.getName()
});
assertTrue(target.exists());
WritablePDOM wpdom= new WritablePDOM(target, new URIRelativeLocationConverter(baseURI));
verifyProject1Content(wpdom);
String fid= wpdom.getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID);
assertNotNull(fid);
assertTrue(fid.startsWith("export")); // check for default export id
}
public void testSimpleExportProjectProvider2() throws Exception {
File target= File.createTempFile("test", "pdom");
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, TestProjectProvider4.class.getName()
});
assertTrue(target.exists());
WritablePDOM wpdom= new WritablePDOM(target, new URIRelativeLocationConverter(baseURI));
verifyProject1Content(wpdom);
String fid= wpdom.getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID);
assertNotNull(fid);
assertEquals(ACME_SDK_ID, fid); // check for default export id
String sdkVer= wpdom.getProperty(SDK_VERSION);
assertNotNull(sdkVer);
assertEquals("4.0.1", sdkVer); // check for default export id
}
public void testExternalExportProjectProvider_BadCmdLine1() throws Exception {
File target= File.createTempFile("test", "pdom");
try {
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, ExternalExportProjectProvider.class.getName()
});
assertTrue(target.exists());
fail("Expected failure: -source must be specified");
} catch(CoreException ce) {
// correct behaviour
}
}
public void testExternalExportProjectProvider_BadCmdLine2() throws Exception {
File target= File.createTempFile("test", "pdom");
TestProjectProvider4 tpp4= new TestProjectProvider4();
ICProject cproject= tpp4.createProject();
try {
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, ExternalExportProjectProvider.class.getName(),
ExternalExportProjectProvider.OPT_SOURCE, cproject.getProject().getLocation().toFile().getAbsolutePath()
});
assertTrue(target.exists());
fail("Expected failure: -id must be specified");
} catch(CoreException ce) {
// correct behaviour
}
}
public void testExternalExportProjectProvider() throws Exception {
File target= File.createTempFile("test", "pdom");
final int[] stateCount = new int[1];
IIndexerStateListener listener= new IIndexerStateListener() {
public void indexChanged(IIndexerStateEvent event) {
stateCount[0]++;
}
};
CCorePlugin.getIndexManager().addIndexerStateListener(listener);
URL url= FileLocator.find(CTestPlugin.getDefault().getBundle(), new Path(s), null);
String baseDir= FileLocator.toFileURL(url).getFile();
doGenerate(new String[] {
GeneratePDOMApplication.OPT_TARGET, target.getAbsolutePath(),
GeneratePDOMApplication.OPT_PROJECTPROVIDER, ExternalExportProjectProvider.class.getName(),
ExternalExportProjectProvider.OPT_SOURCE, baseDir,
ExternalExportProjectProvider.OPT_FRAGMENT_ID, "hello.world"
});
assertTrue(target.exists());
WritablePDOM wpdom= new WritablePDOM(target, new URIRelativeLocationConverter(baseURI));
verifyProject1Content(wpdom);
String fid= wpdom.getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID);
assertNotNull(fid);
assertEquals("hello.world", fid); // check for default export id
assertTrue(stateCount[0] == 2);
}
public void verifyProject1Content(WritablePDOM wpdom) throws CoreException {
IBinding[] bindings= wpdom.findBindings(Pattern.compile(".*foo.*"), false, IndexFilter.ALL, PROGRESS);
assertEquals(1, bindings.length);
bindings= wpdom.findBindings(Pattern.compile(".*bar.*"), false, IndexFilter.ALL, PROGRESS);
assertEquals(1, bindings.length);
}
private void doGenerate(String[] args) throws CoreException {
GeneratePDOMApplication app = new GeneratePDOMApplication();
IApplicationContext ac= new MockApplicationContext(args);
app.start(ac);
}
/*
* IExportProjectProvider test implementations
*/
public static class TestProjectProvider1 implements IExportProjectProvider {
public ICProject createProject() throws CoreException {return null;}
public Map getExportProperties() {return null;}
public IIndexLocationConverter getLocationConverter(ICProject cproject) {return null;}
public void setApplicationArguments(String[] arguments) {}
}
public static class TestProjectProvider2 implements IExportProjectProvider {
public ICProject createProject() throws CoreException {
ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER);
toDeleteOnTearDown.add(cproject);
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), s);
return cproject;
}
public Map getExportProperties() {return null;}
public IIndexLocationConverter getLocationConverter(ICProject cproject) {return null;}
public void setApplicationArguments(String[] arguments) {}
}
public static class TestProjectProvider3 implements IExportProjectProvider {
public ICProject createProject() throws CoreException {
ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER);
toDeleteOnTearDown.add(cproject);
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), s);
return cproject;
}
public Map getExportProperties() {return null;}
public IIndexLocationConverter getLocationConverter(ICProject cproject) {
return new ResourceContainerRelativeLocationConverter(cproject.getProject());
}
public void setApplicationArguments(String[] arguments) {}
}
public static class TestProjectProvider4 implements IExportProjectProvider {
public ICProject createProject() throws CoreException {
ICProject cproject= CProjectHelper.createCCProject("test"+System.currentTimeMillis(), null, IPDOMManager.ID_NO_INDEXER);
toDeleteOnTearDown.add(cproject);
CProjectHelper.importSourcesFromPlugin(cproject, CTestPlugin.getDefault().getBundle(), s);
return cproject;
}
public Map getExportProperties() {
Map map= new HashMap();
map.put(SDK_VERSION, "4.0.1");
map.put(IIndexFragment.PROPERTY_FRAGMENT_ID, ACME_SDK_ID);
return map;
}
public IIndexLocationConverter getLocationConverter(ICProject cproject) {
return new ResourceContainerRelativeLocationConverter(cproject.getProject());
}
public void setApplicationArguments(String[] arguments) {}
}
}
class MockApplicationContext implements IApplicationContext {
Map arguments;
MockApplicationContext(String[] appArgs) {
arguments= new HashMap();
arguments.put(APPLICATION_ARGS, appArgs);
}
public void applicationRunning() {}
public Map getArguments() {return arguments;}
public String getBrandingApplication() {return null;}
public Bundle getBrandingBundle() {return null;}
public String getBrandingDescription() {return null;}
public String getBrandingId() {return null;}
public String getBrandingName() {return null;}
public String getBrandingProperty(String key) {return null;}
}

View file

@ -39,7 +39,8 @@ public class PDOMTests extends TestSuite {
suite.addTest(OverloadsWithinCommonHeaderTests.suite());
suite.addTest(BTreeTests.suite());
suite.addTest(FilesOnReindexTests.suite());
suite.addTest(GeneratePDOMApplicationTest.suite());
suite.addTest(CPPFieldTests.suite());
suite.addTest(CPPFunctionTests.suite());
suite.addTest(CPPVariableTests.suite());

View file

@ -32,6 +32,18 @@
<ReadOnlyPDOMProvider
class="org.eclipse.cdt.core.internal.index.provider.test.DummyProvider1">
</ReadOnlyPDOMProvider>
<ExportProjectProvider
class="org.eclipse.cdt.internal.pdom.tests.GeneratePDOMApplicationTest$TestProjectProvider1">
</ExportProjectProvider>
<ExportProjectProvider
class="org.eclipse.cdt.internal.pdom.tests.GeneratePDOMApplicationTest$TestProjectProvider2">
</ExportProjectProvider>
<ExportProjectProvider
class="org.eclipse.cdt.internal.pdom.tests.GeneratePDOMApplicationTest$TestProjectProvider3">
</ExportProjectProvider>
<ExportProjectProvider
class="org.eclipse.cdt.internal.pdom.tests.GeneratePDOMApplicationTest$TestProjectProvider4">
</ExportProjectProvider>
</extension>
</plugin>

View file

@ -0,0 +1,7 @@
class A {
public:
class B {};
int c;
A* foo(B b) {};
};

View file

@ -0,0 +1,7 @@
#include "a/b/c/d e/this.h"
void bar() {
A a;
a.foo(*new A::B());
}

View file

@ -65,6 +65,7 @@ Export-Package: org.eclipse.cdt.core,
org.eclipse.cdt.internal.core.pdom.dom;x-friends:="org.eclipse.cdt.ui",
org.eclipse.cdt.internal.core.pdom.dom.c;x-internal:=true,
org.eclipse.cdt.internal.core.pdom.dom.cpp;x-internal:=true,
org.eclipse.cdt.internal.core.pdom.export,
org.eclipse.cdt.internal.core.pdom.indexer;x-friends:="org.eclipse.cdt.ui",
org.eclipse.cdt.internal.core.pdom.indexer.fast;x-internal:=true,
org.eclipse.cdt.internal.core.pdom.indexer.full;x-internal:=true,

View file

@ -31,6 +31,9 @@ import org.eclipse.cdt.core.index.ResourceContainerRelativeLocationConverter;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IPathEntry;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.core.settings.model.extension.impl.CDefaultConfigurationData;
import org.eclipse.cdt.internal.core.index.IIndexFragment;
import org.eclipse.cdt.internal.core.pdom.indexer.IndexerPreferences;
import org.eclipse.core.resources.IFolder;
@ -58,9 +61,9 @@ import org.eclipse.core.runtime.Path;
public class ExternalExportProjectProvider extends AbstractExportProjectProvider implements IExportProjectProvider {
private static final String ORG_ECLIPSE_CDT_CORE_INDEX_EXPORT_DATESTAMP = "org.eclipse.cdt.core.index.export.datestamp"; //$NON-NLS-1$
private static final String CONTENT = "content"; //$NON-NLS-1$
protected static final String ARG_SOURCE = "-source"; //$NON-NLS-1$
protected static final String ARG_INCLUDE = "-include"; //$NON-NLS-1$
protected static final String ARG_FRAGMENT_ID = "-id"; //$NON-NLS-1$
public static final String OPT_SOURCE = "-source"; //$NON-NLS-1$
public static final String OPT_INCLUDE = "-include"; //$NON-NLS-1$
public static final String OPT_FRAGMENT_ID = "-id"; //$NON-NLS-1$
private IFolder content;
private String fragmentId;
@ -75,21 +78,21 @@ public class ExternalExportProjectProvider extends AbstractExportProjectProvider
*/
public ICProject createProject() throws CoreException {
// -source
File source= new File(getSingleString(ARG_SOURCE));
File source= new File(getSingleString(OPT_SOURCE));
if(!source.exists()) {
fail(MessageFormat.format(Messages.ExternalContentPEM_LocationToIndexNonExistent, new Object[] {source}));
}
// -include
List includeFiles= new ArrayList();
if(isPresent(ARG_INCLUDE)) {
includeFiles.addAll(getParameters(ARG_INCLUDE));
if(isPresent(OPT_INCLUDE)) {
includeFiles.addAll(getParameters(OPT_INCLUDE));
}
// -id
fragmentId= getSingleString(ARG_FRAGMENT_ID);
fragmentId= getSingleString(OPT_FRAGMENT_ID);
return createCProject("__"+System.currentTimeMillis(), source, IPDOMManager.ID_FAST_INDEXER, includeFiles); //$NON-NLS-1$
return createCCProject("__"+System.currentTimeMillis(), source, includeFiles); //$NON-NLS-1$
}
/**
@ -109,10 +112,9 @@ public class ExternalExportProjectProvider extends AbstractExportProjectProvider
* @return a new project
* @throws CoreException
*/
private ICProject createCProject(
private ICProject createCCProject(
final String projectName,
final File location,
final String indexerID,
final List includeFiles
) throws CoreException {
final IWorkspace ws = ResourcesPlugin.getWorkspace();
@ -158,19 +160,29 @@ public class ExternalExportProjectProvider extends AbstractExportProjectProvider
(IPathEntry[]) entries.toArray(new IPathEntry[includeFiles.size()]),
new NullProgressMonitor()
);
ICProjectDescription pd= CoreModel.getDefault().createProjectDescription(cproject.getProject(), false); // create the description
newCfg(pd, project.getName(), "cfg1"); //$NON-NLS-1$
CoreModel.getDefault().setProjectDescription(cproject.getProject(), pd, true, new NullProgressMonitor());
newProject[0]= cproject;
IndexerPreferences.set(newProject[0].getProject(), IndexerPreferences.KEY_INDEX_ALL_FILES, Boolean.TRUE.toString());
IndexerPreferences.set(newProject[0].getProject(), IndexerPreferences.KEY_INDEXER_ID, IPDOMManager.ID_NO_INDEXER);
}
}, null);
if (indexerID != null) {
IndexerPreferences.set(newProject[0].getProject(), IndexerPreferences.KEY_INDEX_ALL_FILES, Boolean.TRUE.toString());
IndexerPreferences.set(newProject[0].getProject(), IndexerPreferences.KEY_INDEXER_ID, indexerID);
}
return newProject[0];
}
private ICConfigurationDescription newCfg(ICProjectDescription des, String project, String config) throws CoreException {
CDefaultConfigurationData data= new CDefaultConfigurationData(project+"."+config, project+" "+config+" name", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
data.initEmptyData();
String ID= CCorePlugin.PLUGIN_ID + ".defaultConfigDataProvider"; //$NON-NLS-1$
return des.createConfiguration(ID, data);
}
/*
* This should be a platform/CDT API
*/

View file

@ -41,7 +41,8 @@ public interface IExportProjectProvider {
public void setApplicationArguments(String[] arguments);
/**
* Creates, configures and returns a project for the indexer to index
* Creates, configures and returns a project for the indexer to index. This routine should
* not itself index the project, as a reindex will be performed by the framework.
* May not return null.
* @return
*/

View file

@ -29,6 +29,8 @@ import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.settings.model.ICConfigurationDescription;
import org.eclipse.cdt.core.settings.model.ICProjectDescription;
import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.index.provider.IndexProviderManager;
import org.eclipse.cdt.internal.core.pdom.PDOMManager;
import org.eclipse.core.resources.IProject;
@ -56,7 +58,7 @@ public class IndexFactory {
boolean addDependent= (options & ADD_DEPENDENT) != 0;
boolean skipProvided= (options & SKIP_PROVIDED) != 0;
IndexProviderManager m = ((PDOMManager)CCorePlugin.getPDOMManager()).getIndexProviderManager();
IndexProviderManager m = CCoreInternals.getPDOMManager().getIndexProviderManager();
HashMap map= new HashMap();
Collection selectedProjects= getProjects(projects, addDependencies, addDependent, map, new Integer(1));
@ -68,10 +70,13 @@ public class IndexFactory {
fragments.put(pdom.getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID), pdom);
if(!skipProvided) {
ICConfigurationDescription activeCfg= CoreModel.getDefault().getProjectDescription(cproject.getProject()).getActiveConfiguration();
IIndexFragment[] pFragments= m.getProvidedIndexFragments(activeCfg);
for(int i=0; i<pFragments.length; i++) {
fragments.put(pFragments[i].getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID), pFragments[i]);
ICProjectDescription pd= CoreModel.getDefault().getProjectDescription(cproject.getProject(), false);
if(pd!=null) {
ICConfigurationDescription activeCfg= pd.getActiveConfiguration();
IIndexFragment[] pFragments= m.getProvidedIndexFragments(activeCfg);
for(int i=0; i<pFragments.length; i++) {
fragments.put(pFragments[i].getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID), pFragments[i]);
}
}
}
}
@ -94,10 +99,13 @@ public class IndexFactory {
fragments.put(pdom.getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID), pdom);
}
if(!skipProvided) {
ICConfigurationDescription activeCfg= CoreModel.getDefault().getProjectDescription(cproject.getProject()).getActiveConfiguration();
IIndexFragment[] pFragments= m.getProvidedIndexFragments(activeCfg);
for(int i=0; i<pFragments.length; i++) {
fragments.put(pFragments[i].getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID), pFragments[i]);
ICProjectDescription pd= CoreModel.getDefault().getProjectDescription(cproject.getProject(), false);
if(pd!=null) {
ICConfigurationDescription activeCfg= pd.getActiveConfiguration();
IIndexFragment[] pFragments= m.getProvidedIndexFragments(activeCfg);
for(int i=0; i<pFragments.length; i++) {
fragments.put(pFragments[i].getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID), pFragments[i]);
}
}
}
}
@ -168,7 +176,8 @@ public class IndexFactory {
}
public IWritableIndex getWritableIndex(ICProject project) throws CoreException {
// mstodo to support dependent projects: Collection selectedProjects= getSelectedProjects(new ICProject[]{project}, false);
// mstodo to support dependent projects: Collection selectedProjects= getSelectedProjects(new ICProject[]{project}, false);
IndexProviderManager m = CCoreInternals.getPDOMManager().getIndexProviderManager();
Collection selectedProjects= Collections.singleton(project);
Map readOnlyFrag= new LinkedHashMap();
@ -178,11 +187,13 @@ public class IndexFactory {
IWritableIndexFragment pdom= (IWritableIndexFragment) fPDOMManager.getPDOM(p);
if (pdom != null) {
fragments.put(pdom.getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID), pdom);
IndexProviderManager m = ((PDOMManager)CCorePlugin.getPDOMManager()).getIndexProviderManager();
ICConfigurationDescription activeCfg= CoreModel.getDefault().getProjectDescription(p.getProject()).getActiveConfiguration();
IIndexFragment[] pFragments= m.getProvidedIndexFragments(activeCfg);
for(int i=0; i<pFragments.length; i++) {
readOnlyFrag.put(pFragments[i].getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID), pFragments[i]);
ICProjectDescription pd= CoreModel.getDefault().getProjectDescription(p.getProject(), false);
if(pd!=null) {
ICConfigurationDescription activeCfg= pd.getActiveConfiguration();
IIndexFragment[] pFragments= m.getProvidedIndexFragments(activeCfg);
for(int i=0; i<pFragments.length; i++) {
readOnlyFrag.put(pFragments[i].getProperty(IIndexFragment.PROPERTY_FRAGMENT_ID), pFragments[i]);
}
}
}
}

View file

@ -21,6 +21,7 @@ import org.eclipse.cdt.core.index.export.IExportProjectProvider;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.pdom.WritablePDOM;
import org.eclipse.cdt.internal.core.pdom.indexer.IndexerPreferences;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.NullProgressMonitor;
@ -37,21 +38,33 @@ public class GeneratePDOM implements ISafeRunnable {
protected IExportProjectProvider pm;
protected String[] applicationArguments;
protected File targetLocation;
protected String indexerID;
public GeneratePDOM(IExportProjectProvider pm, String[] applicationArguments, File targetLocation) {
this.pm = pm;
public GeneratePDOM(IExportProjectProvider pm, String[] applicationArguments, File targetLocation, String indexerID) {
this.pm= pm;
this.applicationArguments= applicationArguments;
this.targetLocation = targetLocation;
this.targetLocation= targetLocation;
this.indexerID= indexerID;
}
public final void run() throws CoreException {
pm.setApplicationArguments(applicationArguments);
final ICProject cproject = pm.createProject();
if(cproject==null) {
throw new CoreException(CCorePlugin.createStatus(Messages.GeneratePDOM_ProjectProviderReturnedNullCProject));
fail(MessageFormat.format(Messages.GeneratePDOM_ProjectProviderReturnedNullCProject,
new Object [] {pm.getClass().getName()}));
}
CCorePlugin.getIndexManager().joinIndexer(Integer.MAX_VALUE, new NullProgressMonitor());
IIndexLocationConverter converter= pm.getLocationConverter(cproject);
if(converter==null) {
fail(MessageFormat.format(Messages.GeneratePDOM_NullLocationConverter,
new Object [] {pm.getClass().getName()}));
}
// index the project
IndexerPreferences.set(cproject.getProject(), IndexerPreferences.KEY_INDEXER_ID, indexerID);
CCorePlugin.getIndexManager().joinIndexer(Integer.MAX_VALUE, new NullProgressMonitor());
try {
CCoreInternals.getPDOMManager().exportProjectPDOM(cproject, targetLocation, converter);
WritablePDOM exportedPDOM= new WritablePDOM(targetLocation, converter);
@ -78,4 +91,8 @@ public class GeneratePDOM implements ISafeRunnable {
// subclass for custom behaviour
CCorePlugin.log(exception);
}
private void fail(String message) throws CoreException {
throw new CoreException(CCorePlugin.createStatus(message));
}
}

View file

@ -18,6 +18,7 @@ import java.util.Map;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMIndexerTask;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.index.export.ExternalExportProjectProvider;
import org.eclipse.cdt.core.index.export.IExportProjectProvider;
import org.eclipse.core.runtime.CoreException;
@ -39,9 +40,10 @@ import org.eclipse.equinox.app.IApplicationContext;
public class GeneratePDOMApplication implements IApplication {
private static final String EXPORT_PROJECT_PROVIDER = "ExportProjectProvider"; //$NON-NLS-1$
private static final String DEFAULT_PROJECT_PROVIDER = ExternalExportProjectProvider.class.getName();
private static final String OPT_PROJECTPROVIDER= "-pprovider"; //$NON-NLS-1$
private static final String OPT_TARGET= "-target"; //$NON-NLS-1$
private static final String OPT_QUIET= "-quiet"; //$NON-NLS-1$
public static final String OPT_PROJECTPROVIDER= "-pprovider"; //$NON-NLS-1$
public static final String OPT_TARGET= "-target"; //$NON-NLS-1$
public static final String OPT_QUIET= "-quiet"; //$NON-NLS-1$
public static final String OPT_INDEXER_ID= "-indexer"; //$NON-NLS-1$
private static Map/*<String,IProjectForExportManager>*/ projectInitializers;
@ -49,7 +51,8 @@ public class GeneratePDOMApplication implements IApplication {
* Starts this application
*/
public Object start(IApplicationContext context) throws CoreException {
Map arguments= CLIUtil.parseToMap(Platform.getApplicationArgs());
String[] appArgs= (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
Map arguments= CLIUtil.parseToMap(appArgs);
output(Messages.GeneratePDOMApplication_Initializing);
setupCLIProgressProvider();
@ -64,6 +67,16 @@ public class GeneratePDOMApplication implements IApplication {
String target= (String) CLIUtil.getArg(arguments, OPT_TARGET, 1).get(0);
boolean quiet= arguments.get(OPT_QUIET)!=null;
String indexerID= IPDOMManager.ID_FAST_INDEXER;
String[] indexerIDs= (String[]) arguments.get(OPT_INDEXER_ID);
if(indexerIDs!=null) {
if(indexerIDs.length==1) {
indexerID= indexerIDs[0];
} else if(indexerIDs.length>1) {
fail(MessageFormat.format(Messages.GeneratePDOMApplication_InvalidIndexerID, new Object[] {OPT_INDEXER_ID}));
}
}
if(!quiet) {
System.setProperty(IPDOMIndexerTask.TRACE_ACTIVITY, Boolean.TRUE.toString());
System.setProperty(IPDOMIndexerTask.TRACE_PROBLEMS, Boolean.TRUE.toString());
@ -72,12 +85,11 @@ public class GeneratePDOMApplication implements IApplication {
IExportProjectProvider pprovider = getExportProjectProvider(pproviderFQN);
if(pprovider==null) {
output(MessageFormat.format(Messages.GeneratePDOMApplication_CouldNotFindInitializer, new Object[]{pproviderFQN}));
return null;
fail(MessageFormat.format(Messages.GeneratePDOMApplication_CouldNotFindInitializer, new Object[]{pproviderFQN}));
}
File targetLocation = new File(target);
GeneratePDOM generate = new GeneratePDOM(pprovider, Platform.getApplicationArgs(), targetLocation);
GeneratePDOM generate = new GeneratePDOM(pprovider, appArgs, targetLocation, indexerID);
output(Messages.GeneratePDOMApplication_GenerationStarts);
generate.run();
output(Messages.GeneratePDOMApplication_GenerationEnds);
@ -92,6 +104,10 @@ public class GeneratePDOMApplication implements IApplication {
// do nothing
}
private void fail(String message) throws CoreException {
throw new CoreException(CCorePlugin.createStatus(message));
}
/**
* Returns the IExportProjectProvider registed in the plug-in registry under the
* specified fully qualified class name
@ -109,20 +125,16 @@ public class GeneratePDOMApplication implements IApplication {
IExtension extension = extensions[i];
IConfigurationElement[] ce = extension.getConfigurationElements();
IExportProjectProvider pfem = null;
for(int j=0; j<ce.length; j++) {
if(ce[j].getName().equals(EXPORT_PROJECT_PROVIDER)) {
try {
pfem = (IExportProjectProvider) ce[j].createExecutableExtension("class"); //$NON-NLS-1$
IExportProjectProvider epp = (IExportProjectProvider) ce[j].createExecutableExtension("class"); //$NON-NLS-1$
projectInitializers.put(epp.getClass().getName(), epp);
} catch(CoreException cee) {
CCorePlugin.log(cee);
}
break;
}
}
if(pfem!=null) {
projectInitializers.put(pfem.getClass().getName(), pfem);
}
}
}

View file

@ -15,11 +15,13 @@ import org.eclipse.osgi.util.NLS;
public class Messages extends NLS {
private static final String BUNDLE_NAME = "org.eclipse.cdt.internal.core.pdom.export.messages"; //$NON-NLS-1$
public static String GeneratePDOM_GenericGenerationFailed;
public static String GeneratePDOM_NullLocationConverter;
public static String GeneratePDOM_ProjectProviderReturnedNullCProject;
public static String GeneratePDOMApplication_CouldNotFindInitializer;
public static String GeneratePDOMApplication_GenerationEnds;
public static String GeneratePDOMApplication_GenerationStarts;
public static String GeneratePDOMApplication_Initializing;
public static String GeneratePDOMApplication_InvalidIndexerID;
public static String GeneratePDOMApplication_UsingDefaultProjectProvider;
static {
// initialize resource bundle

View file

@ -1,7 +1,9 @@
GeneratePDOMApplication_Initializing=== Initializing
GeneratePDOM_GenericGenerationFailed=Generation failed: {0}
GeneratePDOM_NullLocationConverter=IExportProjectProvider implementation of getLocationConverter() returned null ({0})
GeneratePDOMApplication_CouldNotFindInitializer=Could not find IExportProjectProvider: {0}
GeneratePDOM_ProjectProviderReturnedNullCProject=IExportProjectProvider implementation of createProject({0} returned null
GeneratePDOM_ProjectProviderReturnedNullCProject=IExportProjectProvider implementation of createProject() returned null ({0})
GeneratePDOMApplication_UsingDefaultProjectProvider=-pprovider not specified - defaulting to {0}
GeneratePDOMApplication_GenerationStarts=== Generation starts
GeneratePDOMApplication_InvalidIndexerID={0} takes zero or one argument
GeneratePDOMApplication_GenerationEnds=== Generation ends