Dynamic null check [duplicate] - java

This question already has answers here:
Check chains of "get" calls for null
(11 answers)
Closed 3 years ago.
I intend to make a common dynamic null check function on any object before doing some work on it.
For example:
Class A{
B b;
// with getters and setters of b
}
class B{
C c;
//with getters and setters of c
}
class C{
BigInteger d;
//with getters and setters of d
}
now, I want to check whether objA.getB().getC().getD() returns some value or throws NullPointerException?
By common I mean I can pass any kind of object to it, something like below function
CheckNull.checkingDynamicNull( "objA.getB().getC().getD()" )
will return me true or false depending on the case.
Any ideas?

Unfortunately it is not possible to pass a funktion into a method in Java (yet, Java 8 will). Also, if you pass the variable name as String this wont work, since the recieving method has no way of knowing what Object is mapped to that variable (if you ignore reflection but even with that you are screwed if its a local variable).
Another way would be to do something like this
boolean nullCheck(Object obj, String Methods)
and then parse the String to Methods and invoke them on the object. But that wont work if you have Methods with arguments.
In short: It's more trouble then it's worth
In the most cases you want to have something like this
if(object == null){
//error behavior
}
This assumes that you want to throw a certain Exception, have means to get a value for the null object elswhere or want to do something before you throw an exception.
If you simply want to throw an Exception (that is not a NullPointerException)
assert object != null

What about this:
public boolean checkingDynamicNull(A objA) {
try {
objA.getB().getC().getD().toString();
} catch(NullPointerException npe) {
return true;
}
return false;
}

To know if a statement would return null, you would have to execute it first. Even if you somehow manage to execute the statement given in String to your method, it would be no different than just running it and checking whether the result is null, or catching the NullPointerException.

You are looking for something that will be part of Java 8 (or 9 or 10?) soon. Cf. http://viralpatel.net/blogs/null-safe-type-java-7-nullpointerexception/

With reflection it could be something like that:
nullCheck(objA, "getB", "getC", "getD" );
public static boolean nullCheck(Object obj, String... methods) {
Object o = obj;
Class<?> clazz = obj.getClass();
Method method = null;
try {
for( String name : methods ) {
method = clazz.getMethod( name, null );
o = method.invoke( o, null );
clazz = method.getReturnType();
}
} catch( NullPointerException e ) {
System.err.println(clazz.getSimpleName()+"(null)."+method.getName());
return false;
} catch( NoSuchMethodException e ) {
e.printStackTrace();
} catch( SecurityException e ) {
e.printStackTrace();
} catch( IllegalAccessException e ) {
e.printStackTrace();
} catch( IllegalArgumentException e ) {
e.printStackTrace();
} catch( InvocationTargetException e ) {
e.printStackTrace();
}
return true;
}
System.out.println(nullCheck(GraphicsEnvironment.getLocalGraphicsEnvironment(), "getDefaultScreenDevice", "getDisplayMode", "toString"));
System.out.println(nullCheck(GraphicsEnvironment.getLocalGraphicsEnvironment(), "getDefaultScreenDevice", "getFullScreenWindow", "doLayout"));
brings
true
false
'Window(null).doLayout'

you can achieve this by using reflection, here is your method:
public boolean dynamicNullCheck(Object o, String path) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
Object object= o;
for (String element : path.split("\\."))
{
Field field = object.getClass().getDeclaredField(element);
field.setAccessible(true);
Object fieldVal = field.get(object);
if (fieldVal!=null)
{
field.setAccessible(true);
object = fieldVal;
}
else
{
return true;
}
}
return false;
}
use it by giving your root element and path to each object, ie dynamicNullCheck(objA,"b.c.d")

Related

java 7: Get optional nested POJO property

I'm coming from a Swift background jumping back to Android and i'm used to using notation of this
let fooOptional = foo?.fooer?.fooest
print(fooOptional)
In java 8, this is possible:
Optional.of(new Foo())
.map(Foo::Fooer)
.map(Fooer::Fooest)
.ifPresent(System.out::println);
However, in java 7, there is no real out of the box way of doing this without resorting to later versions of Android, which does not work with our minimum SDK specs. Is there one?
If the pojos set up have getter calls to grab nested properties, you can use reflection to grab the optional get calls and walk down the nest:
public class NestedOptional<T> {
public static <T> Optional<T> fromNullable(Object obj, String... calls) {
if (obj == null) {
return Optional.absent();
}
for (String call: calls) {
try {
obj = obj.getClass().getMethod(call).invoke(obj);
if (obj == null) {
return Optional.absent();
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException("Couldn't call " + call + "() on " + obj, e);
}
}
return Optional.of(obj);
}
}
If there is a null pointer, it will return Guava's optional object thats available pre java 8.
With this, you can then:
Optional<Fooest> fooOptional = NestedOptional.fromNullable(foo, "getFooer", "getFooest");
if (fooOptional.isPresent()) {
System.out.println(fooOptional.get().toString());
}

Java exception handling: Missing return type

I'm relatively new to Java and I have run into an issue which I'm unable to figure a way to get around.
Here is my code:
// Ex2 is a child of Ex1
public T method(someType someArg) throws Ex1{
try{
someFunc() // Throws both Ex1 and Ex2
T ret = someOtherFunc() //Throws Ex1, cannot declare/initialize ret outside
return ret
} catch(Ex2){
//do something
} catch(Ex1){
//do something
}
}
Now java complains about missing return type whereas it DOES NOT complain when I remove Ex2 handling/catching.
Your method must return a T, you could add return null; as the last statement. Or, modify the visibility (and default value of) ret like
public T method(someType someArg) throws Ex1{
T ret = null;
try{
someFunc(); // Throws both Ex1 and Ex2
ret = someOtherFunc();
} catch(Ex2){
//do something
} catch(Ex1){
//do something
}
return ret;
}
If you are merely catching the exceptions and not throwing any exceptions after handling/logging them, then the method is handling the errors correctly but not returning anything (boom, compilation error).
If you are going to handle the exceptions yourself, you need to either return a null (or other reasonable invalid value) or re-throw the caught exception.
If you are going to rethrow an exception (other than ex1), make sure you add it to the method signature
For example:
// Ex2 is a child of Ex1
public T method(someType someArg) throws Ex1{
try{
someFunc() // Throws both Ex1 and Ex2
T ret = someOtherFunc() //Throws Ex1, cannot declare/initialize ret outside
return ret;
} catch(Ex2 ex2){
// either re-throw the exception (or return null instead)
throw ex2;
} catch(Ex1 ex1){
// or return a null value (you can re-throw the exception instead)
return null;
}
// you can also return null here if you want to catch/handle both exceptions separately but want to return null in either case
// return null;
}

How I can transfer atrributes values from a Objects to another

I want tranfers attributes values from a object that came from my Entity manager to a new object.
The return Object is always null
public class ReflectionUtil {
public static Object copyAttributesFromTo(Object a, Object b) throws IllegalArgumentException, IllegalAccessException {
Field[] fieldsFromFirstClass = a.getClass().getDeclaredFields();
Field[] fieldsFromSecondClass = b.getClass().getDeclaredFields();
for (Field currentFieldFromTheFirstClass : fieldsFromFirstClass) {
for (Field currentFieldFromTheSecondClass : fieldsFromSecondClass) {
String nameOfTheFirstField = currentFieldFromTheFirstClass.getName();
String nameOfTheSecondField = currentFieldFromTheSecondClass.getName();
if (!Modifier.isFinal(currentFieldFromTheFirstClass.getModifiers())) {//Dispensa os Final
if (!currentFieldFromTheFirstClass.isAnnotationPresent(Id.class)) {//Não sobescreve campo id
if (nameOfTheFirstField.equals(nameOfTheSecondField)) {
currentFieldFromTheFirstClass.setAccessible(true);
currentFieldFromTheSecondClass.setAccessible(true);
currentFieldFromTheSecondClass.get(b));
currentFieldFromTheFirstClass.set(a, currentFieldFromTheSecondClass.get(b));
}
}
}
}
}
return a;
}
}
In the Facade call I always have to put all the attribute values to new object
public void update(Profile object) {
dao.beginTransaction();
Profile persistedObject = dao.find(object.getId());
persistedObject.setName(object.getName());
dao.commitAndCloseTransaction();
}
So I think to create some like that
public void update(Profile object) {
dao.beginTransaction();
Profile persistedObject = dao.find(object.getId());
ReflectionUtil.copyAttributesFromTo(persistedObject , object);
dao.commitAndCloseTransaction();
}
really miss understood why u are using 2 loop? .. if the classes are same . u dont need to do it .. just do it in 1 loop .. and use fields get for obj which is holding data .. and use set for to set .. here is the more better way .. if the same object is required u can use generics .. and same object type will be required ( request return type )
public static <T> T copyAttributesFromTo(T value, T dataHolder) throws IllegalArgumentException, IllegalAccessException {
if (value == null || dataHolder == null) {
throw new IllegalArgumentException();
}
final Field[] fields = value.getClass().getDeclaredFields();
for (Field field : fields) {
if (!Modifier.isFinal(field.getModifiers())) {
field.setAccessible(true);
field.set(value, field.get(dataHolder));
}
}
return value;
}
The return Object is always null
It is impossible for the return object (i.e. whatever a contains when you return) to be null.
It is easy to see that the code does not change the reference a. There are no assignments to it in the method, so it cannot change.
The other possibility was that you called the method with a null value for a. But if you did that, the first line of the method calls a.getClass() and that will throw an NPE if a is null.
TL;DR - it is impossible.
So what does this mean?
Here are the most likely explanations:
You are mistaken that null is being returned. Perhaps the method is not being called? Perhaps, it is not returning?
Maybe you have misinterpreted the evidence in some other way. It is hard to know without seeing the code ... and the evidence.
Maybe you don't mean that the method is returning Object; i.e. I misunderstood the question. (Your problem description is pretty unambiguous though ...)
I make a change in the code and works to update for me
public static Object copyAttributesFromTo(Object a, Object b) throws IllegalArgumentException, IllegalAccessException {
Field[] fieldsFromFirstClass = a.getClass().getDeclaredFields();
Field[] fieldsFromSecondClass = b.getClass().getDeclaredFields();
//JSFMessageUtil.addMsgLog(JSFMessageUtil.matricula, ReflectionUtil.class.getCanonicalName(), "ReflectionUtil: Aqui");
for (Field currentFieldFromTheFirstClass : fieldsFromFirstClass) {
for (Field currentFieldFromTheSecondClass : fieldsFromSecondClass) {
Object nameOfTheFirstField = currentFieldFromTheFirstClass.getName();
Object nameOfTheSecondField = currentFieldFromTheSecondClass.getName();
if (!Modifier.isFinal(currentFieldFromTheFirstClass.getModifiers())) {//Dispensa os Final
//if (!currentFieldFromTheFirstClass.isAnnotationPresent(Id.class)) {//Não sobescreve campo id
if (nameOfTheFirstField.equals(nameOfTheSecondField)) {
currentFieldFromTheFirstClass.setAccessible(true);
currentFieldFromTheSecondClass.setAccessible(true);
//JSFMessageUtil.addMsgLog(JSFMessageUtil.matricula, ReflectionUtil.class.getCanonicalName(), "ReflectionUtil: " + currentFieldFromTheSecondClass.get(b));
currentFieldFromTheFirstClass.set(a, currentFieldFromTheSecondClass.get(b));
}
//}
}
}
}
return a;
}
The call
public void update(Aluno a) {
try {
Aluno aluno = new Aluno();//(Aluno) em.find(Aluno.class, a.getId());
em.getTransaction().begin();
//aluno.setNome(a.getNome());
ReflectionUtil.copyAttributesFromTo(aluno, a);
em.merge(aluno);
em.getTransaction().commit();
} catch (Exception e) {
System.out.println("Error " + e.getLocalizedMessage());
JOptionPane.showMessageDialog(null, e.getLocalizedMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
} finally {
em.close();
}
}

handling return from function where no return type is expected in some case in java

I had writtern a piece of code:In this code we are calling validitem function to check if passes param is some valid item or not.If it is valid item,i want to return that item else i want to abort processing stating its not valid item.Please suggest solution for the initial code writtern below, on how to handle situation where we dont want to return anything.And if we return null,then how and where to have exception catching?
public void constructdata(){
String foo = validitem(param);
//code to avoid processing if null is returned
}
public validitem(String item){
if(item.equals("Apple"){
return item;
}
if(item.equals("Ball"){
return item;}
return null;
}
If you want validitem() still to return null and if so, throw an exception:
String foo = validitem(param);
if ( foo != null ) {
container.putField(key, foo);
} else {
throw new Exception();
}
If you want to make validitem() only return a valid value and throw an exception otherwise:
public void constructdata(){
try {
container.putField(key, validitem(param));
} catch (Exception e) {
// Handle exception
}
}
public validitem(String item) throws Exception {
if (item.equals("Apple") {
return item;
}
if (item.equals("Ball") {
return item;
}
throw new Exception("Error message");
}
You should use a more specific exception than Exception(). I've only used it to demonstrate the logic.
Avoid returning null when possible because it can lead to bugs, insted you can return a Java8 Optional. If you are not using java8 there is an equivalent in the guava library. Returning an Optional forces the client to think about how to handle the case where nothing is returned.

Java global isEmpty() method

I have an application which uses code that produces various types of objects and data structures, returning them as Object instances, and would like a generic way of establishing whether any of those objects is "empty" (or null).
(This is not a matter of design, or of whether such a method should be used, but a question of optimizing the solution to an existing requirement.)
So, here is a simple go:
public static boolean isEmpty(Object content)
{
if (content == null)
{
return true;
}
else if (content instanceof CharSequence)
{
return (((CharSequence)content).length() == 0);
}
else if (content instanceof Collection<?>)
{
return ((Collection<?>)content).isEmpty();
}
else if (content instanceof Object[])
{
return (((Object[])content).length == 0);
}
else // Use reflection (an exaggeration, for demo purposes)
{
try
{
Method isEmpty = content.getClass().
getDeclaredMethod("isEmpty", (Class<?>[])null);
if (isEmpty != null)
{
Object result = isEmpty.invoke(content, (Object[])null);
if (result instanceof Boolean)
{
return (Boolean)result;
}
}
}
catch (Exception e)
{
}
}
return false;
}
Any ideas for potential improvements, in terms of either performance, or coverage?
For instance, reflection could be also used to establish whether the object has a length() or size() method, invoke it and see if the result is 0. (In reality, reflection is probably too much, but I am including it here for completeness.)
Is there a top-level class very commonly used, which has a length() or size() method, instead of the isEmpty() method, to include in the above case, similarly to Collection that has isEmpty()?
Instead of the ugly instanceofs, split up the method into several methods with the same name but different args. e.g.
static boolean isEmpty(Object[] array)
static boolean isEmpty(Collection collection)
static boolean isEmpty(CharSequence cs)
Instead of reflection, if you really want your own interface for special objects, declare that interface, and then, for consistency with the above, offer the static utility
static boolean isEmpty(IMayBeEmpty imbe);
This method would at least solve your problem of the generic isEmpty(Object) problem. However, you don't get compile time safety with this, and calling it without the method existing for the exact type requested will yield a runtime error. Note the "MethodUtils" class is from apache commons-beanutils, though you could easily use reflection directly, but for the sake of simplicity, i'm using beanutils here.
The "invokeExactcMethod" method looks for a static method in the given class with the given name that has the compatible parameters of the object array being passed. So if the runtime type of the object is ArrayList, it would look for isEmpty(ArrayList) then isEmpty(AbstractList) then isEmpty(List). It then invokes that method if it can find it, otherwise it throws a NoSuchMethodException.
public class MyUtility {
static boolean isEmpty(Object object) {
if (object == null) {
return true;
}
else {
try {
return MethodUtils.invokeStaticMethod(
MyUtility.class, "isEmpty", new Object[]{object});
}
catch (NoSuchMethodException e) {
throw new IllegalArgumentException(e);
}
catch (IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}
}
the "invokeExactStaticMethod" is more deterministic and doesn't use assignment compatibility, but exact signature matching. That means isEmpty(List) would never match anything because you can't construct anything of that type.

Categories