Update of /cvsroot/xdoclet/xdoclet2/src/java/xdoclet/util/predicates
In directory sc8-pr-cvs1:/tmp/cvs-serv10623/src/java/xdoclet/util/predicates

Added Files:
        ReflectionPredicate.java 
Log Message:
New

--- NEW FILE: ReflectionPredicate.java ---
package xdoclet.util.predicates;

import xjavadoc.predicates.ProgramElementPredicate;
import xjavadoc.XProgramElement;

import java.lang.reflect.Method;

/**
 * Predicate that uses reflection to call a no-argument method that returns a boolean.
 * This is so we don't have to write one Predicate for all the different
 * isXxx() methods in the XJavadoc API.
 *
 * @author <a href="mailto:aslak.hellesoy at bekk.no">Aslak Helles&oslash;y</a>
 * @version $Revision: 1.1 $
 */
public class ReflectionPredicate extends ProgramElementPredicate {
    private String _methodName;
    private boolean _negation;

    protected boolean evaluate(XProgramElement programElement) {
        // Find the method. We only support methods with no arguments that return a 
boolean.
        Method method = null;
        try {
            method = programElement.getClass().getMethod( _methodName, null );
        } catch (Exception e) {
            throw new IllegalStateException(_methodName + " should be a no-arg 
method");
        }
        Class returnType = method.getReturnType();
        if( returnType != Boolean.TYPE ) {
            throw new IllegalStateException(_methodName + " should return boolean.");
        }
        Boolean bool = null;
        try {
            bool = (Boolean) method.invoke(programElement, null);
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalStateException("Failed to invoke " + _methodName);
        }
        boolean result = bool.booleanValue();
        result = _negation ? !result : result;
        return result;
    }

    public ReflectionPredicate() {
    }

    public ReflectionPredicate(String method) {
        setMethod(method);
    }

    public void setMethod(String method) {
        if( method.charAt(0) == '!' ) {
            _negation = true;
            _methodName = method.substring(1);
        } else {
            _negation = false;
            _methodName = method;
        }
    }
}



-------------------------------------------------------
This SF.net email is sponsored by: Etnus, makers of TotalView, The debugger 
for complex code. Debugging C/C++ programs can leave you feeling lost and 
disoriented. TotalView can help you find your way. Available on major UNIX 
and Linux platforms. Try it free. www.etnus.com
_______________________________________________
xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel

Reply via email to