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

fix: handling of String const in getCorrespondingRuntimeObject/convertElementToRuntimeObject #2978

Merged
merged 1 commit into from
May 18, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ public void visitField(Field field) {
try {
Set<ModifierKind> modifiers = RtHelper.getModifiers(field.getModifiers());
if (modifiers.contains(ModifierKind.STATIC)
&& modifiers.contains(ModifierKind.PUBLIC)
&& field.getType().isPrimitive()
&& modifiers.contains(ModifierKind.PUBLIC)
&& (field.getType().isPrimitive() || String.class.isAssignableFrom(field.getType()))
) {
CtLiteral<Object> defaultExpression = factory.createLiteral(field.get(null));
ctField.setDefaultExpression(defaultExpression);
Expand Down
15 changes: 9 additions & 6 deletions src/test/java/spoon/test/annotation/AnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1556,12 +1556,15 @@ public void testAnnotationArray() throws Exception {
public void testGetValueAsObject() {
// contract: annot.getValueAsObject now handles static values in binary classes
CtClass<?> cl =
Launcher.parseClass(
"public class C { @SuppressWarnings(\"a\"+Integer.SIZE) void m() {} }");
CtAnnotation<?> annot = cl.getMethodsByName("m").get(0).getAnnotations().get(0);

// this triggers an exception because "a"+Integer.SIZE is not known at runtime
assertEquals("[a32]", Arrays.toString((Object[]) annot.getValueAsObject("value")));
Launcher.parseClass("public class C { " +
" @SuppressWarnings(\"int\"+Integer.SIZE) void i() {} " +
" @SuppressWarnings(\"str\"+java.io.File.pathSeparator) void s() {} " +
"}");
CtAnnotation<?> annot_i = cl.getMethodsByName("i").get(0).getAnnotations().get(0);
CtAnnotation<?> annot_s = cl.getMethodsByName("s").get(0).getAnnotations().get(0);

assertEquals("[int" + Integer.SIZE + "]", Arrays.toString((Object[]) annot_i.getValueAsObject("value")));
assertEquals("[str" + File.pathSeparator + "]", Arrays.toString((Object[]) annot_s.getValueAsObject("value")));
}

}
3 changes: 3 additions & 0 deletions src/test/java/spoon/test/eval/EvalTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import spoon.support.reflect.eval.VisitorPartialEvaluator;
import spoon.test.eval.testclasses.Foo;

import java.io.File;
import java.util.List;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -232,6 +233,8 @@ public void testconvertElementToRuntimeObject() {
// also works for static runtime fields
assertEquals(Integer.MAX_VALUE, EvalHelper.convertElementToRuntimeObject(foo.getField("i1").getDefaultExpression()));
assertEquals(Integer.MAX_VALUE, EvalHelper.getCorrespondingRuntimeObject(foo.getField("i1").getDefaultExpression()));
assertEquals(File.pathSeparator, EvalHelper.convertElementToRuntimeObject(foo.getField("str1").getDefaultExpression()));
assertEquals(File.pathSeparator, EvalHelper.getCorrespondingRuntimeObject(foo.getField("str1").getDefaultExpression()));

}
}
4 changes: 3 additions & 1 deletion src/test/java/spoon/test/eval/testclasses/Foo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package spoon.test.eval.testclasses;

import java.io.File;

public class Foo {
final boolean b0 = true && false;
boolean b2 = true; // not final so not considered
Expand All @@ -8,7 +10,7 @@ public class Foo {
boolean b5 = true ^ false; // exec BITXOR
final boolean b6 = b0;
final int i1 = Integer.MAX_VALUE; // access to static field

final String str1 = File.pathSeparator;
void foo() {
boolean b1 = true ? false || b0 : b2; // will be simplified to "false"
}
Expand Down