From 930b17b85be652405bf47752d1dbf4272afbace1 Mon Sep 17 00:00:00 2001 From: Roel Spilker Date: Sun, 15 Dec 2013 21:02:09 +0100 Subject: [configuration] Added the .getAbsoluteFileLocation() method to AST; this now works properly (even with eclipse Linked Resources) in both eclipse and javac. We will now use this feature to search for and apply config files, which will apply hierarchically based on the directory of the source file. --- src/core/lombok/core/AST.java | 8 ++ src/core/lombok/eclipse/EclipseAST.java | 23 ++++- .../lombok/eclipse/EclipseProjectSearcher.java | 103 --------------------- src/core/lombok/eclipse/handlers/HandleWither.java | 15 --- src/core/lombok/javac/JavacAST.java | 12 ++- 5 files changed, 38 insertions(+), 123 deletions(-) delete mode 100644 src/core/lombok/eclipse/EclipseProjectSearcher.java (limited to 'src/core') diff --git a/src/core/lombok/core/AST.java b/src/core/lombok/core/AST.java index 6fed0252..3582c6cb 100644 --- a/src/core/lombok/core/AST.java +++ b/src/core/lombok/core/AST.java @@ -28,6 +28,7 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.net.URI; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -64,6 +65,13 @@ public abstract class AST, L extends LombokNode, this.imports = imports; } + /** + * Attempts to find the absolute path (in URI form) to the source file represented by this AST. + * + * May return {@code null} if this cannot be done. We don't yet know under which conditions this will happen. + */ + public abstract URI getAbsoluteFileLocation(); + public void setChanged() { this.changed = true; } diff --git a/src/core/lombok/eclipse/EclipseAST.java b/src/core/lombok/eclipse/EclipseAST.java index 0f04f749..14d5726a 100644 --- a/src/core/lombok/eclipse/EclipseAST.java +++ b/src/core/lombok/eclipse/EclipseAST.java @@ -23,8 +23,8 @@ package lombok.eclipse; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.URI; import java.util.ArrayList; -import org.eclipse.jdt.internal.compiler.CompilationResult; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -35,6 +35,11 @@ import lombok.Lombok; import lombok.core.AST; import lombok.core.LombokImmutableList; +import org.eclipse.core.internal.resources.Project; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.Path; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.internal.compiler.CompilationResult; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; import org.eclipse.jdt.internal.compiler.ast.Annotation; @@ -47,7 +52,6 @@ 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.core.BasicCompilationUnit; import org.eclipse.jdt.internal.core.JavaElement; import org.eclipse.jdt.internal.core.JavaProject; @@ -69,6 +73,10 @@ public class EclipseAST extends AST { clearChanged(); } + public URI getAbsoluteFileLocation() { + return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(getFileName())).getLocationURI(); + } + private static String packageDeclaration(CompilationUnitDeclaration cud) { ImportReference pkg = cud.currentPackage; return pkg == null ? null : Eclipse.toQualifiedName(pkg.getImportName()); @@ -183,6 +191,7 @@ public class EclipseAST extends AST { } private static final Pattern PROJECT_NAME_FROM_FILEPATH = Pattern.compile("^/([^/]+)/(.*)$"); + /** * Returns the JavaProject object (eclipse's abstraction of the project) associated with the source file that is represented by this AST. */ @@ -199,13 +208,19 @@ public class EclipseAST extends AST { Matcher m = PROJECT_NAME_FROM_FILEPATH.matcher(new String(fn)); if (m.matches()) { String projName = m.group(1); - String path = m.group(2); - return EclipseProjectSearcher.getProject(projName); + return getProject0(projName); } return null; } + private static JavaProject getProject0(String projectName) { + Project depProjWrapper = (Project) ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); + if (depProjWrapper == null) return null; + if (!JavaProject.hasJavaNature(depProjWrapper)) return null; + return (JavaProject) JavaCore.create(depProjWrapper); + } + /** * Call this method to move an EclipseAST generated for a diet parse to rebuild itself for the full parse - * with filled in method bodies and such. Also propagates problems and errors, which in diet parse diff --git a/src/core/lombok/eclipse/EclipseProjectSearcher.java b/src/core/lombok/eclipse/EclipseProjectSearcher.java deleted file mode 100644 index b0a0e131..00000000 --- a/src/core/lombok/eclipse/EclipseProjectSearcher.java +++ /dev/null @@ -1,103 +0,0 @@ -package lombok.eclipse; - -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.eclipse.core.internal.resources.Project; -import org.eclipse.core.resources.ResourcesPlugin; -import org.eclipse.core.runtime.IPath; -import org.eclipse.jdt.core.IClasspathEntry; -import org.eclipse.jdt.core.JavaCore; -import org.eclipse.jdt.internal.core.ClasspathEntry; -import org.eclipse.jdt.internal.core.JavaProject; - -public class EclipseProjectSearcher { - private final JavaProject project; - - public EclipseProjectSearcher(JavaProject project) { - this.project = project; - } - - private static final String[] ENTRY_KINDS = {"", "CPE_LIBRARY", "CPE_PROJECT", "CPE_SOURCE", "CPE_VARIABLE", "CPE_CONTAINER"}; - - - public static JavaProject getProject(String projectName) { - Project depProjWrapper = (Project) ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); - if (depProjWrapper == null) return null; - if (!JavaProject.hasJavaNature(depProjWrapper)) return null; - return (JavaProject) JavaCore.create(depProjWrapper); - } - - public List findAllWithName(String pathSpec) throws Exception { - List list = new ArrayList(); - findAllWithName0(list, new HashSet(), pathSpec, project, false); - System.out.println("-------RESULTS [" + pathSpec + "]:"); - System.out.println(list); - System.out.println("---------------"); - - for (IPath path : list) { - try { - System.out.println(path + ": " + ResourcesPlugin.getWorkspace().getRoot().getFile(path.append(pathSpec)).getLocationURI()); - } catch (Exception ignore) { - try { - System.out.println(path + ": " + ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(pathSpec)).getLocationURI()); - } catch (Exception e) { - } - } - - // Check if we got through (didn't double-exept out), and then check if the resulting file URI - // actually exists. - // - // Then, check if 'path' itself is a jar, and if so, go root around in there. - } - - System.out.println("---------------"); - - return null; - } - - private void findAllWithName0(List list, Set coveredProjectNames, String pathSpec, JavaProject proj, boolean exportedOnly) throws Exception { - if (proj == null) return; -// if (System.currentTimeMillis() > 0) return; - - - /* - * - Research conclusions: - - * Eclipse is CRAZY. - * Take a path, for which you don't know if it's project relative or not (nor does eclipse!!! yes, crazy), append the resource you are looking for (whether you know if its a jar or a path), - and ask eclipse if it exists as a file. If yes, you have your answer. - * Then, check if the path is 1 or 0 long, in that case, there is no answer. - * Then, check if the path itself is a file and if yes, try and open it as a zip. Possibly try appending exclamationmark-resource and ask eclipse, maybe it has jar-style handling built in. - * Cache the heck out of this because this code is called after just about every keystroke. - * We should probably do something about duplicate detection and collapse, maybe just grab the textual content of those IPaths (they sure as hell don't contain anything useful beyond that), and - toss em in a set. - - */ - if (!coveredProjectNames.add(proj.getElementName())) return; - - for (IClasspathEntry rawEntry : proj.getResolvedClasspath(true)) { - ClasspathEntry entry = (ClasspathEntry) rawEntry; - if (exportedOnly && !entry.isExported() && entry.entryKind != IClasspathEntry.CPE_SOURCE) continue; - - switch (entry.entryKind) { - case IClasspathEntry.CPE_PROJECT: - String projName = entry.path.lastSegment(); - JavaProject depProj = getProject(projName); - findAllWithName0(list, coveredProjectNames, pathSpec, depProj, true); - break; - case IClasspathEntry.CPE_SOURCE: - list.add(entry.path); - break; - case IClasspathEntry.CPE_LIBRARY: - list.add(entry.path); - break; - default: - System.out.println("Wot's this then? " + entry); - } - } - } -} diff --git a/src/core/lombok/eclipse/handlers/HandleWither.java b/src/core/lombok/eclipse/handlers/HandleWither.java index e114ab19..27fbc635 100644 --- a/src/core/lombok/eclipse/handlers/HandleWither.java +++ b/src/core/lombok/eclipse/handlers/HandleWither.java @@ -31,13 +31,11 @@ import java.util.Collections; import java.util.List; import lombok.AccessLevel; -import lombok.Lombok; import lombok.core.AST.Kind; import lombok.core.AnnotationValues; import lombok.core.TransformationsUtil; import lombok.eclipse.EclipseAnnotationHandler; import lombok.eclipse.EclipseNode; -import lombok.eclipse.EclipseProjectSearcher; import lombok.eclipse.handlers.EclipseHandlerUtil.FieldAccess; import lombok.experimental.Wither; @@ -58,7 +56,6 @@ import org.eclipse.jdt.internal.compiler.ast.ThisReference; import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; import org.eclipse.jdt.internal.compiler.ast.TypeReference; import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; -import org.eclipse.jdt.internal.core.JavaProject; import org.mangosdk.spi.ProviderFor; @ProviderFor(EclipseAnnotationHandler.class) @@ -127,18 +124,6 @@ public class HandleWither extends EclipseAnnotationHandler { AccessLevel level = annotation.getInstance().value(); if (level == AccessLevel.NONE || node == null) return; - /** hackery */ { - JavaProject project = annotationNode.getAst().getProject(); - if (project != null) try { - new EclipseProjectSearcher(project).findAllWithName("/java/lang/String.class"); - } catch (Exception e) { - throw Lombok.sneakyThrow(e); - } else { - Thread.dumpStack(); - System.err.println("****!!!!*!*!*!*!*!*!!*!********!!!! PROJECT IS NULL!!!!"); - } - } - List onMethod = unboxAndRemoveAnnotationParameter(ast, "onMethod", "@Setter(onMethod=", annotationNode); List onParam = unboxAndRemoveAnnotationParameter(ast, "onParam", "@Setter(onParam=", annotationNode); diff --git a/src/core/lombok/javac/JavacAST.java b/src/core/lombok/javac/JavacAST.java index 31bdc3a6..5064f852 100644 --- a/src/core/lombok/javac/JavacAST.java +++ b/src/core/lombok/javac/JavacAST.java @@ -22,6 +22,7 @@ package lombok.javac; import java.lang.reflect.Field; +import java.net.URI; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -88,7 +89,16 @@ public class JavacAST extends AST { this.javacTypes = JavacTypes.instance(context); clearChanged(); } - + + @Override public URI getAbsoluteFileLocation() { + try { + JCCompilationUnit cu = (JCCompilationUnit) top().get(); + return cu.sourcefile.toUri(); + } catch (Exception e) { + return null; + } + } + private static String sourceName(JCCompilationUnit cu) { return cu.sourcefile == null ? null : cu.sourcefile.toString(); } -- cgit