Skip to content

Commit

Permalink
fix: handling of String const in getCorrespondingRuntimeObject/conver…
Browse files Browse the repository at this point in the history
…tElementToRuntimeObject (INRIA#2978)

fix INRIA#2977
  • Loading branch information
mictaege authored and monperrus committed May 18, 2019
1 parent d0aebbb commit 0c26805
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
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

0 comments on commit 0c26805

Please sign in to comment.