Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store transformation state in CompilationUnit #3546

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/core/lombok/eclipse/EcjAugments.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014 The Project Lombok Authors.
* Copyright (C) 2014-2023 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 All @@ -21,12 +21,13 @@
*/
package lombok.eclipse;

import java.util.Map;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

import org.eclipse.jdt.internal.compiler.ast.ASTNode;
import org.eclipse.jdt.internal.compiler.ast.Annotation;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.core.CompilationUnit;
Expand All @@ -44,6 +45,7 @@ private EcjAugments() {
public static final FieldAugment<ASTNode, ASTNode> ASTNode_generatedBy = FieldAugment.augment(ASTNode.class, ASTNode.class, "$generatedBy");
public static final FieldAugment<Annotation, Boolean> Annotation_applied = FieldAugment.augment(Annotation.class, boolean.class, "lombok$applied");
public static final FieldAugment<ICompilationUnit, Map<String, String>> CompilationUnit_javadoc = FieldAugment.augment(ICompilationUnit.class, Map.class, "$javadoc");
public static final FieldAugment<CompilationUnitDeclaration, TransformationState> CompilationUnitDeclaration_transformationState = FieldAugment.augment(CompilationUnitDeclaration.class, Object.class, "$transformationState");

public static final class EclipseAugments {
private EclipseAugments() {
Expand Down
26 changes: 11 additions & 15 deletions src/core/lombok/eclipse/TransformEclipseAST.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2020 The Project Lombok Authors.
* Copyright (C) 2009-2023 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 All @@ -21,12 +21,10 @@
*/
package lombok.eclipse;

import static lombok.eclipse.EcjAugments.CompilationUnitDeclaration_transformationState;
import static lombok.eclipse.handlers.EclipseHandlerUtil.*;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;

import lombok.ConfigurationKeys;
import lombok.core.LombokConfiguration;
Expand Down Expand Up @@ -63,7 +61,6 @@ public class TransformEclipseAST {

public static boolean disableLombok = false;
private static final HistogramTracker lombokTracker;
private static Map<CompilationUnitDeclaration, State> transformationStates = Collections.synchronizedMap(new WeakHashMap<CompilationUnitDeclaration, State>());

static {
String v = System.getProperty("lombok.histogram");
Expand Down Expand Up @@ -142,14 +139,14 @@ public static EclipseAST getAST(CompilationUnitDeclaration ast, boolean forceReb
* @return <code>true</code> if this AST was already handled by lombok.
*/
public static boolean alreadyTransformed(CompilationUnitDeclaration ast) {
State state = transformationStates.get(ast);
TransformationState state = CompilationUnitDeclaration_transformationState.get(ast);

if (state == State.FULL) return true;
if (state == State.DIET) {
if (state == TransformationState.FULL) return true;
if (state == TransformationState.DIET) {
if (!EclipseAST.isComplete(ast)) return true;
transformationStates.put(ast, State.FULL);
CompilationUnitDeclaration_transformationState.set(ast, TransformationState.FULL);
} else {
transformationStates.put(ast, State.DIET);
CompilationUnitDeclaration_transformationState.set(ast, TransformationState.DIET);
}
return false;
}
Expand All @@ -168,6 +165,10 @@ public static boolean alreadyTransformed(CompilationUnitDeclaration ast) {
public static void transform(Parser parser, CompilationUnitDeclaration ast) {
if (disableLombok) return;

// Skip module-info.java
char[] fileName = ast.getFileName();
if (fileName != null && String.valueOf(fileName).endsWith("module-info.java")) return;

if (Symbols.hasSymbol("lombok.disable")) return;
if (alreadyTransformed(ast)) return;

Expand Down Expand Up @@ -270,9 +271,4 @@ public long getNextPriority() {
nextPriority = Math.min(nextPriority, handlers.handleAnnotation(top, annotationNode, annotation, priority));
}
}

private static enum State {
DIET,
FULL
}
}
27 changes: 27 additions & 0 deletions src/core/lombok/eclipse/TransformationState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2023 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.eclipse;

enum TransformationState {
DIET,
FULL
}
7 changes: 6 additions & 1 deletion src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2021 The Project Lombok Authors.
* Copyright (C) 2009-2023 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 @@ -702,6 +702,11 @@ private static void patchLombokizeAST(ScriptManager sm) {
.fieldName("$lombokAST").fieldType("Ljava/lang/Object;")
.setPublic().setTransient().build());

sm.addScript(ScriptBuilder.addField()
.targetClass("org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration")
.fieldName("$transformationState").fieldType("Ljava/lang/Object;")
.setPublic().setTransient().build());

final String PARSER_SIG = "org.eclipse.jdt.internal.compiler.parser.Parser";
final String CUD_SIG = "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration";
final String OBJECT_SIG = "java.lang.Object";
Expand Down
Loading