Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » IFile for project-less source file?
IFile for project-less source file? [message #233527] Mon, 31 July 2006 18:06 Go to next message
David Coppit is currently offline David CoppitFriend
Messages: 41
Registered: July 2009
Member
Hi all,

I've written a little plugin that runs OrganizeImportsOperation on a
file opened in the IDE. I'm currently trying to turn this into a
standalone Java app that organizes the imports of a file specified as a
program argument.

My question is this: how can I get an IFile for a .java file that does
not have an associated Eclipse project? Do I really have to create a
project and copy the file into it? I seem to recall seeing somewhere in
the JavaEditor that a dummy project was created so that the Java Editor
could open up .java files independent of projects. Could I do a similar
thing here? (I can't remember now where I saw the pseudo-project magic
being done.)

In either case, can someone give me a nudge in the right direction? I
tried doing something like that described here:

http://dev.eclipse.org/newslists/news.eclipse.platform/msg50 548.html

but got a "Workspace is closed" exception.

Thanks,
David
Re: ICompilationUnit for project-less source file? [message #233544 is a reply to message #233527] Mon, 31 July 2006 19:30 Go to previous messageGo to next message
David Coppit is currently offline David CoppitFriend
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 Go to previous message
Dani Megert is currently offline Dani MegertFriend
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();
> }
>
> }
Previous Topic:Strange Error: Files Appearing Blank
Next Topic:Build path specifies unavailable execution environment: ... How to eliminate?
Goto Forum:
  


Current Time: Thu Dec 26 10:16:34 GMT 2024

Powered by FUDForum. Page generated in 0.05098 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top