View Javadoc

1   package net.sourceforge.jparam.conversion.converters;
2   
3   import java.lang.reflect.Method;
4   import java.lang.reflect.Modifier;
5   
6   import net.sourceforge.jparam.conversion.creators.ObjectCreator;
7   import net.sourceforge.jparam.conversion.weights.ScalarConversionWeight;
8   import net.sourceforge.jparam.util.Utils;
9   
10  public class MethodConverter extends AbstractConverter {
11  
12  	Method conversionMethod;
13  
14  	public MethodConverter(Method conversionMethod) {
15  		String validationError = getMethodValidError(conversionMethod);
16  		Utils.assert(validationError == null, validationError);
17  
18  		this.conversionMethod = conversionMethod;
19  
20  		if (conversionMethod.getParameterTypes().length != 1) {
21  			throw new RuntimeException(
22  					"Method must have exactly one argument: "
23  							+ conversionMethod);
24  		}
25  
26  		if (!Modifier.isStatic(conversionMethod.getModifiers())) {
27  			throw new RuntimeException("Method must be static: "
28  					+ conversionMethod);
29  		}
30  
31  		sourceClass = ObjectCreator.getInstance().getNonPrimitiveClass(
32  				conversionMethod.getParameterTypes()[0]);
33  		targetClass = ObjectCreator.getInstance().getNonPrimitiveClass(
34  				conversionMethod.getReturnType());
35  
36  		String methodName = conversionMethod.getName().toLowerCase();
37  
38  		if (methodName.startsWith("convert")
39  				|| methodName.startsWith("user_convert")) {
40  			conversionWeight = ScalarConversionWeight.CONV_USER;
41  		} else if (methodName.startsWith("std_convert")) {
42  			conversionWeight = ScalarConversionWeight.CONV_STANDARD;
43  		} else if (methodName.startsWith("promote_convert")) {
44  			conversionWeight = ScalarConversionWeight.CONV_PROMOTE;
45  		} else {
46  			throw new RuntimeException("Method name is not for conversion: "
47  					+ conversionMethod.getName());
48  		}
49  	}
50  
51  	protected Object internalConvert(Object o) throws Exception {
52  		return conversionMethod.invoke(null, new Object[] { o });
53  	}
54  
55  	private static String[] SUPPORTED_PREFIX = new String[] { "convert",
56  			"user_convert", "std_convert", "promote_convert" };
57  
58  	public static String getMethodValidError(Method m) {
59  		if (m.getParameterTypes().length != 1)
60  			return "Converter method must have exactly one argument, method: "
61  					+ m;
62  		if (!Modifier.isStatic(m.getModifiers()))
63  			return "Converter method must be static, method: " + m;
64  		if (!Modifier.isPublic(m.getModifiers()))
65  			return "Converter method must be public, method: " + m;
66  		if (m.getReturnType().getClass().equals(void.class))
67  			return "Converter method must have a return type, method: " + m;
68  
69  		String methodName = m.getName();
70  		for (int i = 0; i < SUPPORTED_PREFIX.length; i++) {
71  			if (methodName.startsWith(SUPPORTED_PREFIX[i]))
72  				return null;
73  		}
74  		return "Converter method name is not for conversion, method: " + m;
75  	}
76  
77  	public static boolean isMethodValid(Method m) {
78  		return getMethodValidError(m) == null;
79  	}
80  
81  	protected boolean converterEquals(IConverter o) {
82  		MethodConverter other = (MethodConverter) o;
83  		return conversionMethod.equals(other.conversionMethod);
84  	}
85  
86  	public IConverter update(IConverter updatedConverter) {
87  	    // ignore updatedConverter
88  	    if (updatedConverter == null){
89  	    }
90  		// this converter cannot change due to registration of new converters...
91  		return this;
92  	}
93  }