-
-
Notifications
You must be signed in to change notification settings - Fork 347
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
review: fix: Make CtFieldRead inherit from CtResource #5860
Merged
MartinWitt
merged 4 commits into
INRIA:master
from
I-Al-Istannen:fix/field-in-try-with-resources
Aug 2, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9b6f5f7
fix: Make CtField inherit from CtResource
I-Al-Istannen 5f2ea21
fix: Make CtFieldRead extend CtResource and remove CtVariable superty…
I-Al-Istannen 507cb3b
Adjust test to new model
I-Al-Istannen a5b4c1a
Fix CtInheritanceScanner
I-Al-Istannen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,29 +34,34 @@ | |
import spoon.reflect.CtModel; | ||
import spoon.reflect.code.CtCatch; | ||
import spoon.reflect.code.CtCatchVariable; | ||
import spoon.reflect.code.CtFieldRead; | ||
import spoon.reflect.code.CtLocalVariable; | ||
import spoon.reflect.code.CtResource; | ||
import spoon.reflect.code.CtTry; | ||
import spoon.reflect.code.CtTryWithResource; | ||
import spoon.reflect.code.CtVariableRead; | ||
import spoon.reflect.declaration.CtClass; | ||
import spoon.reflect.declaration.CtField; | ||
import spoon.reflect.declaration.CtMethod; | ||
import spoon.reflect.declaration.CtParameter; | ||
import spoon.reflect.declaration.CtVariable; | ||
import spoon.reflect.declaration.ModifierKind; | ||
import spoon.reflect.factory.Factory; | ||
import spoon.reflect.reference.CtCatchVariableReference; | ||
import spoon.reflect.reference.CtLocalVariableReference; | ||
import spoon.reflect.reference.CtTypeReference; | ||
import spoon.reflect.reference.CtVariableReference; | ||
import spoon.reflect.visitor.filter.AbstractFilter; | ||
import spoon.reflect.visitor.filter.TypeFilter; | ||
import spoon.support.compiler.VirtualFile; | ||
import spoon.support.reflect.CtExtendedModifier; | ||
import spoon.test.trycatch.testclasses.Foo; | ||
import spoon.test.trycatch.testclasses.Main; | ||
import spoon.testing.utils.ModelTest; | ||
import spoon.testing.utils.LineSeparatorExtension; | ||
|
||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
@@ -236,6 +241,7 @@ public void testCompileMultiTryCatchWithCustomExceptions() { | |
fail(e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testTryCatchVariableGetType() { | ||
Factory factory = createFactory(); | ||
|
@@ -376,7 +382,7 @@ public void testCatchUnqualifiedReferenceMarkedSimplyQualifiedWhenMultipleTypesA | |
|
||
CtCatch targetCatch = catches.get(0); | ||
List<CtTypeReference<?>> paramTypes = targetCatch.getParameter().getMultiTypes(); | ||
assertThat(paramTypes.size(), equalTo(2)); | ||
assertThat(paramTypes).hasSize(2); | ||
assertTrue(paramTypes.get(0).isSimplyQualified(), "first type reference is fully qualified"); | ||
assertTrue(paramTypes.get(1).isSimplyQualified(), "second type reference is fully qualified"); | ||
} | ||
|
@@ -393,7 +399,7 @@ public void testCatchWithQualifiedAndUnqualifiedTypeReferencesInSameCatcher() th | |
|
||
CtCatch targetCatch = catches.get(0); | ||
List<CtTypeReference<?>> paramTypes = targetCatch.getParameter().getMultiTypes(); | ||
assertThat(paramTypes.size(), equalTo(2)); | ||
assertThat(paramTypes).hasSize(2); | ||
assertTrue(paramTypes.get(0).isSimplyQualified(), "first type reference should be unqualified"); | ||
assertFalse(paramTypes.get(1).isSimplyQualified(), "second type reference should be qualified"); | ||
} | ||
|
@@ -411,10 +417,10 @@ public void testNonCloseableGenericTypeInTryWithResources(CtModel model) { | |
|
||
CtLocalVariableReference<?> varRef = model.filterChildren(CtLocalVariableReference.class::isInstance).first(); | ||
|
||
assertThat(varRef.getType().getQualifiedName(), equalTo("NonClosableGenericInTryWithResources.GenericType")); | ||
assertThat(varRef.getType().getQualifiedName()).isEqualTo("NonClosableGenericInTryWithResources.GenericType"); | ||
|
||
// We don't extract the type arguments | ||
assertThat(varRef.getType().getActualTypeArguments().size(), equalTo(0)); | ||
assertThat(varRef.getType().getActualTypeArguments()).isEmpty(); | ||
} | ||
|
||
@ExtendWith(LineSeparatorExtension.class) | ||
|
@@ -431,15 +437,15 @@ public void testTryWithVariableAsResource() { | |
List<CtResource<?>> resources = tryStmt.getResources(); | ||
assertEquals(1, resources.size()); | ||
final CtResource<?> ctResource = resources.get(0); | ||
assertTrue(ctResource instanceof CtVariable); | ||
assertEquals("resource", ((CtVariable<?>) ctResource).getSimpleName()); | ||
assertThat(ctResource).isInstanceOf(CtVariableRead.class); | ||
assertThat(((CtVariableRead<?>) ctResource).getVariable().getSimpleName()).isEqualTo("resource"); | ||
|
||
// contract: pretty-printing of existing resources works | ||
assertEquals("try (resource) {\n}", tryStmt.toString()); | ||
assertThat(tryStmt).asString().isEqualTo("try (resource) {\n}"); | ||
|
||
// contract: removeResource does remove the resource | ||
tryStmt.removeResource(ctResource); | ||
assertEquals(0, tryStmt.getResources().size()); | ||
assertThat(tryStmt.getResources()).isEmpty(); | ||
// contract: removeResource of nothing is graceful and accepts it | ||
tryStmt.removeResource(ctResource); | ||
|
||
|
@@ -466,7 +472,7 @@ void addsCatcherAtTheSpecifiedPosition() { | |
.addCatcherAt(1, second); | ||
|
||
// assert | ||
assertThat(tryStatement.getCatchers(), contains(first, second, third)); | ||
assertThat(tryStatement.getCatchers()).containsExactlyInAnyOrder(first, second, third); | ||
} | ||
|
||
@Test | ||
|
@@ -492,4 +498,80 @@ private <T extends Throwable> CtCatch createCatch(Factory factory, Class<T> type | |
); | ||
} | ||
} | ||
|
||
@Test | ||
void testFieldAsCtResource() { | ||
// contract: Since java 9 normal variables, fields, parameters and catch variables are allowed | ||
// in try-with-resources | ||
CtModel model = createModelFromString(""" | ||
class ClosableException extends RuntimeException implements AutoCloseable { | ||
@Override | ||
public void close() throws Exception {} | ||
} | ||
class Foo { | ||
final AutoCloseable field = null; | ||
public void bar(AutoCloseable param) { | ||
try { } | ||
catch (ClosableException e) { | ||
AutoCloseable localVar = param; | ||
try (e; field; param; localVar; AutoCloseable inside = () -> {}) { | ||
} catch (Exception ignored) {} | ||
} | ||
} | ||
} | ||
"""); | ||
CtClass<?> foo = (CtClass<?>) model.getAllTypes() | ||
.stream() | ||
.filter(it -> it.getSimpleName().equals("Foo")) | ||
.findAny() | ||
.orElseThrow(); | ||
CtMethod<?> bar = foo.getMethodsByName("bar").get(0); | ||
|
||
CtCatchVariable<?> e = bar.getElements(new TypeFilter<>(CtCatchVariable.class)).get(0); | ||
CtLocalVariable<?> localVar = bar.getElements(new TypeFilter<>(CtLocalVariable.class)) | ||
.stream() | ||
.filter(it -> it.getSimpleName().equals("localVar")) | ||
.findAny() | ||
.orElseThrow(); | ||
CtField<?> field = foo.getField("field"); | ||
CtParameter<?> param = bar.getParameters().get(0); | ||
CtLocalVariable<?> inside = bar.getElements(new TypeFilter<>(CtLocalVariable.class)) | ||
.stream() | ||
.filter(it -> it.getSimpleName().equals("inside")) | ||
.findAny() | ||
.orElseThrow(); | ||
|
||
List<CtResource<?>> resources = bar.getElements(new TypeFilter<>(CtTryWithResource.class)) | ||
.get(0) | ||
.getResources(); | ||
assertThat(resources).containsExactlyInAnyOrder( | ||
getRead(field), | ||
getRead(param.getReference()), | ||
getRead(e.getReference()), | ||
getRead(localVar.getReference()), | ||
inside | ||
); | ||
} | ||
|
||
private <T> CtVariableRead<T> getRead(CtVariableReference<T> ref) { | ||
return ref.getFactory().<T>createVariableRead().<CtFieldRead<T>>setVariable(ref); | ||
} | ||
|
||
private <T> CtVariableRead<T> getRead(CtField<T> field) { | ||
Factory factory = field.getFactory(); | ||
CtFieldRead<T> read = factory.createFieldRead(); | ||
read.setVariable(field.getReference()); | ||
read.setTarget(factory.createThisAccess(field.getDeclaringType().getReference())); | ||
|
||
return read; | ||
} | ||
|
||
private static CtModel createModelFromString(String code) { | ||
Launcher launcher = new Launcher(); | ||
launcher.getEnvironment().setComplianceLevel(21); | ||
launcher.getEnvironment().setShouldCompile(true); | ||
launcher.addInputResource(new VirtualFile(code)); | ||
return launcher.buildModel(); | ||
} | ||
Comment on lines
+569
to
+575
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One day, we will have a single method for this :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And other lies I tell myself? despair |
||
|
||
} |
2 changes: 1 addition & 1 deletion
2
src/test/java/spoon/testing/assertions/CtResourceAssertInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package spoon.testing.assertions; | ||
import org.assertj.core.api.AbstractObjectAssert; | ||
import spoon.reflect.code.CtResource; | ||
public interface CtResourceAssertInterface<A extends AbstractObjectAssert<A, W>, W extends CtResource<?>> extends CtVariableAssertInterface<A, W> , SpoonAssert<A, W> {} | ||
public interface CtResourceAssertInterface<A extends AbstractObjectAssert<A, W>, W extends CtResource<?>> extends CtElementAssertInterface<A, W> , SpoonAssert<A, W> {} |
2 changes: 1 addition & 1 deletion
2
src/test/java/spoon/testing/assertions/CtVariableReadAssertInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package spoon.testing.assertions; | ||
import org.assertj.core.api.AbstractObjectAssert; | ||
import spoon.reflect.code.CtVariableRead; | ||
public interface CtVariableReadAssertInterface<A extends AbstractObjectAssert<A, W>, W extends CtVariableRead<?>> extends CtVariableAccessAssertInterface<A, W> , SpoonAssert<A, W> {} | ||
public interface CtVariableReadAssertInterface<A extends AbstractObjectAssert<A, W>, W extends CtVariableRead<?>> extends CtVariableAccessAssertInterface<A, W> , SpoonAssert<A, W> , CtResourceAssertInterface<A, W> {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will miss this easy to understand and maintainable code.