Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FIY695
jenkins
提交
89a0050c
J
jenkins
项目概览
FIY695
/
jenkins
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jenkins
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
89a0050c
编写于
8月 18, 2015
作者:
A
Antonio Muñiz
提交者:
Antonio Muñiz
10月 05, 2015
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[JENKINS-30002] Improve Util.isOverridden
上级
94d80ea4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
113 addition
and
5 deletion
+113
-5
core/src/main/java/hudson/Util.java
core/src/main/java/hudson/Util.java
+33
-5
core/src/test/java/hudson/util/IsOverriddenTest.java
core/src/test/java/hudson/util/IsOverriddenTest.java
+80
-0
未找到文件。
core/src/main/java/hudson/Util.java
浏览文件 @
89a0050c
...
...
@@ -26,6 +26,7 @@ package hudson;
import
com.sun.jna.Memory
;
import
com.sun.jna.Native
;
import
com.sun.jna.NativeLong
;
import
edu.umd.cs.findbugs.annotations.SuppressWarnings
;
import
hudson.Proc.LocalProc
;
import
hudson.model.TaskListener
;
...
...
@@ -33,6 +34,7 @@ import hudson.os.PosixAPI;
import
hudson.util.QuotedStringTokenizer
;
import
hudson.util.VariableResolver
;
import
hudson.util.jna.WinIOException
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.lang.time.FastDateFormat
;
import
org.apache.tools.ant.BuildException
;
...
...
@@ -40,14 +42,18 @@ import org.apache.tools.ant.Project;
import
org.apache.tools.ant.taskdefs.Chmod
;
import
org.apache.tools.ant.taskdefs.Copy
;
import
org.apache.tools.ant.types.FileSet
;
import
jnr.posix.FileStat
;
import
jnr.posix.POSIX
;
import
javax.crypto.SecretKey
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.io.*
;
import
java.lang.reflect.Array
;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Modifier
;
import
java.net.InetAddress
;
import
java.net.URI
;
import
java.net.URISyntaxException
;
...
...
@@ -70,12 +76,14 @@ import java.util.regex.Matcher;
import
java.util.regex.Pattern
;
import
hudson.util.jna.Kernel32Utils
;
import
static
hudson
.
util
.
jna
.
GNUCLibrary
.
LIBC
;
import
java.security.DigestInputStream
;
import
javax.annotation.CheckForNull
;
import
javax.annotation.Nonnull
;
import
javax.annotation.Nullable
;
import
org.apache.commons.codec.digest.DigestUtils
;
/**
...
...
@@ -1415,16 +1423,36 @@ public class Util {
}
/**
* Checks if the
public
method defined on the base type with the given arguments
*
are
overridden in the given derived type.
* Checks if the method defined on the base type with the given arguments
*
is
overridden in the given derived type.
*/
public
static
boolean
isOverridden
(
@Nonnull
Class
base
,
@Nonnull
Class
derived
,
@Nonnull
String
methodName
,
@Nonnull
Class
...
types
)
{
return
!
getMethod
(
base
,
methodName
,
types
).
equals
(
getMethod
(
derived
,
methodName
,
types
));
}
private
static
Method
getMethod
(
@Nonnull
Class
clazz
,
@Nonnull
String
methodName
,
@Nonnull
Class
...
types
)
{
Method
res
=
null
;
try
{
return
!
base
.
getMethod
(
methodName
,
types
).
equals
(
derived
.
getMethod
(
methodName
,
types
));
res
=
clazz
.
getDeclaredMethod
(
methodName
,
types
);
// private, static or final methods can not be overridden
if
(
res
!=
null
&&
(
Modifier
.
isPrivate
(
res
.
getModifiers
())
||
Modifier
.
isFinal
(
res
.
getModifiers
())
||
Modifier
.
isStatic
(
res
.
getModifiers
())))
{
res
=
null
;
}
}
catch
(
NoSuchMethodException
e
)
{
// Method not found in clazz, let's search in superclasses
Class
superclass
=
clazz
.
getSuperclass
();
if
(
superclass
!=
null
)
{
res
=
getMethod
(
superclass
,
methodName
,
types
);
}
}
catch
(
SecurityException
e
)
{
throw
new
AssertionError
(
e
);
}
if
(
res
==
null
)
{
throw
new
IllegalArgumentException
(
String
.
format
(
"Method %s not found in %s (or it is private, final or static)"
,
methodName
,
clazz
.
getName
()));
}
return
res
;
}
/**
...
...
core/src/test/java/hudson/util/IsOverriddenTest.java
0 → 100644
浏览文件 @
89a0050c
/*
* The MIT License
*
* Copyright (c) 2015, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package
hudson.util
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.*;
import
hudson.Util
;
/**
* Test for {@link Util.isOverridden} method.
*/
public
class
IsOverriddenTest
{
/**
* Test that a method is found by isOverridden even when it is inherited from an intermediate class.
*/
@Test
public
void
isOverriddenTest
()
{
assertTrue
(
Util
.
isOverridden
(
Base
.
class
,
Derived
.
class
,
"method"
));
assertTrue
(
Util
.
isOverridden
(
Base
.
class
,
Intermediate
.
class
,
"method"
));
assertFalse
(
Util
.
isOverridden
(
Base
.
class
,
Base
.
class
,
"method"
));
assertTrue
(
Util
.
isOverridden
(
Base
.
class
,
Intermediate
.
class
,
"setX"
,
Object
.
class
));
assertTrue
(
Util
.
isOverridden
(
Base
.
class
,
Intermediate
.
class
,
"getX"
));
}
/**
* Negative test.
* Trying to check for a method which does not exist in the hierarchy,
*/
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
isOverriddenNegativeTest
()
{
Util
.
isOverridden
(
Base
.
class
,
Derived
.
class
,
"method2"
);
}
/**
* Do not inspect private methods.
*/
@Test
(
expected
=
IllegalArgumentException
.
class
)
public
void
avoidPrivateMethodsInspection
()
{
Util
.
isOverridden
(
Base
.
class
,
Intermediate
.
class
,
"aPrivateMethod"
);
}
public
abstract
class
Base
<
T
>
{
protected
abstract
void
method
();
private
void
aPrivateMethod
()
{}
public
void
setX
(
T
t
)
{}
public
T
getX
()
{
return
null
;
}
}
public
abstract
class
Intermediate
extends
Base
<
Integer
>
{
protected
void
method
()
{}
private
void
aPrivateMethod
()
{}
public
void
setX
(
Integer
i
)
{}
public
Integer
getX
()
{
return
0
;
}
}
public
class
Derived
extends
Intermediate
{}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录