You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For some reason spoon adds the Test. to the type and does not mark it implicit:
Test.A.BmyVariable;
^^^^ thisisnotimplicit
What is the use-case?
I want to check if types are (unnecessarily) qualified in the source code. If someone writes Test.A.B, then I want to suggest replacing it with A.B, because the Test. is redundant.
Workaround?
If you do not care about arrays, then you can simply check if !type.getPosition().isValid().
I care about arrays, because I do want to detect Test.A.B[] as well, which makes this a bit more complicated.
My solution is to check if type.getPosition().isValid() || type.getParent(CtArrayTypeReference.class) != null. This seems to work mostly, but I think it will result in problems in the future...
@I-Al-Istannen has spent some time looking into this issue, but it seems like something difficult to fix.
importorg.junit.jupiter.api.Test;
importspoon.Launcher;
importspoon.reflect.declaration.CtClass;
importspoon.reflect.declaration.CtField;
importspoon.reflect.reference.CtTypeReference;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importstaticorg.junit.jupiter.api.Assertions.assertTrue;
classTestSpoon {
@TestvoidtestImplicitOuterClass() {
CtClass<?> ctClass = Launcher.parseClass(
"class Test {\n" +
" static class A { static class B {} }\n" +
"\n" +
" A.B variable = new A.B();\n" +
"}\n"
);
CtField<?> ctField = ctClass.getField("variable");
CtTypeReference<?> fieldType = ctField.getType();
assertEquals("B", fieldType.getSimpleName());
CtTypeReference<?> a = fieldType.getDeclaringType();
assertEquals("A", a.getSimpleName());
CtTypeReference<?> test = a.getDeclaringType();
assertEquals("Test", test.getSimpleName());
assertTrue(test.isImplicit(), "`Test` class access should be implicit, because it is not in the source code");
}
}
Describe the bug
Given
For some reason spoon adds the
Test.
to the type and does not mark itimplicit
:What is the use-case?
I want to check if types are (unnecessarily) qualified in the source code. If someone writes
Test.A.B
, then I want to suggest replacing it withA.B
, because theTest.
is redundant.Workaround?
If you do not care about arrays, then you can simply check if
!type.getPosition().isValid()
.I care about arrays, because I do want to detect
Test.A.B[]
as well, which makes this a bit more complicated.My solution is to check if
type.getPosition().isValid() || type.getParent(CtArrayTypeReference.class) != null
. This seems to work mostly, but I think it will result in problems in the future...@I-Al-Istannen has spent some time looking into this issue, but it seems like something difficult to fix.
Source code you are trying to analyze/transform
Source code for your Spoon processing
Actual output
Expected output
No response
Spoon Version
10.4.0
JVM Version
openjdk version "17.0.1" 2021-10-19 OpenJDK Runtime Environment Temurin-17.0.1+12 (build 17.0.1+12) OpenJDK 64-Bit Server VM Temurin-17.0.1+12 (build 17.0.1+12, mixed mode, sharing)
What operating system are you using?
Windows 10
The text was updated successfully, but these errors were encountered: