Skip to content

Commit

Permalink
fix: replace Infinity and NaN in reflection model with divisions (#5445)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell authored Sep 11, 2023
1 parent f815806 commit adedea9
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import java.util.Deque;
import java.util.Iterator;
import java.util.Set;
import spoon.reflect.code.CtLiteral;

import spoon.reflect.code.BinaryOperatorKind;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtExpression;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtAnnotationMethod;
import spoon.reflect.declaration.CtAnnotationType;
Expand Down Expand Up @@ -337,7 +340,7 @@ public void visitField(Field field) {
if (modifiers.contains(ModifierKind.STATIC)
&& modifiers.contains(ModifierKind.PUBLIC)
&& (field.getType().isPrimitive() || String.class.isAssignableFrom(field.getType()))) {
CtLiteral<Object> defaultExpression = factory.createLiteral(field.get(null));
CtExpression<Object> defaultExpression = buildExpressionForValue(field.get(null));
ctField.setDefaultExpression(defaultExpression);
}
} catch (IllegalAccessException | ExceptionInInitializerError | UnsatisfiedLinkError e) {
Expand All @@ -351,6 +354,37 @@ public void visitField(Field field) {
contexts.peek().addField(ctField);
}

private CtExpression<Object> buildExpressionForValue(Object value) {
if (value instanceof Double) {
double d = (double) value;
if (Double.isNaN(d)) {
return buildDivision(0.0d, 0.0d);
}
if (Double.POSITIVE_INFINITY == d) {
return buildDivision(1.0d, 0.0d);
}
if (Double.NEGATIVE_INFINITY == d) {
return buildDivision(-1.0d, 0.0d);
}
} else if (value instanceof Float) {
float f = (float) value;
if (Float.isNaN(f)) {
return buildDivision(0.0f, 0.0f);
}
if (Float.POSITIVE_INFINITY == f) {
return buildDivision(1.0f, 0.0f);
}
if (Float.NEGATIVE_INFINITY == f) {
return buildDivision(-1.0f, 0.0f);
}
}
return factory.createLiteral(value);
}

private CtBinaryOperator<Object> buildDivision(Object first, Object second) {
return factory.createBinaryOperator(factory.createLiteral(first), factory.createLiteral(second), BinaryOperatorKind.DIV);
}

@Override
public void visitEnumValue(Field field) {
final CtEnumValue<Object> ctEnumValue = factory.Core().createEnumValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import spoon.reflect.code.CtConditional;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtLiteral;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtAnnotation;
Expand Down Expand Up @@ -957,5 +958,32 @@ void testInnerClassesAreNotAddedToPackage(String collider) throws ClassNotFoundE
assertNull(victim.getField("foo"));
}

@Test
void test() throws ClassNotFoundException {
// contract: Infinity, -Infinity, NaN are not literals
ClassLoader loader = JavacFacade.compileFiles(
Map.of(
"SpecialValues.java",
"public class SpecialValues {\n" +
" public static final double d_inf = 1.0d / 0.0d;\n" +
" public static final double d_m_inf = -1.0d / 0.0d;\n" +
" public static final double d_nan = 0.0d / 0.0d;\n" +
" public static final float f_inf = 1.0f / 0.0f;\n" +
" public static final float f_m_inf = -1.0f / 0.0f;\n" +
" public static final float f_nan = 0.0f / 0.0f;\n" +
"}\n"
),
List.of()
);

Factory factory = createFactory();
// Load the class
CtType<?> specialValues = factory.Type().get(loader.loadClass("SpecialValues"));
for (CtField<?> field : specialValues.getFields()) {
assertNotNull(field.getDefaultExpression());
assertFalse(field.getDefaultExpression() instanceof CtLiteral<?>, "special value cannot be represented by literal");
}

}

}

0 comments on commit adedea9

Please sign in to comment.