Unlock Complex Math in Java with MXReflection: Annotation‑Based Calculator
MXReflection is a Java framework built on the mXparser library that lets developers perform sophisticated mathematical calculations using simple @Arg and @Expression annotations, with Maven/Gradle integration, comprehensive function support, and example-driven usage for both simple and complex scenarios.
MXReflection: A Java Framework for Complex Calculations Based on mXparser
Remember those complicated math formulas from school—sin, log2, tan, and the like? MXReflection allows you to evaluate such expressions in Java by annotating class fields, eliminating the need to write manual parsing code.
The framework reads values from annotated fields and injects the computed result into a field marked with @Expression.
<dependency>
<groupId>com.github.ismail-mekni</groupId>
<artifactId>mxreflection</artifactId>
<version>1.0.1</version>
</dependency> dependencies {
compile group: 'com.github.ismail-mekni', name: 'mxreflection', version: '1.0.1'
}Using the framework is straightforward: you only need two annotations. @Arg("name") specifies the custom parameter name to be used in the target function. @Expression("expression") contains the function expression that may reference the parameters.
Below are two illustrative examples.
Example 1
package com.ismail.mxreflection.example;
import com.ismail.mxreflection.annotations.Arg;
import com.ismail.mxreflection.annotations.Expression;
import com.ismail.mxreflection.core.Calculator;
import com.ismail.mxreflection.factory.MXFactory;
import org.junit.Test;
public class Example1Test {
class Example1 {
@Arg("f1")
String field1;
@Arg("f2")
int field2;
@Expression("f1 * sin(f2) * log2(f1 + f2) + der(cos(f1), f1) * pi + int(tan(f2), f2, 0, e)")
double field3;
}
@Test
public void example1Test() {
Example1 example1 = new Example1();
example1.field1 = "2.2";
example1.field2 = 5;
Calculator<Example1> calculator = MXFactory.createCalculator(Example1.class);
calculator.calculate(example1);
System.out.println("Field 3 result: " + example1.field3);
}
}Output:
Field 3 result: -34.32819235851987Example 2
package com.ismail.mxreflection.example;
import com.ismail.mxreflection.annotations.Arg;
import com.ismail.mxreflection.annotations.Expression;
import com.ismail.mxreflection.core.Calculator;
import com.ismail.mxreflection.factory.MXFactory;
import org.junit.Test;
public class Example2Test {
public class Example2 {
@Arg("f1")
private String field1;
@Arg("f2")
private Long field2;
@Expression("f2 - f1")
@Arg("f3")
private double field3;
@Expression("f3 - f2")
@Arg("f4")
private double field4;
@Expression("f1 - f2")
@Arg("f5")
private Double field5;
@Expression("f4-f5")
@Arg("f6")
private String field6;
@Expression("f6-f5")
@Arg("f7")
private long field7;
@Expression("f7+5")
private Long field8;
}
@Test
public void exampleTest() {
Example2 example2 = new Example2();
example2.field1 = "2.2";
example2.field2 = 5L;
Calculator<Example2> calculator = MXFactory.createCalculator(Example2.class);
calculator.calculate(example2);
System.out.println("Field 3 result: " + example2.field3);
System.out.println("Field 4 result: " + example2.field4);
System.out.println("Field 5 result: " + example2.field5);
System.out.println("Field 6 result: " + example2.field6);
System.out.println("Field 7 result: " + example2.field7);
System.out.println("Field 8 result: " + example2.field8);
}
}Output:
Field 3 result: 2.8
Field 4 result: -2.2
Field 5 result: -2.8
Field 6 result: 0.6
Field 7 result: 3
Field 8 result: 8MXReflection supports the full set of mathematical operators, functions, constants, and units provided by the mXparser library, including:
Operators (+, -, *, /, #, !, ^)
Binary Relations (=, ==, <=, >=, <, >, <>, !=, ~=)
Boolean Operators (&, &&, /, ~&, ~&&, ~/, |, ||…)
Bitwise Operators (@~, @&, @^, @|, @<<, @>>)
Unary Functions (sin, cos, tan, …)
Binary Functions (log, mod, C, Bern, Stirl1, Stirl2, …)
3‑argument Functions (if, chi, CHi, …)
Variadic Functions (iff, min, max, ConFrac, …)
Iterated Operators (sum, prod, avg, vari, stdi, mini, maxi)
Calculus Operators (int, der, der-, der+, dern, diff, difb)
Math Constants (pi, e, [gam], [phi], …)
Physical Constants ([c], [G.], [g], …)
Astronomical Constants ([ly], [au], [pc], …)
Random Variables ([Uni], [Int], …)
Metric Prefixes ([%], [%%], [Y], …)
Parser Symbols ((, ), ,, ;)
Units
For parameter parsing, MXReflection accepts any Java field type that can be converted to a numeric string. Supported result field types are:
Double / double
Long / long
String
BigInteger
Note: For long and BigInteger fields, the framework parses the final numeric result before injection, so ensure the expression returns an integer value.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
