For what it's worth, most scripting languages (like Perl) and non-static compile-time languages (like Pick) support an automatic dynamic String sequence for (relatively arbitrary) objects. This MAY be implemented in Java, as well as without loss of type safety and the good materials that statically typed languages provide WITHOUT the unpleasant side effects of some other languages that do evil with dynamic casting. An example of Perl that calls up some dubious math:
print ++($foo = '99');
In Java, this is better done (IMHO) using a method that I call cross-casting. With cross-casting, reflection is used in the lazy cache of constructors and methods that are dynamically detected using the following static method:
Object fromString (String value, Class targetClass)
Unfortunately, no built-in Java methods, such as Class.cast (), will do this for String for BigDecimal or String for Integer or any other conversion where there is no supporting class hierarchy. For its part, the goal is to provide a fully dynamic way to achieve this goal - for which I do not think the previous link is the right approach - the need to encode every transformation. Simply put, an implementation is just different from the string if it is legal / possible.
Thus, the decision is a simple reflection seeking public members:
STRING_CLASS_ARRAY = (new class [] {String.class});
a) Member = targetClass.getMethod (method.getName (), STRING_CLASS_ARRAY); b) Member = targetClass.getConstructor (STRING_CLASS_ARRAY);
You will find that all primitives (Integer, Long, etc.) and all the basics (BigInteger, BigDecimal, etc.) and even java.regex.Pattern are covered by this approach. I used this with considerable success in production projects where there are a huge number of arbitrary values of String values, where a more stringent check is required. In this approach, if there is no method or when the method is called, an exception is thrown (since this is an invalid value, such as non-numeric input in BigDecimal or illegal RegEx for the template), which provides a check specific to the logic of the target class.
There are some disadvantages:
1) You need a good understanding of reflection (this is a bit complicated, not for beginners). 2) Some of the Java classes and truly third-party libraries (surprises) are incorrectly encoded. That is, there are methods that take one string argument as input and return an instance of the target class, but this is not what you think ... Consider the Integer class:
static Integer getInteger(String nm) Determines the integer value of the system property with the specified name.
The above method really has nothing to do with integers as objects that combine ints primitives. Reflection will find this as a possible candidate for creating Integer from String incorrectly compared to decode, valueof and constructor members, which are suitable for most arbitrary String conversions, where you really don't control your input, but just want to know if an integer is possible .
To fix this, looking for methods that throw exceptions is a good start, because invalid input values that instantiate such objects should throw an exception. Unfortunately, implementations differ depending on whether the exceptions are declared checked or not. Integer.valueOf (String) throws a NumberFormatException thrown exception, but Pattern.compile () exceptions were not found while looking for reflections. Again, this dynamic cross-casting approach does not fail, I think this is a very non-standard implementation for declaring exceptions in object creation methods.
If someone wants to get more detailed information about how this was implemented, let me know, but I think this solution is much more flexible / extensible and with less code, without losing the good parts of type safety. Of course, it’s always better to “know your data”, but as many of us find, we are sometimes only recipients of unmanaged content and should do everything possible to use it properly.
Greetings.