出于某种原因,我不明白为什么这个代码会打印出来,true
并且false
有关数组的特殊之处是它不包含这个注释吗?
如果您使用它,它会按预期工作getParameters
.
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE_USE, ElementType.PARAMETER})
@interface Lel {
}
class Test {
public static void a(@Lel String args) {}
public static void b(@Lel String[] args) {}
public static void main(String[] args) throws Exception {
System.out.println(Test.class.getDeclaredMethod("a", String.class)
.getAnnotatedParameterTypes()[0].isAnnotationPresent(Lel.class));
System.out.println(Test.class.getDeclaredMethod("b", String[].class)
.getAnnotatedParameterTypes()[0].isAnnotationPresent(Lel.class));
}
}
Run Code Online (Sandbox Code Playgroud) 我有TypeToken
类用来表示这样的泛型类型:
TypeToken<List<String>> listOfStrings = new TypeToken<List<String>> {}
这样工作正常,TypeToken
只是class TypeToken<T> {}
用简单的方法来获得该类型.
现在我想为常见类型创建简单的方法,例如List
更动态的用法:
TypeToken<List<? extends Number>> numbers = list(extendsType(Number.class))
使用:
public static <T> TypeToken<? extends T> extendsType(Class<T> type) {return null;}
public static <T> TypeToken<List<T>> list(TypeToken<T> type) {return null;}
Run Code Online (Sandbox Code Playgroud)
(返回nulls,因为我只询问编译器而不是逻辑)
但由于某种原因,这不符合我的预期:(因为我期望有效的代码不能编译,而我预期无效的代码会编译)
class TypeToken<X> {
static <T> TypeToken<? extends T> extendsType(Class<T> type) {return null;}
static <T> TypeToken<List<T>> list(TypeToken<T> type) {return null;}
static void wat() {
TypeToken<List<? extends Number>> a = new TypeToken<List<? extends Number>>() {}; // …
Run Code Online (Sandbox Code Playgroud) 我发现了这个线程:如何在运行时使用反射更改批注值?
而且我正在尝试更改方法注释,但是java.lang.reflect.Method不包含任何地图字段(例如“ annotations”)或方法(例如“ getDeclaredAnnotationMap”)
只有private byte[] annotations
但是该字节数组能做什么?
那么,如何修改方法的注释呢?
编辑:
我创建了:http : //pastebin.com/T2rewcwU
但是,仅编辑此方法实例,如果您取消注释33行代码,则值将重置。
我正在使用 testcontainers.org docker.elastic.co/elasticsearch/elasticsearch-oss:7.3.2
,我想用它来测试我正在更新的插件,但我找不到在测试环境中安装它的方法。
我可以尝试复制里面的文件并安装它
ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:$ELASTIC_VERSION")
String pluginPath = "/usr/share/elasticsearch/$PLUGIN_FILE_NAME"
container.start()
container.copyFileToContainer(MountableFile.forHostPath(new File(PLUGIN_PLUGIN_PATH).toPath()), pluginPath)
ExecResult result = container.execInContainer("bin/elasticsearch-plugin", "install", "file://$pluginPath")
Run Code Online (Sandbox Code Playgroud)
但是容器已经启动并且弹性搜索已经在运行,所以插件不会被加载,所以我需要杀死它并复制它的创建方式,听起来像是很多黑客攻击。有更好的方法来做到这一点吗?
我试图获取页面(HTML),从中读取特殊令牌(每次都不同),然后发送POST请求,但这给了我 java.lang.IllegalStateException: connect in progress
final URL url = new URL("<...>");
String token = null;
final HttpURLConnection con = (HttpURLConnection) url.openConnection();
try (final InputStreamReader isr = new InputStreamReader(con.getInputStream())) {
try (final BufferedReader in = new BufferedReader(isr)) {
String str;
while ((str = in.readLine()) != null) {
if (str.contains("__token")) {
token = str.replace("<...>", "").replace("<...>", "");
System.out.println(token);
break;
}
}
in.close();
}
isr.close();
}
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setDoOutput(true);
try (final DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
final String params = getParams("par1", "par2", …
Run Code Online (Sandbox Code Playgroud)