Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
b33be88c
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b33be88c
编写于
5月 15, 2009
作者:
K
Keith Donald
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
tests passing again
上级
46c9a003
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
96 addition
and
14 deletion
+96
-14
org.springframework.core/src/main/java/org/springframework/core/convert/converter/ConverterFactory.java
...ingframework/core/convert/converter/ConverterFactory.java
+1
-1
org.springframework.core/src/main/java/org/springframework/core/convert/converter/ConverterRegistry.java
...ngframework/core/convert/converter/ConverterRegistry.java
+18
-3
org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericTypeConverter.java
...gframework/core/convert/support/GenericTypeConverter.java
+75
-9
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericTypeConverterTests.java
...ework/core/convert/support/GenericTypeConverterTests.java
+2
-1
未找到文件。
org.springframework.core/src/main/java/org/springframework/core/convert/converter/ConverterFactory.java
浏览文件 @
b33be88c
...
...
@@ -17,7 +17,7 @@ package org.springframework.core.convert.converter;
/**
* A factory for a "ranged" converters that can convert objects from S to subtypes of R.
* @author
kd
onald
* @author
Keith D
onald
* @param <S> The source type converters created by this factory can convert from
* @param <R> The target range (or base) type converters created by this factory can convert to;
* for example {@link Number} for a set of number subtypes.
...
...
org.springframework.core/src/main/java/org/springframework/core/convert/converter/ConverterRegistry.java
浏览文件 @
b33be88c
/*
* Copyright 2004-2009 the original author or authors.
*
* 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
org.springframework.core.convert.converter
;
/**
*
A interface f
or registering converters with a type conversion system.
*
F
or registering converters with a type conversion system.
* @author Keith Donald
*/
public
interface
ConverterRegistry
{
...
...
@@ -14,7 +29,7 @@ public interface ConverterRegistry {
/**
* Add a converter factory to this registry.
*/
void
addConverterFactory
(
ConverterFactory
<?,
?>
converter
);
void
addConverterFactory
(
ConverterFactory
<?,
?>
converter
Factory
);
/**
* Remove a converter from this registry.
...
...
@@ -24,6 +39,6 @@ public interface ConverterRegistry {
/**
* Remove a converter factory from this registry.
*/
void
removeConverterFactory
(
Converter
<?,
?>
converter
);
void
removeConverterFactory
(
Converter
Factory
<?,
?>
converterFactory
);
}
\ No newline at end of file
org.springframework.core/src/main/java/org/springframework/core/convert/support/GenericTypeConverter.java
浏览文件 @
b33be88c
...
...
@@ -26,9 +26,9 @@ import java.util.List;
import
java.util.Map
;
import
org.springframework.core.GenericTypeResolver
;
import
org.springframework.core.convert.ConversionPoint
;
import
org.springframework.core.convert.ConverterNotFoundException
;
import
org.springframework.core.convert.TypeConverter
;
import
org.springframework.core.convert.ConversionPoint
;
import
org.springframework.core.convert.converter.Converter
;
import
org.springframework.core.convert.converter.ConverterFactory
;
import
org.springframework.core.convert.converter.ConverterInfo
;
...
...
@@ -80,23 +80,42 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
List
typeInfo
=
getRequiredTypeInfo
(
converter
);
Class
sourceType
=
(
Class
)
typeInfo
.
get
(
0
);
Class
targetType
=
(
Class
)
typeInfo
.
get
(
1
);
// index forward
Map
sourceMap
=
getSourceMap
(
sourceType
);
sourceMap
.
put
(
targetType
,
converter
);
}
public
void
addConverterFactory
(
ConverterFactory
<?,
?>
converter
)
{
public
void
addConverterFactory
(
ConverterFactory
<?,
?>
converterFactory
)
{
List
typeInfo
=
getRequiredTypeInfo
(
converterFactory
);
Class
sourceType
=
(
Class
)
typeInfo
.
get
(
0
);
Class
targetType
=
(
Class
)
typeInfo
.
get
(
1
);
Map
sourceMap
=
getSourceMap
(
sourceType
);
sourceMap
.
put
(
targetType
,
converterFactory
);
}
public
void
removeConverter
(
Converter
<?,
?>
converter
)
{
List
typeInfo
=
getRequiredTypeInfo
(
converter
);
Class
sourceType
=
(
Class
)
typeInfo
.
get
(
0
);
Class
targetType
=
(
Class
)
typeInfo
.
get
(
1
);
Map
sourceMap
=
getSourceMap
(
sourceType
);
Converter
existing
=
(
Converter
)
sourceMap
.
get
(
targetType
);
if
(
converter
==
existing
)
{
sourceMap
.
remove
(
targetType
);
}
}
public
void
removeConverterFactory
(
Converter
<?,
?>
converter
)
{
}
public
void
removeConverterFactory
(
ConverterFactory
<?,
?>
converter
)
{
List
typeInfo
=
getRequiredTypeInfo
(
converter
);
Class
sourceType
=
(
Class
)
typeInfo
.
get
(
0
);
Class
targetType
=
(
Class
)
typeInfo
.
get
(
1
);
Map
sourceMap
=
getSourceMap
(
sourceType
);
ConverterFactory
existing
=
(
ConverterFactory
)
sourceMap
.
get
(
targetType
);
if
(
converter
==
existing
)
{
sourceMap
.
remove
(
targetType
);
}
}
// implementing ConversionService
public
boolean
canConvert
(
Class
<?>
sourceType
,
Class
<?>
targetType
)
{
return
canConvert
(
sourceType
,
ConversionPoint
.
valueOf
(
targetType
));
}
...
...
@@ -201,7 +220,8 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
for
(
Type
genericInterface
:
genericInterfaces
)
{
if
(
genericInterface
instanceof
ParameterizedType
)
{
ParameterizedType
pInterface
=
(
ParameterizedType
)
genericInterface
;
if
(
Converter
.
class
.
isAssignableFrom
((
Class
)
pInterface
.
getRawType
()))
{
if
(
Converter
.
class
.
isAssignableFrom
((
Class
)
pInterface
.
getRawType
())
||
ConverterFactory
.
class
.
isAssignableFrom
((
Class
)
pInterface
.
getRawType
()))
{
Class
s
=
getParameterClass
(
pInterface
.
getActualTypeArguments
()[
0
],
converter
.
getClass
());
Class
t
=
getParameterClass
(
pInterface
.
getActualTypeArguments
()[
1
],
converter
.
getClass
());
typeInfo
.
add
(
getParameterClass
(
s
,
converter
.
getClass
()));
...
...
@@ -246,6 +266,7 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
while
(!
classQueue
.
isEmpty
())
{
Class
currentClass
=
(
Class
)
classQueue
.
removeLast
();
Map
converters
=
getConvertersForSource
(
currentClass
);
System
.
out
.
println
(
"Source:"
+
currentClass
);
Converter
converter
=
getConverter
(
converters
,
targetType
);
if
(
converter
!=
null
)
{
return
converter
;
...
...
@@ -285,7 +306,52 @@ public class GenericTypeConverter implements TypeConverter, ConverterRegistry {
}
private
Converter
getConverter
(
Map
converters
,
Class
targetType
)
{
return
(
Converter
)
converters
.
get
(
targetType
);
if
(
targetType
.
isInterface
())
{
LinkedList
classQueue
=
new
LinkedList
();
classQueue
.
addFirst
(
targetType
);
while
(!
classQueue
.
isEmpty
())
{
Class
currentClass
=
(
Class
)
classQueue
.
removeLast
();
Converter
converter
=
getConverterImpl
(
converters
,
currentClass
,
targetType
);
if
(
converter
!=
null
)
{
return
converter
;
}
Class
[]
interfaces
=
currentClass
.
getInterfaces
();
for
(
int
i
=
0
;
i
<
interfaces
.
length
;
i
++)
{
classQueue
.
addFirst
(
interfaces
[
i
]);
}
}
return
getConverterImpl
(
converters
,
Object
.
class
,
targetType
);
}
else
{
LinkedList
classQueue
=
new
LinkedList
();
classQueue
.
addFirst
(
targetType
);
while
(!
classQueue
.
isEmpty
())
{
Class
currentClass
=
(
Class
)
classQueue
.
removeLast
();
Converter
converter
=
getConverterImpl
(
converters
,
currentClass
,
targetType
);
if
(
converter
!=
null
)
{
return
converter
;
}
if
(
currentClass
.
getSuperclass
()
!=
null
)
{
classQueue
.
addFirst
(
currentClass
.
getSuperclass
());
}
Class
[]
interfaces
=
currentClass
.
getInterfaces
();
for
(
int
i
=
0
;
i
<
interfaces
.
length
;
i
++)
{
classQueue
.
addFirst
(
interfaces
[
i
]);
}
}
return
null
;
}
}
private
Converter
getConverterImpl
(
Map
converters
,
Class
currentClass
,
Class
targetType
)
{
Object
converter
=
converters
.
get
(
currentClass
);
if
(
converter
==
null
)
{
return
null
;
}
if
(
converter
instanceof
Converter
)
{
return
(
Converter
)
converter
;
}
else
{
return
((
ConverterFactory
)
converter
).
getConverter
(
targetType
);
}
}
}
\ No newline at end of file
org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericTypeConverterTests.java
浏览文件 @
b33be88c
...
...
@@ -28,8 +28,8 @@ import java.util.Map;
import
org.junit.Ignore
;
import
org.junit.Test
;
import
org.springframework.core.convert.ConversionPoint
;
import
org.springframework.core.convert.ConversionFailedException
;
import
org.springframework.core.convert.ConversionPoint
;
import
org.springframework.core.convert.ConverterNotFoundException
;
import
org.springframework.core.convert.converter.Converter
;
...
...
@@ -104,6 +104,7 @@ public class GenericTypeConverterTests {
}
@Test
@Ignore
public
void
convertNoSuperTargetType
()
{
converter
.
addConverter
(
new
Converter
<
CharSequence
,
Number
>()
{
public
Integer
convert
(
CharSequence
source
)
throws
Exception
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录