Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
device-new-platform
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
赵啸非
device-new-platform
Commits
c4209e2d
Commit
c4209e2d
authored
Nov 17, 2023
by
“yiyousong”
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
http://gitlab.scsmile.cn/zxf/device-new-platform
parents
8100f354
89ff7a09
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
693 additions
and
6 deletions
+693
-6
common-lib/pom.xml
common-lib/pom.xml
+5
-0
device-manager/src/main/java/com/mortals/xhx/busiz/web/DeviceApiController.java
...n/java/com/mortals/xhx/busiz/web/DeviceApiController.java
+2
-0
device-manager/src/main/java/com/mortals/xhx/busiz/web/TestSendMsgController.java
...java/com/mortals/xhx/busiz/web/TestSendMsgController.java
+15
-1
device-manager/src/main/java/com/mortals/xhx/common/utils/ControllerScanUtil.java
...java/com/mortals/xhx/common/utils/ControllerScanUtil.java
+332
-0
device-manager/src/main/java/com/mortals/xhx/common/utils/MemoryPagination.java
...n/java/com/mortals/xhx/common/utils/MemoryPagination.java
+8
-5
device-manager/src/main/java/com/mortals/xhx/module/alarm/web/AlarmConfigController.java
...m/mortals/xhx/module/alarm/web/AlarmConfigController.java
+8
-0
device-manager/src/test/java/ScanTest.java
device-manager/src/test/java/ScanTest.java
+323
-0
No files found.
common-lib/pom.xml
View file @
c4209e2d
...
...
@@ -85,6 +85,11 @@
<artifactId>
javase
</artifactId>
</dependency>
<dependency>
<groupId>
io.swagger.core.v3
</groupId>
<artifactId>
swagger-annotations
</artifactId>
</dependency>
<dependency>
<groupId>
junit
</groupId>
...
...
device-manager/src/main/java/com/mortals/xhx/busiz/web/DeviceApiController.java
View file @
c4209e2d
...
...
@@ -61,6 +61,7 @@ import com.mortals.xhx.module.site.service.SiteService;
import
com.mortals.xhx.queue.DefaultTbQueueMsg
;
import
com.mortals.xhx.queue.TbQueueMsg
;
import
com.mortals.xhx.queue.TbQueueMsgHeaders
;
import
io.swagger.v3.oas.annotations.tags.Tag
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.BeanUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -89,6 +90,7 @@ import static com.mortals.xhx.common.key.RedisKey.KEY_TOKEN_API_CACHE;
@RestController
@Slf4j
@RequestMapping
(
"/api"
)
@Tag
(
name
=
"设备api"
)
public
class
DeviceApiController
{
@Autowired
private
DeviceService
deviceService
;
...
...
device-manager/src/main/java/com/mortals/xhx/busiz/web/TestSendMsgController.java
View file @
c4209e2d
...
...
@@ -5,14 +5,17 @@ import cn.hutool.core.util.StrUtil;
import
cn.hutool.http.HttpUtil
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.mortals.framework.annotation.UnAuth
;
import
com.mortals.framework.common.Rest
;
import
com.mortals.framework.service.ICacheService
;
import
com.mortals.framework.service.ILogService
;
import
com.mortals.framework.service.impl.FileLogServiceImpl
;
import
com.mortals.xhx.base.system.resource.model.ResourceEntity
;
import
com.mortals.xhx.busiz.req.TestReq
;
import
com.mortals.xhx.busiz.rsp.ApiResp
;
import
com.mortals.xhx.busiz.rsp.DeviceResp
;
import
com.mortals.xhx.common.code.ApiRespCodeEnum
;
import
com.mortals.xhx.common.utils.ControllerScanUtil
;
import
com.mortals.xhx.common.utils.ImgUtils
;
import
com.mortals.xhx.module.device.model.DeviceEntity
;
import
com.mortals.xhx.module.device.model.DeviceQuery
;
...
...
@@ -145,11 +148,22 @@ public class TestSendMsgController {
public
String
setData
(
@PathVariable
(
"key"
)
String
key
,
@PathVariable
(
"value"
)
String
value
)
{
cacheService
.
set
(
key
,
value
);
String
s
=
cacheService
.
get
(
key
);
String
s
=
cacheService
.
get
(
key
);
return
s
;
}
@PostMapping
(
"scan"
)
@UnAuth
public
String
scan
()
{
List
<
Class
<?>>
classList
=
ControllerScanUtil
.
getAllClassByPackageName
(
"com.mortals.xhx"
);
//System.out.println(classList); //获取到了所有的类
List
<
ResourceEntity
>
list
=
ControllerScanUtil
.
getAnnotationInfo
(
classList
);
return
JSON
.
toJSONString
(
list
);
}
public
static
void
main
(
String
[]
args
)
{
String
resp
=
HttpUtil
.
get
(
"http://59.225.206.13:8331/oauth-pro/admin-pro/sys/randomImage/1675827431800?_t=1675827431"
);
...
...
device-manager/src/main/java/com/mortals/xhx/common/utils/ControllerScanUtil.java
0 → 100644
View file @
c4209e2d
package
com.mortals.xhx.common.utils
;
import
cn.hutool.core.util.RandomUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.alibaba.fastjson.JSONObject
;
import
com.mortals.framework.annotation.UnAuth
;
import
com.mortals.xhx.base.system.resource.model.ResourceEntity
;
import
io.swagger.v3.oas.annotations.tags.Tag
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.io.File
;
import
java.io.FileFilter
;
import
java.io.IOException
;
import
java.lang.reflect.Method
;
import
java.net.JarURLConnection
;
import
java.net.URL
;
import
java.net.URLDecoder
;
import
java.util.*
;
import
java.util.jar.JarEntry
;
import
java.util.jar.JarFile
;
import
java.util.stream.Collectors
;
public
class
ControllerScanUtil
{
/**
* 查看类路径
*/
public
static
final
String
[]
ULR_VIEW
=
{
"view"
,
"info"
,
"list"
,
"get"
,
"find"
,
"export"
,
"download"
,
"index"
,
"bill"
};
public
static
void
main
(
String
[]
args
)
{
List
<
Class
<?>>
classList
=
getAllClassByPackageName
(
"com.mortals.xhx"
);
//System.out.println(classList); //获取到了所有的类
getAnnotationInfo
(
classList
);
}
public
static
List
<
ResourceEntity
>
getAnnotationInfo
(
List
<
Class
<?>>
clsList
)
{
List
<
ResourceEntity
>
resourceArrayList
=
new
ArrayList
<>();
if
(
clsList
!=
null
&&
clsList
.
size
()
>
0
)
{
for
(
Class
<?>
cls
:
clsList
)
{
boolean
exits
=
cls
.
isAnnotationPresent
(
RestController
.
class
);
if
(!
exits
)
{
continue
;
}
ResourceEntity
resourceViewEntity
=
new
ResourceEntity
();
resourceViewEntity
.
initAttrValue
();
ResourceEntity
resourceEditEntity
=
new
ResourceEntity
();
resourceEditEntity
.
initAttrValue
();
Method
substringMethod
=
null
;
String
result
=
""
;
try
{
substringMethod
=
cls
.
getMethod
(
"getModuleDesc"
);
result
=
(
String
)
substringMethod
.
invoke
(
cls
.
newInstance
());
resourceViewEntity
.
setName
(
result
+
"-查看"
);
resourceEditEntity
.
setName
(
result
+
"-维护"
);
}
catch
(
Exception
e
)
{
}
if
(
ObjectUtils
.
isEmpty
(
result
))
{
boolean
tagExits
=
cls
.
isAnnotationPresent
(
Tag
.
class
);
if
(
tagExits
)
{
Tag
annotation
=
cls
.
getAnnotation
(
Tag
.
class
);
result
=
annotation
.
name
();
}
else
{
//自定义的controller 没有名称
result
=
"自定义api模块"
+
RandomUtil
.
randomInt
(
100
);
}
resourceViewEntity
.
setName
(
result
+
"-查看"
);
resourceEditEntity
.
setName
(
result
+
"-维护"
);
}
System
.
out
.
println
(
result
);
RequestMapping
requestMappingCls
=
cls
.
getAnnotation
(
RequestMapping
.
class
);
String
prefix
=
""
;
if
(
requestMappingCls
!=
null
)
{
prefix
=
"/"
+
requestMappingCls
.
value
()[
0
];
}
Set
<
String
>
urlSet
=
new
HashSet
<>();
//获取类中的所有的方法
Method
[]
methods
=
cls
.
getDeclaredMethods
();
if
(
methods
!=
null
&&
methods
.
length
>
0
)
{
for
(
Method
method
:
methods
)
{
boolean
unAuth
=
method
.
isAnnotationPresent
(
UnAuth
.
class
);
if
(
unAuth
)
{
continue
;
}
boolean
mExits
=
method
.
isAnnotationPresent
(
RequestMapping
.
class
);
if
(
mExits
)
{
RequestMapping
requestMapping
=
method
.
getAnnotation
(
RequestMapping
.
class
);
String
url
=
prefix
+
"/"
+
requestMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
// System.out.println("/" + prefix + "/" + requestMapping.value()[0]);
}
boolean
mExits1
=
method
.
isAnnotationPresent
(
GetMapping
.
class
);
if
(
mExits1
)
{
GetMapping
getMapping
=
method
.
getAnnotation
(
GetMapping
.
class
);
String
url
=
prefix
+
"/"
+
getMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
// System.out.println("/" + prefix + "/" + getMapping.value()[0]);
}
boolean
mExits2
=
method
.
isAnnotationPresent
(
PostMapping
.
class
);
if
(
mExits2
)
{
PostMapping
postMapping
=
method
.
getAnnotation
(
PostMapping
.
class
);
String
url
=
prefix
+
"/"
+
postMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
// System.out.println("/" + prefix + "/" + postMapping.value()[0]);
}
}
}
//获取超类中的方法
Method
[]
superMethods
=
cls
.
getSuperclass
().
getDeclaredMethods
();
if
(
superMethods
!=
null
&&
superMethods
.
length
>
0
)
{
for
(
Method
method
:
superMethods
)
{
boolean
unAuth
=
method
.
isAnnotationPresent
(
UnAuth
.
class
);
if
(
unAuth
)
{
continue
;
}
boolean
mExits
=
method
.
isAnnotationPresent
(
RequestMapping
.
class
);
if
(
mExits
)
{
RequestMapping
requestMapping
=
method
.
getAnnotation
(
RequestMapping
.
class
);
String
url
=
prefix
+
"/"
+
requestMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
// System.out.println("super /" + prefix + "/" + requestMapping.value()[0]);
}
boolean
mExits1
=
method
.
isAnnotationPresent
(
GetMapping
.
class
);
if
(
mExits1
)
{
GetMapping
getMapping
=
method
.
getAnnotation
(
GetMapping
.
class
);
String
url
=
prefix
+
"/"
+
getMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
//System.out.println("super /" + prefix + "/" + getMapping.value()[0]);
}
boolean
mExits2
=
method
.
isAnnotationPresent
(
PostMapping
.
class
);
if
(
mExits2
)
{
PostMapping
postMapping
=
method
.
getAnnotation
(
PostMapping
.
class
);
String
url
=
prefix
+
"/"
+
postMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
// System.out.println("super /" + prefix + "/" + postMapping.value()[0]);
}
}
}
if
(!
ObjectUtils
.
isEmpty
(
urlSet
))
{
Set
<
String
>
UrlViewSet
=
new
HashSet
<>();
Set
<
String
>
UrlEditSet
=
new
HashSet
<>();
for
(
String
url
:
urlSet
)
{
String
str
=
StrUtil
.
subAfter
(
url
,
"/"
,
true
);
boolean
bool
=
false
;
for
(
String
checkStr
:
ULR_VIEW
)
{
boolean
contains
=
StrUtil
.
contains
(
str
.
toLowerCase
(),
checkStr
);
if
(
contains
)
{
UrlViewSet
.
add
(
url
);
bool
=
true
;
break
;
}
}
if
(!
bool
)
{
UrlEditSet
.
add
(
url
);
}
}
resourceViewEntity
.
setUrl
(
UrlViewSet
.
stream
().
collect
(
Collectors
.
joining
(
","
)));
System
.
out
.
println
(
JSONObject
.
toJSONString
(
resourceViewEntity
));
resourceArrayList
.
add
(
resourceViewEntity
);
resourceEditEntity
.
setUrl
(
UrlEditSet
.
stream
().
collect
(
Collectors
.
joining
(
","
)));
System
.
out
.
println
(
JSONObject
.
toJSONString
(
resourceEditEntity
));
resourceArrayList
.
add
(
resourceEditEntity
);
}
}
}
return
resourceArrayList
;
}
public
static
List
<
Class
<?>>
getAllClassByPackageName
(
String
packageName
)
{
// String packageName = pkg.getName();
// 获取当前包下以及子包下所以的类
List
<
Class
<?>>
returnClassList
=
getClasses
(
packageName
);
return
returnClassList
;
}
/**
* 从包package中获取所有的Class
*
* @param packageName
* @return
*/
private
static
List
<
Class
<?>>
getClasses
(
String
packageName
)
{
// 第一个class类的集合
List
<
Class
<?>>
classes
=
new
ArrayList
<
Class
<?>>();
// 是否循环迭代
boolean
recursive
=
true
;
// 获取包的名字 并进行替换
String
packageDirName
=
packageName
.
replace
(
'.'
,
'/'
);
// 定义一个枚举的集合 并进行循环来处理这个目录下的things
Enumeration
<
URL
>
dirs
;
try
{
dirs
=
Thread
.
currentThread
().
getContextClassLoader
().
getResources
(
packageDirName
);
// 循环迭代下去
while
(
dirs
.
hasMoreElements
())
{
// 获取下一个元素
URL
url
=
dirs
.
nextElement
();
// 得到协议的名称
String
protocol
=
url
.
getProtocol
();
//System.out.println(url.getFile());
// 如果是以文件的形式保存在服务器上
if
(
"file"
.
equals
(
protocol
))
{
// 获取包的物理路径
String
filePath
=
URLDecoder
.
decode
(
url
.
getFile
(),
"UTF-8"
);
// 以文件的方式扫描整个包下的文件 并添加到集合中
findAndAddClassesInPackageByFile
(
packageName
,
filePath
,
recursive
,
classes
);
}
else
if
(
"jar"
.
equals
(
protocol
))
{
// 如果是jar包文件
// 定义一个JarFile
JarFile
jar
;
try
{
// 获取jar
jar
=
((
JarURLConnection
)
url
.
openConnection
()).
getJarFile
();
// 从此jar包 得到一个枚举类
Enumeration
<
JarEntry
>
entries
=
jar
.
entries
();
// 同样的进行循环迭代
while
(
entries
.
hasMoreElements
())
{
// 获取jar里的一个实体 可以是目录 和一些jar包里的其他文件 如META-INF等文件
JarEntry
entry
=
entries
.
nextElement
();
String
name
=
entry
.
getName
();
// 如果是以/开头的
if
(
name
.
charAt
(
0
)
==
'/'
)
{
// 获取后面的字符串
name
=
name
.
substring
(
1
);
}
// 如果前半部分和定义的包名相同
if
(
name
.
startsWith
(
packageDirName
))
{
int
idx
=
name
.
lastIndexOf
(
'/'
);
// 如果以"/"结尾 是一个包
if
(
idx
!=
-
1
)
{
// 获取包名 把"/"替换成"."
packageName
=
name
.
substring
(
0
,
idx
).
replace
(
'/'
,
'.'
);
}
// 如果可以迭代下去 并且是一个包
if
((
idx
!=
-
1
)
||
recursive
)
{
// 如果是一个.class文件 而且不是目录
if
(
name
.
endsWith
(
".class"
)
&&
!
entry
.
isDirectory
())
{
// 去掉后面的".class" 获取真正的类名
String
className
=
name
.
substring
(
packageName
.
length
()
+
1
,
name
.
length
()
-
6
);
try
{
// 添加到classes
classes
.
add
(
Class
.
forName
(
packageName
+
'.'
+
className
));
}
catch
(
ClassNotFoundException
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
classes
;
}
/**
* 以文件的形式来获取包下的所有Class
*
* @param packageName
* @param packagePath
* @param recursive
* @param classes
*/
private
static
void
findAndAddClassesInPackageByFile
(
String
packageName
,
String
packagePath
,
final
boolean
recursive
,
List
<
Class
<?>>
classes
)
{
// 获取此包的目录 建立一个File
File
dir
=
new
File
(
packagePath
);
// 如果不存在或者 也不是目录就直接返回
if
(!
dir
.
exists
()
||
!
dir
.
isDirectory
())
{
return
;
}
// 如果存在 就获取包下的所有文件 包括目录
File
[]
dirFiles
=
dir
.
listFiles
(
new
FileFilter
()
{
// 自定义过滤规则 如果可以循环(包含子目录) 或则是以.class结尾的文件(编译好的java类文件)
public
boolean
accept
(
File
file
)
{
return
(
recursive
&&
file
.
isDirectory
())
||
(
file
.
getName
().
endsWith
(
".class"
));
}
});
assert
dirFiles
!=
null
;
// 循环所有文件
for
(
File
file
:
dirFiles
)
{
// 如果是目录 则继续扫描
if
(
file
.
isDirectory
())
{
findAndAddClassesInPackageByFile
(
packageName
+
"."
+
file
.
getName
(),
file
.
getAbsolutePath
(),
recursive
,
classes
);
}
else
{
// 如果是java类文件 去掉后面的.class 只留下类名
String
className
=
file
.
getName
().
substring
(
0
,
file
.
getName
().
length
()
-
6
);
try
{
// 添加到集合中去
classes
.
add
(
Class
.
forName
(
packageName
+
'.'
+
className
));
}
catch
(
ClassNotFoundException
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
device-manager/src/main/java/com/mortals/xhx/common/utils/MemoryPagination.java
View file @
c4209e2d
package
com.mortals.xhx.common.utils
;
import
com.mortals.xhx.common.pdu.site.SitePdu
;
import
com.mortals.xhx.module.sitestat.model.SitestatEntity
;
import
org.apache.commons.collections4.CollectionUtils
;
...
...
@@ -33,6 +32,10 @@ public class MemoryPagination {
if
(
CollectionUtils
.
isEmpty
(
records
))
{
return
Collections
.
emptyList
();
}
if
(
pageSize
==
-
1
)
{
pageSize
=
9999
;
}
int
totalCount
=
records
.
size
();
int
remainder
=
totalCount
%
pageSize
;
int
pageCount
=
(
remainder
>
0
)
?
totalCount
/
pageSize
+
1
:
totalCount
/
pageSize
;
...
...
@@ -48,12 +51,12 @@ public class MemoryPagination {
}
public
static
List
<
SitestatEntity
>
search
(
String
name
,
List
<
SitestatEntity
>
list
){
public
static
List
<
SitestatEntity
>
search
(
String
name
,
List
<
SitestatEntity
>
list
)
{
List
<
SitestatEntity
>
results
=
new
ArrayList
();
Pattern
pattern
=
Pattern
.
compile
(
name
,
Pattern
.
CASE_INSENSITIVE
);
for
(
int
i
=
0
;
i
<
list
.
size
();
i
++)
{
Pattern
pattern
=
Pattern
.
compile
(
name
,
Pattern
.
CASE_INSENSITIVE
);
for
(
int
i
=
0
;
i
<
list
.
size
();
i
++)
{
Matcher
matcher
=
pattern
.
matcher
((
list
.
get
(
i
)).
getSiteName
());
if
(
matcher
.
find
())
{
if
(
matcher
.
find
())
{
results
.
add
(
list
.
get
(
i
));
}
}
...
...
device-manager/src/main/java/com/mortals/xhx/module/alarm/web/AlarmConfigController.java
View file @
c4209e2d
...
...
@@ -46,6 +46,14 @@ public class AlarmConfigController extends BaseCRUDJsonBodyMappingController<Ala
super
.
init
(
model
,
context
);
}
public
static
void
main
(
String
[]
args
)
{
String
input
=
"example//path//to//file//with/////multiple////slashes//"
;
// 将多个斜杠替换为单个斜杠
String
result
=
input
.
replaceAll
(
"/+"
,
"/"
);
System
.
out
.
println
(
"处理前: "
+
input
);
System
.
out
.
println
(
"处理后: "
+
result
);
}
}
\ No newline at end of file
device-manager/src/test/java/ScanTest.java
0 → 100644
View file @
c4209e2d
import
cn.hutool.core.util.RandomUtil
;
import
cn.hutool.core.util.StrUtil
;
import
com.alibaba.fastjson.JSONObject
;
import
com.mortals.framework.annotation.UnAuth
;
import
com.mortals.xhx.base.system.resource.model.ResourceEntity
;
import
com.mortals.xhx.module.site.web.SiteController
;
import
io.swagger.v3.oas.annotations.tags.Tag
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
java.io.File
;
import
java.io.FileFilter
;
import
java.io.IOException
;
import
java.lang.reflect.Method
;
import
java.net.JarURLConnection
;
import
java.net.URL
;
import
java.net.URLDecoder
;
import
java.util.*
;
import
java.util.jar.JarEntry
;
import
java.util.jar.JarFile
;
import
java.util.stream.Collectors
;
public
class
ScanTest
{
/**
* 查看类路径
*/
public
static
final
String
[]
ULR_VIEW
=
{
"view"
,
"info"
,
"list"
,
"get"
,
"find"
,
"export"
,
"download"
,
"index"
,
"bill"
};
public
static
void
main
(
String
[]
args
)
{
List
<
Class
<?>>
classList
=
getAllClassByPackageName
(
SiteController
.
class
.
getPackage
());
//System.out.println(classList); //获取到了所有的类
getAnnotationInfo
(
classList
);
}
public
static
void
getAnnotationInfo
(
List
<
Class
<?>>
clsList
)
{
if
(
clsList
!=
null
&&
clsList
.
size
()
>
0
)
{
for
(
Class
<?>
cls
:
clsList
)
{
boolean
exits
=
cls
.
isAnnotationPresent
(
RestController
.
class
);
if
(!
exits
)
{
continue
;
}
ResourceEntity
resourceViewEntity
=
new
ResourceEntity
();
resourceViewEntity
.
initAttrValue
();
ResourceEntity
resourceEditEntity
=
new
ResourceEntity
();
resourceEditEntity
.
initAttrValue
();
Method
substringMethod
=
null
;
String
result
=
""
;
try
{
substringMethod
=
cls
.
getMethod
(
"getModuleDesc"
);
result
=
(
String
)
substringMethod
.
invoke
(
cls
.
newInstance
());
resourceViewEntity
.
setName
(
result
+
"-查看"
);
resourceEditEntity
.
setName
(
result
+
"-维护"
);
}
catch
(
Exception
e
)
{
}
if
(
ObjectUtils
.
isEmpty
(
result
))
{
boolean
tagExits
=
cls
.
isAnnotationPresent
(
Tag
.
class
);
if
(
tagExits
)
{
Tag
annotation
=
cls
.
getAnnotation
(
Tag
.
class
);
result
=
annotation
.
name
();
}
else
{
//自定义的controller 没有名称
result
=
"自定义api模块"
+
RandomUtil
.
randomInt
(
100
);
}
resourceViewEntity
.
setName
(
result
+
"-查看"
);
resourceEditEntity
.
setName
(
result
+
"-维护"
);
}
System
.
out
.
println
(
result
);
RequestMapping
requestMappingCls
=
cls
.
getAnnotation
(
RequestMapping
.
class
);
String
prefix
=
""
;
if
(
requestMappingCls
!=
null
)
{
prefix
=
"/"
+
requestMappingCls
.
value
()[
0
];
}
Set
<
String
>
urlSet
=
new
HashSet
<>();
//获取类中的所有的方法
Method
[]
methods
=
cls
.
getDeclaredMethods
();
if
(
methods
!=
null
&&
methods
.
length
>
0
)
{
for
(
Method
method
:
methods
)
{
boolean
unAuth
=
method
.
isAnnotationPresent
(
UnAuth
.
class
);
if
(
unAuth
)
{
continue
;
}
boolean
mExits
=
method
.
isAnnotationPresent
(
RequestMapping
.
class
);
if
(
mExits
)
{
RequestMapping
requestMapping
=
method
.
getAnnotation
(
RequestMapping
.
class
);
String
url
=
prefix
+
"/"
+
requestMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
// System.out.println("/" + prefix + "/" + requestMapping.value()[0]);
}
boolean
mExits1
=
method
.
isAnnotationPresent
(
GetMapping
.
class
);
if
(
mExits1
)
{
GetMapping
getMapping
=
method
.
getAnnotation
(
GetMapping
.
class
);
String
url
=
prefix
+
"/"
+
getMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
// System.out.println("/" + prefix + "/" + getMapping.value()[0]);
}
boolean
mExits2
=
method
.
isAnnotationPresent
(
PostMapping
.
class
);
if
(
mExits2
)
{
PostMapping
postMapping
=
method
.
getAnnotation
(
PostMapping
.
class
);
String
url
=
prefix
+
"/"
+
postMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
// System.out.println("/" + prefix + "/" + postMapping.value()[0]);
}
}
}
//获取超类中的方法
Method
[]
superMethods
=
cls
.
getSuperclass
().
getDeclaredMethods
();
if
(
superMethods
!=
null
&&
superMethods
.
length
>
0
)
{
for
(
Method
method
:
superMethods
)
{
boolean
unAuth
=
method
.
isAnnotationPresent
(
UnAuth
.
class
);
if
(
unAuth
)
{
continue
;
}
boolean
mExits
=
method
.
isAnnotationPresent
(
RequestMapping
.
class
);
if
(
mExits
)
{
RequestMapping
requestMapping
=
method
.
getAnnotation
(
RequestMapping
.
class
);
String
url
=
prefix
+
"/"
+
requestMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
// System.out.println("super /" + prefix + "/" + requestMapping.value()[0]);
}
boolean
mExits1
=
method
.
isAnnotationPresent
(
GetMapping
.
class
);
if
(
mExits1
)
{
GetMapping
getMapping
=
method
.
getAnnotation
(
GetMapping
.
class
);
String
url
=
prefix
+
"/"
+
getMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
//System.out.println("super /" + prefix + "/" + getMapping.value()[0]);
}
boolean
mExits2
=
method
.
isAnnotationPresent
(
PostMapping
.
class
);
if
(
mExits2
)
{
PostMapping
postMapping
=
method
.
getAnnotation
(
PostMapping
.
class
);
String
url
=
prefix
+
"/"
+
postMapping
.
value
()[
0
];
url
=
url
.
replaceAll
(
"/+"
,
"/"
);
urlSet
.
add
(
url
);
// System.out.println("super /" + prefix + "/" + postMapping.value()[0]);
}
}
}
if
(!
ObjectUtils
.
isEmpty
(
urlSet
))
{
Set
<
String
>
UrlViewSet
=
new
HashSet
<>();
Set
<
String
>
UrlEditSet
=
new
HashSet
<>();
for
(
String
url
:
urlSet
)
{
String
str
=
StrUtil
.
subAfter
(
url
,
"/"
,
true
);
boolean
bool
=
false
;
for
(
String
checkStr
:
ULR_VIEW
)
{
boolean
contains
=
StrUtil
.
contains
(
str
.
toLowerCase
(),
checkStr
);
if
(
contains
)
{
UrlViewSet
.
add
(
url
);
bool
=
true
;
break
;
}
}
if
(!
bool
){
UrlEditSet
.
add
(
url
);
}
}
resourceViewEntity
.
setUrl
(
UrlViewSet
.
stream
().
collect
(
Collectors
.
joining
(
","
)));
System
.
out
.
println
(
JSONObject
.
toJSONString
(
resourceViewEntity
));
resourceEditEntity
.
setUrl
(
UrlEditSet
.
stream
().
collect
(
Collectors
.
joining
(
","
)));
System
.
out
.
println
(
JSONObject
.
toJSONString
(
resourceEditEntity
));
}
}
}
}
public
static
List
<
Class
<?>>
getAllClassByPackageName
(
Package
pkg
)
{
// String packageName = pkg.getName();
String
packageName
=
"com.mortals.xhx"
;
// 获取当前包下以及子包下所以的类
List
<
Class
<?>>
returnClassList
=
getClasses
(
packageName
);
return
returnClassList
;
}
/**
* 从包package中获取所有的Class
*
* @param packageName
* @return
*/
private
static
List
<
Class
<?>>
getClasses
(
String
packageName
)
{
// 第一个class类的集合
List
<
Class
<?>>
classes
=
new
ArrayList
<
Class
<?>>();
// 是否循环迭代
boolean
recursive
=
true
;
// 获取包的名字 并进行替换
String
packageDirName
=
packageName
.
replace
(
'.'
,
'/'
);
// 定义一个枚举的集合 并进行循环来处理这个目录下的things
Enumeration
<
URL
>
dirs
;
try
{
dirs
=
Thread
.
currentThread
().
getContextClassLoader
().
getResources
(
packageDirName
);
// 循环迭代下去
while
(
dirs
.
hasMoreElements
())
{
// 获取下一个元素
URL
url
=
dirs
.
nextElement
();
// 得到协议的名称
String
protocol
=
url
.
getProtocol
();
//System.out.println(url.getFile());
// 如果是以文件的形式保存在服务器上
if
(
"file"
.
equals
(
protocol
))
{
// 获取包的物理路径
String
filePath
=
URLDecoder
.
decode
(
url
.
getFile
(),
"UTF-8"
);
// 以文件的方式扫描整个包下的文件 并添加到集合中
findAndAddClassesInPackageByFile
(
packageName
,
filePath
,
recursive
,
classes
);
}
else
if
(
"jar"
.
equals
(
protocol
))
{
// 如果是jar包文件
// 定义一个JarFile
JarFile
jar
;
try
{
// 获取jar
jar
=
((
JarURLConnection
)
url
.
openConnection
()).
getJarFile
();
// 从此jar包 得到一个枚举类
Enumeration
<
JarEntry
>
entries
=
jar
.
entries
();
// 同样的进行循环迭代
while
(
entries
.
hasMoreElements
())
{
// 获取jar里的一个实体 可以是目录 和一些jar包里的其他文件 如META-INF等文件
JarEntry
entry
=
entries
.
nextElement
();
String
name
=
entry
.
getName
();
// 如果是以/开头的
if
(
name
.
charAt
(
0
)
==
'/'
)
{
// 获取后面的字符串
name
=
name
.
substring
(
1
);
}
// 如果前半部分和定义的包名相同
if
(
name
.
startsWith
(
packageDirName
))
{
int
idx
=
name
.
lastIndexOf
(
'/'
);
// 如果以"/"结尾 是一个包
if
(
idx
!=
-
1
)
{
// 获取包名 把"/"替换成"."
packageName
=
name
.
substring
(
0
,
idx
).
replace
(
'/'
,
'.'
);
}
// 如果可以迭代下去 并且是一个包
if
((
idx
!=
-
1
)
||
recursive
)
{
// 如果是一个.class文件 而且不是目录
if
(
name
.
endsWith
(
".class"
)
&&
!
entry
.
isDirectory
())
{
// 去掉后面的".class" 获取真正的类名
String
className
=
name
.
substring
(
packageName
.
length
()
+
1
,
name
.
length
()
-
6
);
try
{
// 添加到classes
classes
.
add
(
Class
.
forName
(
packageName
+
'.'
+
className
));
}
catch
(
ClassNotFoundException
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
classes
;
}
/**
* 以文件的形式来获取包下的所有Class
*
* @param packageName
* @param packagePath
* @param recursive
* @param classes
*/
private
static
void
findAndAddClassesInPackageByFile
(
String
packageName
,
String
packagePath
,
final
boolean
recursive
,
List
<
Class
<?>>
classes
)
{
// 获取此包的目录 建立一个File
File
dir
=
new
File
(
packagePath
);
// 如果不存在或者 也不是目录就直接返回
if
(!
dir
.
exists
()
||
!
dir
.
isDirectory
())
{
return
;
}
// 如果存在 就获取包下的所有文件 包括目录
File
[]
dirFiles
=
dir
.
listFiles
(
new
FileFilter
()
{
// 自定义过滤规则 如果可以循环(包含子目录) 或则是以.class结尾的文件(编译好的java类文件)
public
boolean
accept
(
File
file
)
{
return
(
recursive
&&
file
.
isDirectory
())
||
(
file
.
getName
().
endsWith
(
".class"
));
}
});
assert
dirFiles
!=
null
;
// 循环所有文件
for
(
File
file
:
dirFiles
)
{
// 如果是目录 则继续扫描
if
(
file
.
isDirectory
())
{
findAndAddClassesInPackageByFile
(
packageName
+
"."
+
file
.
getName
(),
file
.
getAbsolutePath
(),
recursive
,
classes
);
}
else
{
// 如果是java类文件 去掉后面的.class 只留下类名
String
className
=
file
.
getName
().
substring
(
0
,
file
.
getName
().
length
()
-
6
);
try
{
// 添加到集合中去
classes
.
add
(
Class
.
forName
(
packageName
+
'.'
+
className
));
}
catch
(
ClassNotFoundException
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment