我有一个简单的注释处理器,需要从与注释类相同的项目中读取配置文件.结构示例:
- myproject
- src
- main
- java
- my.package.SourceFile
- resources
- config.json
Run Code Online (Sandbox Code Playgroud)
在注释处理器中,我尝试读取文件:
FileObject resource = processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", "config.json");
Run Code Online (Sandbox Code Playgroud)
但它会抛出FileNotFoundException.我也尝试过其他路径,例如../resources/config.json(抛出Invalid relative name: ../resources/config.json).我尝试将配置文件放入src/main/java(甚至src/main/java/my/package),我不喜欢,但仍然会抛出FileNotFoundException.
如果我可以filer.getResource()告诉我它在哪里,它会有所帮助.为了找到答案,我尝试生成一个文件:
filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "dummy");
Run Code Online (Sandbox Code Playgroud)
产生于myproject/build/classes/main/dummy.不幸的是,我无法生成SOURCE_PATH,因此无法找到它.
我有一个标签,只显示当天某小时的分钟部分.我想让它读取"PM"或"00",具体取决于时区.
我目前的解决方案只使用Joda's DateTimeFormat.shortTime().withLocale(myLocale)并从结果中取出最后2个字符.这有效,但感觉不对.
相反,我想检查给定的语言环境是否使用AM/PM表示法,但我还没有找到API调用来向我提供该信息.它是否存在,无论是在JodaTime还是Java库本身?例如,我想写一些类似的东西usesAmPm(myLocale)) ? DateTimeFormat.forPattern("a") : DateTimeFormat.forPattern("mm")
我使用Guice绑定有以下代码:
public class MyApplication {
public static void main(String[] args) {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Foo.class).annotatedWith(Names.named("first")).toInstance(new Foo("firstFoo"));
bind(Foo.class).annotatedWith(Names.named("second")).toInstance(new Foo("secondFoo"));
bind(Bar.class).to(BarImpl.class);
bind(MyApplication.class).asEagerSingleton();
}
});
}
private @Named("first") Bar first;
private @Named("second") Bar second;
static @Value class Foo { String name; }
static interface Bar {}
static class BarImpl implements Bar {
@Inject @Named Foo foo;
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试为我的应用程序中注入的Bar两个命名Foos 获取一个对象.基本上,它应该以某种方式将@Namedon Foo与on on 连接起来Bar.我尝试了几种解决方案,从@Named一切到写自定义Provider.后者不起作用,因为我无法访问@Named提供程序中的注释值.我认为解决方案就在某个方面 …
我正在尝试将子注入器的 Guice 功能与 Multibinders 结合起来。例如:
public class Test {
public static void main(String[] args) {
Guice.createInjector(new FirstModule(), new SecondModule()); // works perfectly, returns set with 2 elements
Guice.createInjector(new FirstModule()).createChildInjector(new SecondModule()); // fails: A binding to java.util.Set<Test$MyInterface> was already configured at Test$FirstModule.configure(Test.java:15).
}
private static class FirstModule extends AbstractModule {
@Override
protected void configure() {
Multibinder.newSetBinder(binder(), MyInterface.class).addBinding().to(FirstImplementation.class);
}
}
private static class SecondModule extends AbstractModule {
@Override
protected void configure() {
Multibinder.newSetBinder(binder(), MyInterface.class).addBinding().to(SecondImplementation.class);
}
}
private static interface MyInterface {}
private …Run Code Online (Sandbox Code Playgroud) 我正在尝试检测XHR在混合内容上失败。看起来不同的浏览器具有不同的实现:
var xhr = new XMLHttpRequest();
try {
xhr.open('http://otherdomain/');
} catch (err) {
console.log(err); // IE10 hits this one
}
try {
xhr.send(); // Chrome fails here, but doesn't throw an error
} catch (err) {
console.log(err); // No browser I've tried hits this one
}
Run Code Online (Sandbox Code Playgroud)
我不想使用自动检测(xhr.open('//otherdomain')),因为目标可能不支持http或https。我只是想知道呼叫失败,因此可以在页面中显示错误。是否有可能为所有浏览器正确处理?
我有这两个Typescript类:
class Base {
value: string;
lambdaExample = () => {
this.value = 'one';
}
methodExample() {
this.value = 'two';
}
}
class Child extends Base {
lambdaExample = () => {
super.lambdaExample(); // Error, because I've overwritten (instead of overridden) the method
this.value = 'three'; // ok
}
methodExample() => {
super.methodExample(); // ok
this.value = 'four'; // Error: this refers to window, not to the actual this
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样写我的这样一种方式,方法this的引用是可靠的,而且我可以覆盖的方法和从父类打电话给他们?
我想使用JavaPoet生成一个类型为literal的注释作为值.例如:
@AutoService(MyService.class)
public class GeneratedClass implements MyService {
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了所有我能想到的选项,但都没有用:
TypeSpec.classBuilder("GeneratedClass")
.addModifiers(Modifier.PUBLIC)
.addSuperinterface(MyService.class)
.addAnnotation(
AnnotationSpec.builder(AutoService.class).addMember(
"value", "$L", MyService.class
).build()
)
Run Code Online (Sandbox Code Playgroud)
使用$L生成interface MyService
使用$T生成my.package.MyService,这是接近但错过了.class部分.
使用$N给出错误:expected name but was my.package.MyService
如何将其生MyService.class成为注释值?
我有一个多模块 Gradle 项目。我希望它能够正常编译并执行所有其他任务。但对于单元测试,我希望它运行所有测试,而不是在早期项目中的一个测试失败后立即停止。
我尝试过添加
buildscript {
gradle.startParameter.continueOnFailure = true
}
Run Code Online (Sandbox Code Playgroud)
这适用于测试,但如果出现失败,也可以使编译继续。那不行。
我可以将 Gradle 配置为仅针对测试任务继续吗?
我想在我的日志文件中有一个指示器,用于应用程序记录某些内容。这意味着一个变量对于整个应用程序是相同的,但在应用程序的实例之间是不同的。我尝试为此使用 MDC,希望如果我直接在 main 方法中设置它,该变量将被新生成的线程接管。这曾经或多或少地使用 log4j 工作,但它根本不适用于 logback。我知道您可以在日志文件中显示系统变量,但可能有多个应用程序实例在同一台机器上运行,因此无法正常工作。那么,我应该如何将此变量发送到我的日志文件?
我有一个带有文本字段和tsvector包含该字段的搜索索引的表:
CREATE TABLE test (pk bigint, value text, tsv tsvector);
Run Code Online (Sandbox Code Playgroud)
如何创建一个单一的tsvector,它是满足某些条件的所有行的向量的组合?
SELECT value FROM test
WHERE combine_my_vectors(SELECT tsv FROM test WHERE pk IN (some list))
@@ to_tsquery('search me');
Run Code Online (Sandbox Code Playgroud)
我知道tsvectors 可以与 组合||,但这似乎在这里不可能。我有什么用combine_my_vectors?
我希望避免value先组合文本字段,然后再创建tsvector文本字段。
java ×7
guice ×2
cors ×1
gradle ×1
javapoet ×1
javascript ×1
jodatime ×1
localization ×1
logback ×1
postgresql ×1
typescript ×1
unit-testing ×1