There's this problem in Codewars https://www.codewars.com/kata/58a3fa665973c2a6e80000c4/train/java
We will have to just square root a very big number. Of course all of us would think about BigInteger.sqrt - but the challenge itself reject these words: reflection, biginteger, bigdecimal, runtime, process, script. (P/s you can't use unicode either).
Update: also blocked "newInstance", "invoke"
Well there's no fun in doing what intended for the task, so I currently trying to bypass this by using bytebuddy (luckily they don't reject this).
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.agent.ByteBuddyAgent;
import net.bytebuddy.dynamic.DynamicType.Loaded;
import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;
public class Kata {
public static String integerSquareRoot(String n) {
ByteBuddyAgent.install();
Loaded<Kata> dynamicType = new ByteBuddy().redefine(Kata.class).method(ElementMatchers.named("text"))
.intercept(FixedValue.value(new java.math.BigInteger(n).sqrt().toString())).make()
.load(Thread.currentThread().getContextClassLoader(), ClassReloadingStrategy.fromInstalledAgent());
return Kata.text("forsenCD");
}
public static String text(String n){
return n;
}
}
What it does here is to force the text() method to return what's in the intercept
This solution itself solve the task, but as you can see, it still have "BigInteger" in the intercept.
I'm noob at bytebuddy, so anyone have a way to solve this using bytebuddy reflection, that maybe can change the BigInteger in the intercept with a string (so I can use character array) to solve this.
Or maybe even using Nashorn engine to get to some JavaScript API.
Please feel free to share your thought.