Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wd1105040417
retrofit
提交
f3c97d61
R
retrofit
项目概览
wd1105040417
/
retrofit
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
retrofit
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
f3c97d61
编写于
7月 23, 2019
作者:
J
Jesse Wilson
提交者:
GitHub
7月 23, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3145 from square/jakew/extend/2019-07-03
Allow service interfaces to extend other interfaces
上级
98899549
948a0db7
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
70 addition
and
24 deletion
+70
-24
retrofit/src/main/java/retrofit2/Retrofit.java
retrofit/src/main/java/retrofit2/Retrofit.java
+31
-9
retrofit/src/main/java/retrofit2/Utils.java
retrofit/src/main/java/retrofit2/Utils.java
+0
-12
retrofit/src/test/java/retrofit2/RetrofitTest.java
retrofit/src/test/java/retrofit2/RetrofitTest.java
+39
-3
未找到文件。
retrofit/src/main/java/retrofit2/Retrofit.java
浏览文件 @
f3c97d61
...
...
@@ -22,7 +22,10 @@ import java.lang.reflect.Modifier;
import
java.lang.reflect.Proxy
;
import
java.lang.reflect.Type
;
import
java.net.URL
;
import
java.util.ArrayDeque
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.Deque
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Objects
;
...
...
@@ -128,10 +131,7 @@ public final class Retrofit {
*/
@SuppressWarnings
(
"unchecked"
)
// Single-interface proxy creation guarded by parameter safety.
public
<
T
>
T
create
(
final
Class
<
T
>
service
)
{
Utils
.
validateServiceInterface
(
service
);
if
(
validateEagerly
)
{
eagerlyValidateMethods
(
service
);
}
validateServiceInterface
(
service
);
return
(
T
)
Proxy
.
newProxyInstance
(
service
.
getClassLoader
(),
new
Class
<?>[]
{
service
},
new
InvocationHandler
()
{
private
final
Platform
platform
=
Platform
.
get
();
...
...
@@ -151,11 +151,33 @@ public final class Retrofit {
});
}
private
void
eagerlyValidateMethods
(
Class
<?>
service
)
{
Platform
platform
=
Platform
.
get
();
for
(
Method
method
:
service
.
getDeclaredMethods
())
{
if
(!
platform
.
isDefaultMethod
(
method
)
&&
!
Modifier
.
isStatic
(
method
.
getModifiers
()))
{
loadServiceMethod
(
method
);
private
void
validateServiceInterface
(
Class
<?>
service
)
{
if
(!
service
.
isInterface
())
{
throw
new
IllegalArgumentException
(
"API declarations must be interfaces."
);
}
Deque
<
Class
<?>>
check
=
new
ArrayDeque
<>(
1
);
check
.
add
(
service
);
while
(!
check
.
isEmpty
())
{
Class
<?>
candidate
=
check
.
removeFirst
();
if
(
candidate
.
getTypeParameters
().
length
!=
0
)
{
StringBuilder
message
=
new
StringBuilder
(
"Type parameters are unsupported on "
)
.
append
(
candidate
.
getName
());
if
(
candidate
!=
service
)
{
message
.
append
(
" which is an interface of "
)
.
append
(
service
.
getName
());
}
throw
new
IllegalArgumentException
(
message
.
toString
());
}
Collections
.
addAll
(
check
,
candidate
.
getInterfaces
());
}
if
(
validateEagerly
)
{
Platform
platform
=
Platform
.
get
();
for
(
Method
method
:
service
.
getDeclaredMethods
())
{
if
(!
platform
.
isDefaultMethod
(
method
)
&&
!
Modifier
.
isStatic
(
method
.
getModifiers
()))
{
loadServiceMethod
(
method
);
}
}
}
}
...
...
retrofit/src/main/java/retrofit2/Utils.java
浏览文件 @
f3c97d61
...
...
@@ -317,18 +317,6 @@ final class Utils {
return
ResponseBody
.
create
(
body
.
contentType
(),
body
.
contentLength
(),
buffer
);
}
static
<
T
>
void
validateServiceInterface
(
Class
<
T
>
service
)
{
if
(!
service
.
isInterface
())
{
throw
new
IllegalArgumentException
(
"API declarations must be interfaces."
);
}
// Prevent API interfaces from extending other interfaces. This not only avoids a bug in
// Android (http://b.android.com/58753) but it forces composition of API declarations which is
// the recommended pattern.
if
(
service
.
getInterfaces
().
length
>
0
)
{
throw
new
IllegalArgumentException
(
"API interfaces must not extend other interfaces."
);
}
}
static
Type
getParameterUpperBound
(
int
index
,
ParameterizedType
type
)
{
Type
[]
types
=
type
.
getActualTypeArguments
();
if
(
index
<
0
||
index
>=
types
.
length
)
{
...
...
retrofit/src/test/java/retrofit2/RetrofitTest.java
浏览文件 @
f3c97d61
...
...
@@ -85,6 +85,10 @@ public final class RetrofitTest {
}
interface
Extending
extends
CallMethod
{
}
interface
TypeParam
<
T
>
{
}
interface
ExtendingTypeParam
extends
TypeParam
<
String
>
{
}
interface
StringService
{
@GET
(
"/"
)
String
get
();
}
...
...
@@ -129,15 +133,47 @@ public final class RetrofitTest {
assertThat
(
example
.
toString
()).
isNotEmpty
();
}
@Test
public
void
interfaceWithExtendIsNotSupported
()
{
@Test
public
void
interfaceWithTypeParameterThrows
()
{
Retrofit
retrofit
=
new
Retrofit
.
Builder
()
.
baseUrl
(
server
.
url
(
"/"
))
.
build
();
server
.
enqueue
(
new
MockResponse
().
setBody
(
"Hi"
));
try
{
retrofit
.
create
(
TypeParam
.
class
);
fail
();
}
catch
(
IllegalArgumentException
e
)
{
assertThat
(
e
).
hasMessage
(
"Type parameters are unsupported on retrofit2.RetrofitTest$TypeParam"
);
}
}
@Test
public
void
interfaceWithExtend
()
throws
IOException
{
Retrofit
retrofit
=
new
Retrofit
.
Builder
()
.
baseUrl
(
server
.
url
(
"/"
))
.
build
();
server
.
enqueue
(
new
MockResponse
().
setBody
(
"Hi"
));
Extending
extending
=
retrofit
.
create
(
Extending
.
class
);
String
result
=
extending
.
getResponseBody
().
execute
().
body
().
string
();
assertEquals
(
"Hi"
,
result
);
}
@Test
public
void
interfaceWithExtendWithTypeParameterThrows
()
{
Retrofit
retrofit
=
new
Retrofit
.
Builder
()
.
baseUrl
(
server
.
url
(
"/"
))
.
build
();
server
.
enqueue
(
new
MockResponse
().
setBody
(
"Hi"
));
try
{
retrofit
.
create
(
Extending
.
class
);
retrofit
.
create
(
Extending
TypeParam
.
class
);
fail
();
}
catch
(
IllegalArgumentException
e
)
{
assertThat
(
e
).
hasMessage
(
"API interfaces must not extend other interfaces."
);
assertThat
(
e
).
hasMessage
(
"Type parameters are unsupported on retrofit2.RetrofitTest$TypeParam "
+
"which is an interface of retrofit2.RetrofitTest$ExtendingTypeParam"
);
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录