我正在做一个java代码检查.这是一个函数(片段):
String getValue() {
String res;
StringBuilder strBuilder = new StringBuilder();
// More code here that sets strBuilder
return res = strBuilder.toString();
}
Run Code Online (Sandbox Code Playgroud)
首先是警告不使用res的值.其次我不明白回报.他们为什么不这么做return( strBuilder.toString() ).有某种优势吗?
拥有使用默认方法的接口的动态代理,如何调用默认方法?通过使用像defaultmethod.invoke(this, ...)你这样的东西,只需调用你的代理调用处理程序(这在某种程度上是正确的,因为你没有这个接口的实现类).
我有一个解决方法,使用ASM创建一个实现接口的类,并将此类调用委托给此类的实例.但这不是一个好的解决方案,特别是如果默认方法调用其他接口方法(你得到一个委托人ping-pong).JLS对这个问题惊讶地保持沉默......
这是一个小代码示例:
public class Java8Proxy implements InvocationHandler {
public interface WithDefaultMethod {
void someMethod();
default void someDefaultMethod() {
System.out.println("default method invoked!");
}
}
@Test
public void invokeTest() {
WithDefaultMethod proxy = (WithDefaultMethod) Proxy.newProxyInstance(
WithDefaultMethod.class.getClassLoader(),
new Class<?>[] { WithDefaultMethod.class }, this);
proxy.someDefaultMethod();
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// assuming not knowing the interface before runtime (I wouldn't use a
// proxy, would I?)
// what to do here to …Run Code Online (Sandbox Code Playgroud) 我们有一个黑盒第三方Java程序,它从一个位置获取输入文件并制作PDF.它每次为每个输入将清单文件放在同一位置,这使得我们需要以受控方式提供文件.清单(或.xen/.que)是否仍然存在?不要输入输入文件.
我们的Feed脚本非常罕见(成千上万个文件中的一个)实例没有找到任何内容,提供文件,以及当清单被覆盖且事情不匹配时产生的错误.我写了一个perl脚本,除了将时间打印到百万分之一,在我们关心的目录中输入任何内容,然后打印它.下面你可以看到.xen和.que文件,其中.xen是输入,而.que是它的重命名版本,用于指示处理.
那么我的问题是:如何在94.26493缺少文件?操作系统在重命名时是否隐藏文件?当Feed程序在那一刻查找文件时,我们遇到了问题所以我计划的hack是两次检查文件; 希望足够慢以捕获重命名的任何一端.我还应该指出,一旦有2个文件显示在一行上,那就是feed程序放入另一个文件的位置.它与重命名之前的文件不同.
1421417394.26392/gpfs/fsdd/projects/corr_esch/corr_esch.d.xen
1421417394.26416/gpfs/fsdd/projects/corr_esch/corr_esch.d.xen
1421417394.26442/gpfs/fsdd/projects/corr_esch/corr_esch.d.xen
1421417394.26468/gpfs/fsdd/projects/corr_esch/corr_esch.d.xen
1421417394.26493
1421417394.26907/gpfs/fsdd/projects/corr_esch/corr_esch.d.xen.que_142_1421417394265
1421417394.27426/gpfs/fsdd/projects/corr_esch/corr_esch.d.xen /gpfs/fsdd/projects/corr_esch/corr_esch.d.xen.que_142_1421417394265
1421417394.27456/gpfs/fsdd/projects/corr_esch/corr_esch.d.xen /gpfs/fsdd/projects/corr_esch/corr_esch.d.xen.que_142_1421417394265
1421417394.27486/gpfs/fsdd/projects/corr_esch/corr_esch.d.xen /gpfs/fsdd/projects/corr_esch/corr_esch.d.xen.que_142_1421417394265
1421417394.27528/gpfs/fsdd/projects/corr_esch/corr_esch.d.xen /gpfs/fsdd/projects/corr_esch/corr_esch.d.xen.que_142_1421417394265
Run Code Online (Sandbox Code Playgroud) 通常的做法是在JUnit中使用"test"作为测试方法名称的前缀.但在过去几年中,有些人将其改为前缀"应该".
如果我想在数据库中测试客户创建,我通常会将方法命名为"testCustomerCreation".但是,有些人会将其命名为"shouldCreateCustomer".
当我是项目中唯一的人或项目中的其他人都同意我时,这是很多个人品味.但是当不是这种情况时,会出现一些分歧或不一致的混合物.
我在某处写了一篇名为"testShouldCreateCustomer"的方法的文章,因此他决定放弃"test"前缀.但事实上他并没有在"测试"前缀,他正在使用"testShould"并改为"应该".显然,这并不能说服我.
我个人强烈倾向于坚持使用"test"前缀,因为方法名称通常以不定式形式的动词开头("get","set","add","remove","clear","send","接收","打开","关闭","读取","写","创建","列表","弹出","打印"等,所以是"测试").因此,在方法名称前加上"should"会让我听起来真的很奇怪,看起来不对.
那么,使用"应该"而不是"测试"的真正好理由是什么?有哪些优点和缺点?
我正在编写一个工具,它使用注释处理器根据注释类的方法的返回类型生成源代码.返回类型始终A是定义类型变量的接口的某个子类型(接口或类)T.
interface A<T>{T m();};
Run Code Online (Sandbox Code Playgroud)
我想找到方法m()返回值类型变量的类型参数T.
返回类型由注释处理器表示为javax.lang.model.type.TypeMirror实例.最简单的情况是A<T>直接返回.
@SomeAnnotation
class SomeClass{
A<T> x();
}
Run Code Online (Sandbox Code Playgroud)
要找到的处理器代码T非常简单.(我会在这里投射而不是使用访问者API来保持代码简单.)
DeclaredType type = (DeclaredType) typeMirror;
TypeMirror t = type.getTypeArguments().get(0);
Run Code Online (Sandbox Code Playgroud)
在TypeMirror返回类型的是javax.lang.model.type.DeclaredType和T是第一个类型参数.其结果t是一javax.lang.model.type.TypeVariable对T.同样适用于具体的返回类型A<B>(B某种类型:) interface B{}.结果t是DeclaredType代表B.
其他结果类型开始变得复杂:
interface Subtype<T> extends A<T>{}
interface Concrete extends A<B>{};
interface Multiple<B,T> extends A<T>{}
interface Bounds<T extends B> extends …Run Code Online (Sandbox Code Playgroud) 我按照Facebook教程集成SDK,刚刚在我的.xml文件中添加了登录到Facebook按钮后,我的应用程序将编译,打开(在我的Nexus 4上运行Lollipop,项目针对kitkat +),然后崩溃.我正在开发完全更新的Android Studio.
logcat的:
02-07 22:48:24.340 1563-1600/com.example.prachi.mapsapplication E/AndroidRuntime? FATAL EXCEPTION: AsyncTask #1
Process: com.example.prachi.mapsapplication, PID: 1563
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference
at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:746)
at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:774)
at com.facebook.internal.Utility.queryAppSettings(Utility.java:673)
at com.facebook.widget.LoginButton$1.doInBackground(LoginButton.java:678)
at com.facebook.widget.LoginButton$1.doInBackground(LoginButton.java:675)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Run Code Online (Sandbox Code Playgroud)
.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" …Run Code Online (Sandbox Code Playgroud) 我有一个JFrame使用扩展的类的子类JPanel
public class HelloWorld extends JPanel implements KeyListener
Run Code Online (Sandbox Code Playgroud)
我添加了一个HelloWorld框架的对象- app.add(helloWorld);.现在,当我按任何键盘键时,非KeyListener方法被调用,似乎helloWorld没有窗口焦点.我也试过调用helloWorld.requestFocusInWindow();但仍然没有回应.
如何让它响应按键?
我正在使用Spring Batch(3.0.1.RELEASE)/ JPA和HSQLBD服务器数据库.我需要浏览整个表(使用分页)和更新项目(逐个).所以我使用了jpaPagingItemReader.但是当我运行该作业时,我可以看到跳过了一些行,并且跳过的行数等于页面大小.因为,如果我的表有12行而jpaPagingItemReader.pagesize = 3,则作业将显示:第1,2,3行,然后第7,8,9行(所以跳过第4,5,6行)...你能告诉我是什么我的代码/配置有问题,或者这可能是HSQLDB分页的问题?以下是我的代码:
[编辑]:问题在于我的ItemProcessor执行对POJO实体的修改.由于JPAPagingItemReader在每次读取之间进行刷新,因此实体会更新((这就是我想要的).但似乎游标分页也会增加(如日志中所示:行ID 4,5和6已经过跳过).我该如何处理这个问题?
@Configuration
@EnableBatchProcessing(modular=true)
public class AppBatchConfig {
@Inject
private InfrastructureConfiguration infrastructureConfiguration;
@Inject private JobBuilderFactory jobs;
@Inject private StepBuilderFactory steps;
@Bean public Job job() {
return jobs.get("Myjob1").start(step1()).build();
}
@Bean public Step step1() {
return steps.get("step1")
.<SNUserPerCampaign, SNUserPerCampaign> chunk(0)
.reader(reader()).processor(processor()).build();
}
@Bean(destroyMethod = "")
@JobScope
public ItemStreamReader<SNUserPerCampaign> reader() String trigramme) {
JpaPagingItemReader reader = new JpaPagingItemReader();
reader.setEntityManagerFactory(infrastructureConfiguration.getEntityManagerFactory());
reader.setQueryString("select t from SNUserPerCampaign t where t.isactive=true");
reader.setPageSize(3));
return reader;
}
@Bean @JobScope
public ItemProcessor<SNUserPerCampaign, SNUserPerCampaign> processor() { …Run Code Online (Sandbox Code Playgroud) 我知道乐观和悲观锁定是什么,但是当你编写java代码时,你是如何做到的?假设我使用Oracle和Java,我在JDBC中有任何方法可以帮助我做到这一点吗?我该如何配置这个东西?任何指针将不胜感激.
我有关于jpa查询的问题.有两个表,即Post表和Tag表Post和Tag之间有很多关系
现在我想编写一个查询,以便在选择多个标签时,应选择与这些标签关联的所有帖子.例如,
post1 has tags friends and motivation
post2 has tags motivation and pune
post3 has tag boxing
Run Code Online (Sandbox Code Playgroud)
如果选择标签friends和pune,那么如果选择标签装箱则应检索post1和post2,那么如果选择标签装箱和动机则应检索第3位,则应检索所有三个位置.
我试过跟踪的事情
SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE p.tags IN :tags
Run Code Online (Sandbox Code Playgroud)
但它给出了验证器错误
The state field path 'p.tags' cannot be resolved to a collection type.
Run Code Online (Sandbox Code Playgroud)
如果我这样试试
SELECT DISTINCT p FROM Post p JOIN p.tags tags WHERE p.tags = :tags
Run Code Online (Sandbox Code Playgroud)
然后它符合要求但在传递标签列表后会出错
java.lang.IllegalArgumentException: You have attempted to set a value of type class java.util.ArrayList for parameter tags with expected type of …Run Code Online (Sandbox Code Playgroud)