|
Re: ICompilationUnit for project-less source file? [message #233544 is a reply to message #233527] |
Mon, 31 July 2006 19:30 |
David Coppit Messages: 41 Registered: July 2009 |
Member |
|
|
David Coppit wrote:
> My question is this: how can I get an IFile for a .java file that does
> not have an associated Eclipse project?
Okay, I found createFakeCompiltationUnit in
CompilationUnitDocumentProvider. Using it as a guide, I created the code
below. I'm still getting this "Workspace is closed" exception, but this
time at the call to newWorkingCopy.
Anyone have a suggestion?
Thanks,
David
------------ %< Complete code below %< --------------
package edu.wm.cs.PlanCleanupStandalone;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.Reader;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IBuffer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.ISourceRange;
import org.eclipse.jdt.core.WorkingCopyOwner;
import org.eclipse.jdt.core.dom.CompilationUnit;
import
org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImp ortsOperation;
import
org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImp ortsOperation.IChooseImportQuery;
import org.eclipse.jdt.internal.corext.util.TypeInfo;
import org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter;
import org.eclipse.jdt.launching.JavaRuntime;
public class PlanCleanupStandalone {
public static void main(String[] args) {
if (args.length == 0) {
System.err.println("provide a java file as an argument");
System.exit(1);
}
removeUnusedImports(args[0]);
System.out.println("Done!");
}
private static void removeUnusedImports(String filepath) {
// IFile file = null; // How to get this without a project?
// ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file);
ICompilationUnit cu = createFakeCompilationUnit(filepath);
/*
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(getSource(filepath).toCharArray());
CompilationUnit astRoot = (CompilationUnit)parser.createAST(null);
*/
CompilationUnit astRoot = null;
IChooseImportQuery query= new IChooseImportQuery() {
public TypeInfo[] chooseImports(TypeInfo[][] openChoices,
ISourceRange[] ranges) {
throw new RuntimeException();
}
};
try {
OrganizeImportsOperation oi = new OrganizeImportsOperation(cu,
astRoot,
/*ignore lower case*/ false, false, true, query);
oi.run(null);
} catch (CoreException core) { core.printStackTrace();}
}
// Inspired by CompilationUnitDocumentProvider.createFakecompiltationUnit
private static ICompilationUnit createFakeCompilationUnit(final
String filepath) {
try {
WorkingCopyOwner woc= new WorkingCopyOwner() {
public IBuffer createBuffer(ICompilationUnit workingCopy) {
return new DocumentAdapter(workingCopy, new Path(filepath));
}
};
IClasspathEntry[] cpEntries = new IClasspathEntry[] {
JavaRuntime.getDefaultJREContainerEntry() };
final ICompilationUnit cu= woc.newWorkingCopy(filepath,
cpEntries, null, null);
cu.getBuffer().setContents(getSource(filepath));
// if (!isModifiable(element))
// JavaModelUtil.reconcile(cu);
return cu;
} catch (CoreException ex) {
return null;
}
}
public static String getSource(String filepath) {
int READER_CHUNK_SIZE= 2048;
int BUFFER_SIZE= 8 * READER_CHUNK_SIZE;
StringBuffer buffer= new StringBuffer(BUFFER_SIZE);
try {
Reader in= new BufferedReader(new FileReader(filepath));
char[] readBuffer= new char[READER_CHUNK_SIZE];
int n;
n= in.read(readBuffer);
while (n > 0) {
buffer.append(readBuffer, 0, n);
n= in.read(readBuffer);
}
} catch (Exception e) {
System.err.println("Problems reading from file " + filepath);
e.printStackTrace();
System.exit(1);
}
return buffer.toString();
}
}
|
|
|
Re: ICompilationUnit for project-less source file? [message #233654 is a reply to message #233544] |
Wed, 02 August 2006 12:18 |
Dani Megert Messages: 3802 Registered: July 2009 |
Senior Member |
|
|
David Coppit wrote:
> David Coppit wrote:
>
>> My question is this: how can I get an IFile for a .java file that
>> does not have an associated Eclipse project?
>
>
> Okay, I found createFakeCompiltationUnit in
> CompilationUnitDocumentProvider. Using it as a guide, I created the
> code below. I'm still getting this "Workspace is closed" exception,
> but this time at the call to newWorkingCopy.
>
> Anyone have a suggestion?
You could file a feature request against JDT Core to support calling
this method without having a workspace.
Dani
>
> Thanks,
> David
>
> ------------ %< Complete code below %< --------------
>
> package edu.wm.cs.PlanCleanupStandalone;
>
> import java.io.BufferedReader;
> import java.io.FileReader;
> import java.io.Reader;
>
> import org.eclipse.core.runtime.CoreException;
> import org.eclipse.core.runtime.Path;
> import org.eclipse.jdt.core.IBuffer;
> import org.eclipse.jdt.core.IClasspathEntry;
> import org.eclipse.jdt.core.ICompilationUnit;
> import org.eclipse.jdt.core.ISourceRange;
> import org.eclipse.jdt.core.WorkingCopyOwner;
> import org.eclipse.jdt.core.dom.CompilationUnit;
> import
> org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImp ortsOperation;
>
> import
> org.eclipse.jdt.internal.corext.codemanipulation.OrganizeImp ortsOperation.IChooseImportQuery;
>
> import org.eclipse.jdt.internal.corext.util.TypeInfo;
> import org.eclipse.jdt.internal.ui.javaeditor.DocumentAdapter;
> import org.eclipse.jdt.launching.JavaRuntime;
>
> public class PlanCleanupStandalone {
>
> public static void main(String[] args) {
> if (args.length == 0) {
> System.err.println("provide a java file as an argument");
> System.exit(1);
> }
>
> removeUnusedImports(args[0]);
>
> System.out.println("Done!");
> }
>
> private static void removeUnusedImports(String filepath) {
> // IFile file = null; // How to get this without a project?
> // ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file);
>
> ICompilationUnit cu = createFakeCompilationUnit(filepath);
>
> /*
> ASTParser parser = ASTParser.newParser(AST.JLS3);
> parser.setSource(getSource(filepath).toCharArray());
> CompilationUnit astRoot = (CompilationUnit)parser.createAST(null);
> */
> CompilationUnit astRoot = null;
>
> IChooseImportQuery query= new IChooseImportQuery() {
> public TypeInfo[] chooseImports(TypeInfo[][] openChoices,
> ISourceRange[] ranges) {
> throw new RuntimeException();
> }
> };
>
> try {
> OrganizeImportsOperation oi = new OrganizeImportsOperation(cu,
> astRoot,
> /*ignore lower case*/ false, false, true, query);
>
> oi.run(null);
> } catch (CoreException core) { core.printStackTrace();}
> }
>
>
> // Inspired by
> CompilationUnitDocumentProvider.createFakecompiltationUnit
> private static ICompilationUnit createFakeCompilationUnit(final
> String filepath) {
> try {
> WorkingCopyOwner woc= new WorkingCopyOwner() {
> public IBuffer createBuffer(ICompilationUnit workingCopy) {
> return new DocumentAdapter(workingCopy, new Path(filepath));
> }
> };
>
> IClasspathEntry[] cpEntries = new IClasspathEntry[] {
> JavaRuntime.getDefaultJREContainerEntry() };
>
> final ICompilationUnit cu= woc.newWorkingCopy(filepath,
> cpEntries, null, null);
>
> cu.getBuffer().setContents(getSource(filepath));
>
> // if (!isModifiable(element))
> // JavaModelUtil.reconcile(cu);
>
> return cu;
> } catch (CoreException ex) {
> return null;
> }
> }
>
> public static String getSource(String filepath) {
> int READER_CHUNK_SIZE= 2048;
> int BUFFER_SIZE= 8 * READER_CHUNK_SIZE;
> StringBuffer buffer= new StringBuffer(BUFFER_SIZE);
>
> try {
> Reader in= new BufferedReader(new FileReader(filepath));
> char[] readBuffer= new char[READER_CHUNK_SIZE];
> int n;
>
> n= in.read(readBuffer);
> while (n > 0) {
> buffer.append(readBuffer, 0, n);
> n= in.read(readBuffer);
> }
> } catch (Exception e) {
> System.err.println("Problems reading from file " + filepath);
> e.printStackTrace();
> System.exit(1);
> }
>
> return buffer.toString();
> }
>
> }
|
|
|
Powered by
FUDForum. Page generated in 0.05098 seconds