問題描述
我有一個 java.lang.reflect.InvocationHandler
我需要實現方法 invoke()
I have a java.lang.reflect.InvocationHandler
and I need to implement the method invoke()
我的詳細說明中有一個 java.lang.String
類型的值,我需要將此值轉換為該方法期望的適當 returnType(它可以是一個原始類型,如 int、boolean、double 或包裝類,如 Boolean、Integer、Double、Float 等).
I have a value of type java.lang.String
from my elaboration and I need to convert this value to the appropriate returnType expected by the method (it can be a primitive like int, boolean, double or wrapper classes like Boolean, Integer, Double, Float, etc).
例子:
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String computedValue = compute(...);
return convert(method.getReturnType(), computedValue);
}
private Object convert(Class<?> returnType, String stringValue) {
return ...; // what's the simplest way?
}
我不希望簡單地實現復雜對象之間的自動轉換,但我希望有一種簡單的方法來從 String 轉換為標準 java 類型.
I am not expecting to simply implement an automatic conversion between complex objects, but I expect a simple way to convert from String to the standard java types.
我已經(太)見過很多次這樣的東西,但它似乎不適合我:
I've seen (too) many times stuff like this, but it doesn't seem appropriate to me:
public static Object toObject( Class clazz, String value ) {
if( Boolean.class.isAssignableFrom( clazz ) ) return Boolean.parseBoolean( value );
if( Byte.class.isAssignableFrom( clazz ) ) return Byte.parseByte( value );
if( Short.class.isAssignableFrom( clazz ) ) return Short.parseShort( value );
if( Integer.class.isAssignableFrom( clazz ) ) return Integer.parseInteger( value );
if( Long.class.isAssignableFrom( clazz ) ) return Long.parseLong( value );
if( Float.class.isAssignableFrom( clazz ) ) return Float.parseFloat( value );
if( Double.class.isAssignableFrom( clazz ) ) return Double.parseDouble( value );
return value;
}
到目前為止,以上還不是我看到的最糟糕的一個:)
and the above is not even the worse one I saw, so far :)
這里有什么秘訣嗎?
推薦答案
據我所知,您提供的版本沒有真正的替代方案.您可以稍微簡化一下(因為包裝器類型都是 final
),但您本質上需要使用 if
或 switch
或散列來切換在課堂上.
As far as I'm aware, there is no real alternative to the version you presented. You can simplify it a bit (since the wrapper types are all final
), but you essentially need to use if
or switch
or hashing to switch on the class.
我的建議是像上面那樣編寫代碼.丑陋的代碼本身只是一個問題,如果你不得不看的話.所以把它放在一個實用方法中,不要再看它了.
My advice is to code it like the above. Ugly code is only a problem per se if you have to look at it. So put it inside a utility method and don't look at it again.
FWIW - 這是我簡化方法的方式:
FWIW - this is how I'd simplify the method:
public static Object toObject( Class clazz, String value ) {
if( Boolean.class == clazz ) return Boolean.parseBoolean( value );
if( Byte.class == clazz ) return Byte.parseByte( value );
if( Short.class == clazz ) return Short.parseShort( value );
if( Integer.class == clazz ) return Integer.parseInt( value );
if( Long.class == clazz ) return Long.parseLong( value );
if( Float.class == clazz ) return Float.parseFloat( value );
if( Double.class == clazz ) return Double.parseDouble( value );
return value;
}
這樣更簡單、更高效.并且它等同于原始版本,因為類都是 final
并且因為規范聲明 Class
對象的相等性是對象身份.
This is simpler and more efficient. And it is equivalent to the original version because the classes are all final
and because the specs state that equality for Class
objects is object identity.
可以說,我們應該使用直接返回包裝器對象的 <wrapper>.valueOf(String)
方法.
Arguably, we should be using the <wrapper>.valueOf(String)
methods which return the wrapper objects directly.
我并沒有聲稱這不那么丑……但是美"并不是衡量代碼質量的有用指標,因為它是主觀的,因為它不能告訴您代碼是否易于理解和/或維護.
I make no claim that this is less ugly ... but "beauty" is not a useful measure of code quality, because it is subjective and because it doesn't tell you whether the code is easy to understand and / or maintain.
更新
為了也支持原始類型,將相應的類添加到 if
條件中;例如
To support primitive types as well, add the corresponding classes to the if
conditions; e.g.
if (Boolean.class == clazz || Boolean.TYPE == clazz) {
return Boolean.parseBoolean(value);
}
現在可能已經到了在類型名稱上進行 String 切換更有效的地步,盡管需要考慮一些稍微棘手的類型標識問題.(理論上,您可以擁有由不同類加載器加載的具有相同全名的多個類型.我認為您需要在類加載器中快速而松散地"使用原始包裝類來做到這一點......但是我認為這仍然是可能的.)
It may now be getting to the point where doing a String switch on the type's name is more efficient, though there are some slightly knotty issues of type identity that need to be thought through. (In theory, you can have multiple types with the same full name that have been loaded by different classloaders. I think you'd need to "play fast and loose" in a classloader to do that with the primitive wrapper classes ... but I think it might still be possible.)
這篇關于如何從 String 轉換為原始類型或標準 java Wrapper 類型的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!