Skip to content

Commit

Permalink
[#1033] steps on the way to issue 1033: You can add cleanup tasks whi…
Browse files Browse the repository at this point in the history
…ch are deferred (during the javac run) until the end.

This already fixes the exotic-to-the-point-of-nonexistent bug where setter and wither compete to steal the `@param` off of the field’s javadoc. Next are to fix builder and setter/wither competing whilst bringing javadocs to `@Builder`.

Then for various other conflicts, we should defer removal of lombok imports and annotations until the end too.
  • Loading branch information
rzwitserloot committed Jan 8, 2019
1 parent 20df861 commit ccfab5e
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 114 deletions.
1 change: 1 addition & 0 deletions doc/changelog.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Lombok Changelog

### v1.18.5 "Edgy Guinea Pig"
* BUGFIX: Since version 1.18.4, the delombok ant task didn't work and errored with a `NoClassDefFoundError`. [Issue #1932](https://github.com/rzwitserloot/lombok/issues/1932)
* BUGFIX: Combining both `@Setter` and `@Wither` on the same field, when that field also has javadoc with a `--setter--` section or an `@param` tag, resulted in a race condition where the first handler to get to the field would take that part of the javadoc. This is a step along the way to fixing [Issue #1033](https://github.com/rzwitserloot/lombok/issues/1033)
* FEATURE: The `@FieldNameConstants` feature now allows you to write the inner type by hand and add whatever you like to it; lombok will add the constants to this class. See the updated [FieldNameConstants feature](https://projectlombok.org/features/experimental/FieldNameConstants) page.
* FEATURE: There is now a `lombok.config` key to configure `@ToString`'s call super behavior; it's just like `@EqualsAndHashCode` which has had it for a while now. [Issue #1918](https://github.com/rzwitserloot/lombok/issues/1918)
* ENHANCEMENT: The toString generation of enums now contains the name of the enum constant. [Issue #1916](https://github.com/rzwitserloot/lombok/issues/1916)
Expand Down
64 changes: 64 additions & 0 deletions src/core/lombok/core/CleanupRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2019 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.core;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CleanupRegistry {
private static final class CleanupKey {
private final String key;
private final Object target;

CleanupKey(String key, Object target) {
this.key = key;
this.target = target;
}

@Override public boolean equals(Object other) {
if (other == null) return false;
if (other == this) return true;
if (!(other instanceof CleanupKey)) return false;
CleanupKey o = (CleanupKey) other;
if (!key.equals(o.key)) return false;
return target == o.target;
}

@Override public int hashCode() {
return 109 * System.identityHashCode(target) + key.hashCode();
}
}

private final Map<CleanupKey, CleanupTask> tasks = new ConcurrentHashMap<CleanupKey, CleanupTask>();

public void registerTask(String key, Object target, CleanupTask task) {
CleanupKey ck = new CleanupKey(key, target);
tasks.putIfAbsent(ck, task);
}

public void run() {
for (CleanupTask task : tasks.values()) {
task.cleanup();
}
tasks.clear();
}
}
26 changes: 26 additions & 0 deletions src/core/lombok/core/CleanupTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2019 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package lombok.core;

public interface CleanupTask {
void cleanup();
}
12 changes: 10 additions & 2 deletions src/core/lombok/javac/JavacAST.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2018 The Project Lombok Authors.
* Copyright (C) 2009-2019 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -37,6 +37,8 @@

import com.sun.tools.javac.util.JCDiagnostic;
import lombok.core.AST;
import lombok.core.CleanupRegistry;
import lombok.core.CleanupTask;
import lombok.permit.Permit;

import com.sun.tools.javac.code.Source;
Expand Down Expand Up @@ -65,6 +67,7 @@
* something javac's own AST system does not offer.
*/
public class JavacAST extends AST<JavacAST, JavacNode, JCTree> {
private final CleanupRegistry cleanup;
private final JavacElements elements;
private final JavacTreeMaker treeMaker;
private final Symtab symtab;
Expand All @@ -80,7 +83,7 @@ public class JavacAST extends AST<JavacAST, JavacNode, JCTree> {
* @param context A Context object for interfacing with the compiler.
* @param top The compilation unit, which serves as the top level node in the tree to be built.
*/
public JavacAST(Messager messager, Context context, JCCompilationUnit top) {
public JavacAST(Messager messager, Context context, JCCompilationUnit top, CleanupRegistry cleanup) {
super(sourceName(top), PackageName.getPackageName(top), new JavacImportList(top), statementTypes());
setTop(buildCompilationUnit(top));
this.context = context;
Expand All @@ -90,6 +93,7 @@ public JavacAST(Messager messager, Context context, JCCompilationUnit top) {
this.treeMaker = new JavacTreeMaker(TreeMaker.instance(context));
this.symtab = Symtab.instance(context);
this.javacTypes = JavacTypes.instance(context);
this.cleanup = cleanup;
clearChanged();
}

Expand Down Expand Up @@ -140,6 +144,10 @@ void traverseChildren(JavacASTVisitor visitor, JavacNode node) {
return Javac.getJavaCompilerVersion();
}

public void cleanupTask(String key, JCTree target, CleanupTask task) {
cleanup.registerTask(key, target, task);
}

/** @return A Name object generated for the proper name table belonging to this AST. */
public Name toName(String name) {
return elements.getName(name);
Expand Down
7 changes: 4 additions & 3 deletions src/core/lombok/javac/JavacTransformer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2018 The Project Lombok Authors.
* Copyright (C) 2009-2019 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -36,6 +36,7 @@
import com.sun.tools.javac.util.List;

import lombok.ConfigurationKeys;
import lombok.core.CleanupRegistry;
import lombok.core.LombokConfiguration;

public class JavacTransformer {
Expand All @@ -55,7 +56,7 @@ public SortedSet<Long> getPrioritiesRequiringResolutionReset() {
return handlers.getPrioritiesRequiringResolutionReset();
}

public void transform(long priority, Context context, java.util.List<JCCompilationUnit> compilationUnitsRaw) {
public void transform(long priority, Context context, java.util.List<JCCompilationUnit> compilationUnitsRaw, CleanupRegistry cleanup) {
List<JCCompilationUnit> compilationUnits;
if (compilationUnitsRaw instanceof List<?>) {
compilationUnits = (List<JCCompilationUnit>) compilationUnitsRaw;
Expand All @@ -70,7 +71,7 @@ public void transform(long priority, Context context, java.util.List<JCCompilati

for (JCCompilationUnit unit : compilationUnits) {
if (!Boolean.TRUE.equals(LombokConfiguration.read(ConfigurationKeys.LOMBOK_DISABLE, JavacAST.getAbsoluteFileLocation(unit)))) {
asts.add(new JavacAST(messager, context, unit));
asts.add(new JavacAST(messager, context, unit, cleanup));
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/core/lombok/javac/apt/LombokProcessor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2018 The Project Lombok Authors.
* Copyright (C) 2009-2019 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -50,6 +50,7 @@
import javax.tools.JavaFileObject;

import lombok.Lombok;
import lombok.core.CleanupRegistry;
import lombok.core.DiagnosticsReceiver;
import lombok.javac.JavacTransformer;
import lombok.permit.Permit;
Expand Down Expand Up @@ -288,11 +289,15 @@ private void stopJavacProcessingEnvironmentFromClosingOurClassloader() {
private final IdentityHashMap<JCCompilationUnit, Long> roots = new IdentityHashMap<JCCompilationUnit, Long>();
private long[] priorityLevels;
private Set<Long> priorityLevelsRequiringResolutionReset;
private CleanupRegistry cleanup = new CleanupRegistry();

/** {@inheritDoc} */
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (lombokDisabled) return false;
if (roundEnv.processingOver()) return false;
if (roundEnv.processingOver()) {
cleanup.run();
return false;
}

// We have: A sorted set of all priority levels: 'priorityLevels'

Expand All @@ -318,7 +323,7 @@ private void stopJavacProcessingEnvironmentFromClosingOurClassloader() {
if (prioOfCu == null || prioOfCu != prio) continue;
cusForThisRound.add(entry.getKey());
}
transformer.transform(prio, javacProcessingEnv.getContext(), cusForThisRound);
transformer.transform(prio, javacProcessingEnv.getContext(), cusForThisRound, cleanup);
}

// Step 3: Push up all CUs to the next level. Set level to null if there is no next level.
Expand Down
Loading

0 comments on commit ccfab5e

Please sign in to comment.