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

review: fix: replace Infinity, NaN in reflection model with divisions #5445

Merged
merged 2 commits into from
Sep 11, 2023
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 @@ -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");
}

}

}