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

Remove outer type reference when marking class as static #3789

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 29 additions & 0 deletions .github/workflows/ant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,32 @@ jobs:

- name: Compile in container
run: docker run --entrypoint="" -v $(pwd)/lombok.jar:/workspace/lombok.jar $IMAGE_NAME /bin/bash -c "cd classpath; ${{ matrix.tool.cmd }}"

manual-tests:
runs-on: ubuntu-24.04
needs: build
strategy:
matrix:
jdk: [8, 11, 17, 21, 23]
dir: [compileTests]
fail-fast: false

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up JDK ${{ matrix.jdk }}
uses: actions/setup-java@v4
with:
java-version: ${{ matrix.jdk }}
distribution: 'zulu'

- uses: actions/download-artifact@v4
with:
name: lombok.jar
path: dist

- name: Run tests
working-directory: ./test/manual/${{ matrix.dir }}/
run: ./runTests.sh

24 changes: 13 additions & 11 deletions src/core/lombok/javac/handlers/HandleUtilityClass.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2015-2021 The Project Lombok Authors.
* Copyright (C) 2015-2024 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 @@ -107,7 +107,7 @@ private void changeModifiersAndGenerateConstructor(JavacNode typeNode, JavacNode
if ((typeDecl.mods.flags & (Flags.INTERFACE | Flags.ANNOTATION)) != 0) markStatic = false;
}

if (markStatic) classDecl.mods.flags |= Flags.STATIC;
if (markStatic) markClassAsStatic(classDecl);

for (JavacNode element : typeNode.down()) {
if (element.getKind() == Kind.FIELD) {
Expand All @@ -125,21 +125,23 @@ private void changeModifiersAndGenerateConstructor(JavacNode typeNode, JavacNode

methodDecl.mods.flags |= Flags.STATIC;
} else if (element.getKind() == Kind.TYPE) {
JCClassDecl innerClassDecl = (JCClassDecl) element.get();
innerClassDecl.mods.flags |= Flags.STATIC;
ClassSymbol innerClassSymbol = innerClassDecl.sym;
if (innerClassSymbol != null) {
if (innerClassSymbol.type instanceof ClassType) {
((ClassType) innerClassSymbol.type).setEnclosingType(Type.noType);
}
innerClassSymbol.erasure_field = null;
}
markClassAsStatic((JCClassDecl) element.get());
}
}

if (makeConstructor) createPrivateDefaultConstructor(typeNode);
}

private void markClassAsStatic(JCClassDecl classDecl) {
classDecl.mods.flags |= Flags.STATIC; // maybe Flags.NOOUTERTHIS too?
ClassSymbol innerClassSymbol = classDecl.sym;
if (innerClassSymbol != null && innerClassSymbol.type instanceof ClassType) {
ClassType classType = (ClassType) innerClassSymbol.type;
classType.setEnclosingType(Type.noType);
innerClassSymbol.erasure_field = null;
}
}

private void createPrivateDefaultConstructor(JavacNode typeNode) {
JavacTreeMaker maker = typeNode.getTreeMaker();
JCModifiers mods = maker.Modifiers(Flags.PRIVATE, List.<JCAnnotation>nil());
Expand Down
1 change: 1 addition & 0 deletions test/manual/compileTests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/out
20 changes: 20 additions & 0 deletions test/manual/compileTests/runTests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh
set -e
echo 'This will compile a selection of files in test/transform/resource/before. If the compilation works without error, lombok is working as designed.'

mkdir -p out/

# list of files to iterate over
FILES="UtilityClassInner.java"
BASE_PATH="../../../test/transform/resource/before"
LOMBOK_JAR="../../../dist/lombok.jar"

# compile all files in list
for f in $FILES
do

echo "Compiling $f"
javac -processorpath $LOMBOK_JAR -cp $LOMBOK_JAR -d out/ $BASE_PATH/$f
done

rm -rf out/
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@java.lang.SuppressWarnings("serial")
class UtilityClassInner {
static final class UtilClass {
static final class UtilClass implements java.io.Serializable {
@java.lang.SuppressWarnings("all")
@lombok.Generated
private UtilClass() {
Expand Down
4 changes: 2 additions & 2 deletions test/transform/resource/after-ecj/UtilityClassInner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UtilityClassInner {
static final @lombok.experimental.UtilityClass class UtilClass {
@java.lang.SuppressWarnings("serial") class UtilityClassInner {
static final @lombok.experimental.UtilityClass class UtilClass implements java.io.Serializable {
private @java.lang.SuppressWarnings("all") @lombok.Generated UtilClass() {
super();
throw new java.lang.UnsupportedOperationException("This is a utility class and cannot be instantiated");
Expand Down
3 changes: 2 additions & 1 deletion test/transform/resource/before/UtilityClassInner.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@java.lang.SuppressWarnings("serial")
class UtilityClassInner {
@lombok.experimental.UtilityClass
class UtilClass {}
class UtilClass implements java.io.Serializable {}
}
Loading