GigaSpaceNullValueExtractor.java

package org.microspace.specific.gigaspace;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.microspace.specific.NullValueExtractor;

/**
 * Null value extractor for GigaSpace classes.
 * 
 * @author Gaspar Sinai - {@literal gaspar.sinai@microspace.org}
 * @version 2016-06-26
 */
public class GigaSpaceNullValueExtractor implements NullValueExtractor {
	
	Class<? extends Annotation> spacePropertyClass;
	
	public GigaSpaceNullValueExtractor(Class<? extends Annotation> spacePropertyClass) {
		this.spacePropertyClass = spacePropertyClass;
	}

	public String getNullValue(Method getMethod) {
		Annotation spaceProperty = getMethod.getAnnotation(spacePropertyClass);
		if (spaceProperty == null) return null;
		
		for (Method m : spacePropertyClass.getMethods()) {
			if (m.getName().equals ("nullValue")) {
				if (!String.class.equals (m.getReturnType())) {
					throw new IllegalArgumentException ("Expected SpaceProperty nullValue String return type.");
				}
				Object ret = null;
				try {
					ret = m.invoke (spaceProperty);
				} catch (Exception ex) {
					throw new IllegalArgumentException ("SpaceProperty.nullValue getter does not work.", ex);
				}
				if ("".equals(ret)) return null;
				return (String) ret;
			}
		}
		return null;
	}
}