All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.viaoa.util.OAFindNull Maven / Gradle / Ivy

The newest version!
/*  Copyright 1999 Vince Via [email protected]
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/
package com.viaoa.util;

import java.lang.reflect.*;
import java.util.IdentityHashMap;

/**
 * Search object and it's fields/references for a null value.
 * @author vvia
 */
public class OAFindNull {
    private IdentityHashMap hm = new IdentityHashMap();
    /**
     * Find a null value for an object or any of it's fields/references. 
     * @param obj
     * @return true if a null was found, the foundOne(..) will be called.
     */
    public boolean findNull(Object obj) throws IllegalAccessException {
        hm.clear();
        String s = obj == null ? "" : obj.getClass().getName();
        int x = s.lastIndexOf('.');
        if (x > 0) s = s.substring(x+1);
        return _findNull(s, obj);
    }    

    private boolean _findNull(String propertyPath, Object obj) throws IllegalAccessException {
        if (obj == null) {
            foundOne(propertyPath);
            return true;
        }
        if (hm.get(obj) != null) return false;
        hm.put(obj, obj);

        if (obj instanceof String) {
            return false;
        }
        
        if (obj.getClass().isArray()) {
            int x = Array.getLength(obj);

            boolean bMatch = true;
            for (int i=0; i= 0) continue;
            if (Modifier.isStatic(field.getModifiers())) continue;
            if (Modifier.isTransient(field.getModifiers())) continue;

            Object ox = field.get(obj);
            if (_findNull(propertyPath+"."+field.getName(), ox)) bResult = true;
        }
        return bResult;
    }
    
    /**
     * @return true to continue, false to stop
     */
    public boolean foundOne(String propertyPath) {
        System.out.println(propertyPath);
        return true;
    }
    
    public static void main(String[] args) throws Exception {
        //Object obj1 = "test";
        //Object obj2 = null;
        /*
        TRIOrder obj1 = new TRIOrder();
        TRIOrder obj2 = new TRIOrder();
        obj1.additionalQuantities = new double[0];
        obj2.additionalQuantities = new double[1];
        
        IFindNull oc = new IFindNull() {
            @Override
            public boolean foundOne(String propertyPath) {
                return super.foundOne(propertyPath);
            }
        };
        oc.findNull(obj1);
        */
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy