Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell11
提交
11cb7f29
D
dragonwell11
项目概览
openanolis
/
dragonwell11
通知
7
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell11
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
11cb7f29
编写于
2月 10, 2017
作者:
V
vromero
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8174099: class ComboTask at the combo test library needs an execute() method
Reviewed-by: mcimadamore
上级
a14e153c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
105 addition
and
2 deletion
+105
-2
langtools/test/tools/javac/lib/combo/ComboTask.java
langtools/test/tools/javac/lib/combo/ComboTask.java
+105
-2
未找到文件。
langtools/test/tools/javac/lib/combo/ComboTask.java
浏览文件 @
11cb7f29
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015,
2017,
Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
...
...
@@ -25,9 +25,9 @@ package combo;
import
com.sun.source.tree.CompilationUnitTree
;
import
com.sun.source.util.JavacTask
;
import
com.sun.source.util.TaskEvent.Kind
;
import
com.sun.source.util.TaskListener
;
import
com.sun.tools.javac.api.JavacTool
;
import
com.sun.tools.javac.util.Assert
;
import
com.sun.tools.javac.util.List
;
import
combo.ComboParameter.Resolver
;
...
...
@@ -36,11 +36,18 @@ import javax.tools.Diagnostic;
import
javax.tools.DiagnosticListener
;
import
javax.tools.JavaFileObject
;
import
javax.tools.SimpleJavaFileObject
;
import
java.io.IOException
;
import
java.io.Writer
;
import
java.net.URI
;
import
java.net.URL
;
import
java.net.URLClassLoader
;
import
java.util.ArrayList
;
import
java.util.function.Consumer
;
import
java.util.function.Function
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.stream.Collectors
;
import
java.util.stream.StreamSupport
;
...
...
@@ -183,6 +190,28 @@ public class ComboTask {
return
new
Result
<>(
getTask
().
generate
());
}
/**
* Parse, analyze, perform code generation for the sources associated with this task and finally
* executes them
*/
public
<
Z
>
Optional
<
Z
>
execute
(
Function
<
ExecutionTask
,
Z
>
executionFunc
)
throws
IOException
{
Result
<
Iterable
<?
extends
JavaFileObject
>>
generationResult
=
generate
();
Iterable
<?
extends
JavaFileObject
>
jfoIterable
=
generationResult
.
get
();
if
(
generationResult
.
hasErrors
())
{
// we have nothing else to do
return
Optional
.
empty
();
}
java
.
util
.
List
<
URL
>
urlList
=
new
ArrayList
<>();
for
(
JavaFileObject
jfo
:
jfoIterable
)
{
String
urlStr
=
jfo
.
toUri
().
toURL
().
toString
();
urlStr
=
urlStr
.
substring
(
0
,
urlStr
.
length
()
-
jfo
.
getName
().
length
());
urlList
.
add
(
new
URL
(
urlStr
));
}
return
Optional
.
of
(
executionFunc
.
apply
(
new
ExecutionTask
(
new
URLClassLoader
(
urlList
.
toArray
(
new
URL
[
urlList
.
size
()])))));
}
/**
* Fork a new compilation task; if possible the compilation context from previous executions is
* retained (see comments in ReusableContext as to when it's safe to do so); otherwise a brand
...
...
@@ -214,6 +243,80 @@ public class ComboTask {
return
task
;
}
/**
* This class represents an execution task. It allows the execution of one or more classes previously
* added to a given class loader. This class uses reflection to execute any given static public method
* in any given class. It's not restricted to the execution of the {@code main} method
*/
public
class
ExecutionTask
{
private
ClassLoader
classLoader
;
private
String
methodName
=
"main"
;
private
Class
<?>[]
parameterTypes
=
new
Class
<?>[]{
String
[].
class
};
private
Object
[]
args
=
new
String
[
0
];
private
Consumer
<
Throwable
>
handler
;
private
Class
<?>
c
;
private
ExecutionTask
(
ClassLoader
classLoader
)
{
this
.
classLoader
=
classLoader
;
}
/**
* Set the name of the class to be loaded.
*/
public
ExecutionTask
withClass
(
String
className
)
{
Assert
.
check
(
className
!=
null
,
"class name value is null, impossible to proceed"
);
try
{
c
=
classLoader
.
loadClass
(
className
);
}
catch
(
Throwable
t
)
{
throw
new
IllegalStateException
(
t
);
}
return
this
;
}
/**
* Set the name of the method to be executed along with the parameter types to
* reflectively obtain the method.
*/
public
ExecutionTask
withMethod
(
String
methodName
,
Class
<?>...
parameterTypes
)
{
this
.
methodName
=
methodName
;
this
.
parameterTypes
=
parameterTypes
;
return
this
;
}
/**
* Set the arguments to be passed to the method.
*/
public
ExecutionTask
withArguments
(
Object
...
args
)
{
this
.
args
=
args
;
return
this
;
}
/**
* Set a handler to handle any exception thrown.
*/
public
ExecutionTask
withHandler
(
Consumer
<
Throwable
>
handler
)
{
this
.
handler
=
handler
;
return
this
;
}
/**
* Executes the given method in the given class. Returns true if the execution was
* successful, false otherwise.
*/
public
Object
run
()
{
try
{
java
.
lang
.
reflect
.
Method
meth
=
c
.
getMethod
(
methodName
,
parameterTypes
);
meth
.
invoke
(
null
,
(
Object
)
args
);
return
true
;
}
catch
(
Throwable
t
)
{
if
(
handler
!=
null
)
{
handler
.
accept
(
t
);
}
return
false
;
}
}
}
/**
* This class is used to help clients accessing the results of a given compilation task.
* Contains several helper methods to inspect diagnostics generated during the task execution.
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录