diff options
author | Roel Spilker <r.spilker@gmail.com> | 2014-06-04 00:26:00 +0200 |
---|---|---|
committer | Roel Spilker <r.spilker@gmail.com> | 2014-06-04 00:26:00 +0200 |
commit | 87f628fe2339f010734bf59f73bc334fd15133be (patch) | |
tree | 3a374790d6baf42a89512aebd4487c5235046400 /src/core/lombok | |
parent | b37a95a6a4fa70da38f990b14a0f9517dd5ed0bc (diff) | |
download | lombok-87f628fe2339f010734bf59f73bc334fd15133be.tar.gz lombok-87f628fe2339f010734bf59f73bc334fd15133be.tar.bz2 lombok-87f628fe2339f010734bf59f73bc334fd15133be.zip |
[wip][#682][#683] Research on alternate ways to find the content of lombok.config given a CompilationUnitDeclaration (in eclipse).
Diffstat (limited to 'src/core/lombok')
-rw-r--r-- | src/core/lombok/eclipse/EclipseAST.java | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/src/core/lombok/eclipse/EclipseAST.java b/src/core/lombok/eclipse/EclipseAST.java index 62a01e59..74c60950 100644 --- a/src/core/lombok/eclipse/EclipseAST.java +++ b/src/core/lombok/eclipse/EclipseAST.java @@ -22,6 +22,7 @@ package lombok.eclipse; import java.io.File; +import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URI; @@ -49,6 +50,9 @@ import org.eclipse.jdt.internal.compiler.ast.Initializer; import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; import org.eclipse.jdt.internal.compiler.ast.Statement; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; +import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; +import org.eclipse.jdt.internal.core.Openable; +import org.eclipse.jdt.internal.core.builder.SourceFile; /** * Wraps around Eclipse's internal AST view to add useful features as well as the ability to visit parents from children, @@ -72,7 +76,19 @@ public class EclipseAST extends AST<EclipseAST, EclipseNode, ASTNode> { public URI getAbsoluteFileLocation() { String fileName = getFileName(); + + // state of the research in this: + // * We need an abstraction of a 'directory level'. This abstraction needs 'read()' which returns a string (content of lombok.config) and 'getParent()'. + // * sometimes, cud.compilationResult.compilationUnit is an 'openable', you can chase this down to end up with a path, you can jigger this into being the sibling 'lombok.config', and then use: + // InputStream in = ResourcesPlugin.getWorkspace().getRoot().getFile(ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(x)).getFullPath()).getContents(true); + // to read out this data. Our theory is that this will work even with very crazy virtual filesystems such as sourcecontrol://jazz/blabla. + // * With jazz and other creative file backed systems, is there even a 'project root' concept? Surely there won't be a 'workspace root' concept so how do we abstract the idea that, from jazz://whatever/projectroot, the parent is c:\myWorkspace? + // * Check the .getAlternateAbsolutePath() impl which has the research done so far. + // * VIRTUAL FILES: Sometimes virtual files are created; their location tends to be /FileName.java which cannot be resolved. Optimally speaking we should find the 'source' of the virtual code and use IT for determining lombok.config, but that may not be feasible. If not, can we get at project or at least workspace? + // * Either way there are sufficiently many WTF situations, that in case of error, as painful as this is, we should just carry on and not apply lombok.config, though at least if we don't recognize the scenario we should write a log file imploring the user to send us a bunch of feedback on the situation. + // Relevant issues: Comment 2 on #683, all of #682 if (!skipEclipseWorkspaceBasedFileResolver) { + if (Boolean.TRUE) throw new IllegalArgumentException("Here's the alt strat result: " + getAlternativeAbsolutePath()); try { /*if (fileName.startsWith("/") && fileName.indexOf('/', 1) > -1) */ try { @@ -80,8 +96,9 @@ public class EclipseAST extends AST<EclipseAST, EclipseNode, ASTNode> { } catch (IllegalArgumentException e) { String msg = e.getMessage(); if (msg != null && msg.startsWith("Path must include project and resource name")) { - // go with the fallthrough, but log that this happened. - addProblem(new ParseProblem(true, "Path resolution for lombok.config failed. Path: " + fileName + " -- falling back to: " + new File(fileName).getAbsoluteFile(), 0, 1)); + // We shouldn't throw an exception at all, but we can't reproduce this so we need help from our users to figure this out. + // Let's bother them with an error that slows their eclipse down to a crawl and makes it unusable. + throw new IllegalArgumentException("Path resolution for lombok.config failed. Path: " + fileName + " -- package of this class: " + this.getPackageDeclaration()); } else throw e; } } catch (NoClassDefFoundError e) { @@ -94,6 +111,39 @@ public class EclipseAST extends AST<EclipseAST, EclipseNode, ASTNode> { return new File(fileName).getAbsoluteFile().toURI(); } + private String getAlternativeAbsolutePath() { + try { + ICompilationUnit cu = this.compilationUnitDeclaration.compilationResult.compilationUnit; + + if (cu instanceof Openable) { + String x = ((Openable) cu).getResource().getFullPath().makeAbsolute().toString(); + int lastLoc = x.lastIndexOf('/'); + x = x.substring(0, lastLoc + 1) + "lombok.config"; + URI lombokConfigLoc = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(x)).getLocationURI(); + InputStream in = ResourcesPlugin.getWorkspace().getRoot().getFile(ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(x)).getFullPath()).getContents(true); + byte[] b = new byte[100000]; + int p = 0; + while (true) { + int r = in.read(b, p, b.length - p); + if (r == -1) break; + p += r; + } + in.close(); + return "(Contents of lombok.config: " + new String(b, 0, p, "UTF-8"); + +// return "(alt strategy result C: '" + ((Openable) cu).getResource().getFullPath().makeAbsolute().toString() + "'): resolved: " + EclipseWorkspaceBasedFileResolver.resolve(((Openable) cu).getResource().getFullPath().makeAbsolute().toString()); + } + if (cu instanceof SourceFile) { + String cuFileName = new String(((SourceFile) cu).getFileName()); + String cuIFilePath = ((SourceFile) cu).resource.getFullPath().toString(); + return "(alt strategy result A: \"" + cuFileName + "\" B: \"" + cuIFilePath + "\")"; + } + return "(alt strategy failed: cu isn't a SourceFile or Openable but a " + cu.getClass() + ")"; + } catch (Exception e) { + return "(alt strategy failed: " + e + ")"; + } + } + private static class EclipseWorkspaceBasedFileResolver { public static URI resolve(String path) { return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(path)).getLocationURI(); |