小编Lon*_*zak的帖子

使用哪个@NonNull Java注释

什么是最好的 'NonNull'注释?

意义上的"最佳"

  • 标准方式,例如未来证明(例如标准jdk支持等)
  • 支持IDE(在java doc中显示以指示开发人员的使用情况)
  • 由findbugs等静态分析工具提供支持
  • 支持运行时分析

以下是世界目前的样子 - 任何进一步的见解都值得赞赏:

  • javax.validation.constraints.NotNull(Docs)
    + javax包因此看起来是未来的
    - JEE的一部分不是 JSE.在JSE中需要导入额外的库.
    - 静态分析工具不支持(仅限运行时验证)

  • edu.umd.cs.findbugs.annotations.NonNull(docs)
    - 外部库而不是javax
    - 不推荐使用,因为findbugs版本3.X
    +用于静态分析(由findbugs和Sonar提供)

  • javax.annotation.Nonnull(docs)
    +用于静态分析(在findbugs中)
    - JSR-305处于休眠/死亡/未知状态,如fb邮件列表所示.提交人Bill Pugh,即使直接被问到,多年来也没有对该州进行评论......

  • org.eclipse.jdt.annotation_2.0.0(文档,有趣的演示文稿)
    +用于静态分析(虽然在eclipse中不在findbugs中)
    - eclipse专有(没有尝试独立使用它们)

  • org.jetbrains.annotations.NotNull(docs)
    +用于静态分析(虽然不是在findbugs
    中的IntelliJ )- IntelliJ专有(但也可以公开作为jar)

  • lombok.NonNull(docs)
    +用于控制代码生成
    - 专有注释

  • android.support.annotation.NonNull(docs)
    + android …

java eclipse annotations intellij-idea nullpointerexception

64
推荐指数
1
解决办法
1万
查看次数

来自一个maven项目的多个程序集

我们有不同的java源代码"项目".3个项目完全相同(fatclient,相同的依赖项等) - 只有另一个主类必须被调用.

今天我们有一个带有主类的基础项目:

<project>
    <groupId>net.company.BaseTool</groupId>
    <artifactId>BaseTool</artifactId>
    <version>1.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>BaseTool</name>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>net.company.BaseTool</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

以及取决于基础项目的其他项目

<project>
    <groupId>net.company.AnotherTool</groupId>
    <artifactId>AnotherTool</artifactId>
    <version>1.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>AnotherTool</name>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>net.company.AnotherTool</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>net.company.BaseTool</groupId>
            <artifactId>BaseTool</artifactId>
            <version>1.1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

我们这样做,因为我们需要简单的双击可启动应用程序.

但我不想为每个应用程序创建一个额外的Java项目.

我的问题是:是否可以从一个项目创建多个程序集?如果是的话,应该怎么做.

这是解决方案

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.company.toolbox</groupId>
    <artifactId>toolbox</artifactId>
    <version>1.1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>toolbox</name>
    <url>http://wiki.company.my/toolbox</url>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <finalName>toolbox</finalName> …
Run Code Online (Sandbox Code Playgroud)

java maven maven-assembly-plugin

32
推荐指数
1
解决办法
2万
查看次数

如何在IntelliJ中调试时修改List值

我需要在调试应用程序期间更改变量.到目前为止,它只是可以直接设置的基本变量.现在我需要清除一个数组,以便isEmpty()返回true;

ArrayList<String> someList = new ArrayList<String>;
someList.add("1");
...
if(someList.isEmpty()){ //break point
//need to enter here
}
Run Code Online (Sandbox Code Playgroud)

在intellij调试器中,我看到:

someList={ArrayList@4271} size=1
Run Code Online (Sandbox Code Playgroud)

我使用了调试器的'setValue'方法并尝试:new ArrayList<String>()someList = new ArrayList<String>()

结果

someList={ArrayList@4339} size=0
Run Code Online (Sandbox Code Playgroud)

但是,如果我继续,则在调用isEmpty()时会出现NullPointerException.所以我的问题是:如何在不获取NPE的情况下注入空的ArrayList?

NPe的文本是: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.isEmpty()' on a null object reference

debugging intellij-idea android-studio intellij-14 android-studio-2.0

17
推荐指数
2
解决办法
8620
查看次数

你有没有比较过静态分析工具Klocwork和Findbugs?

我们使用Klocwork作为静态分析工具.

Klocwork是一种商业工具,具有许多优点,但也存在假阳性等局限性.

我想知道谁曾将Klocwork与Findbugs等其他开源工具进行比较.

通常,已知商业工具比开源工具更可靠.

但我认为Klocwork在特定业务领域也有一些可靠的问题,比如android.

你能否说Klocwork优于其他开源工具,尤其是Findbugs在误报和漏报方面?

code-analysis static-analysis findbugs klocwork

11
推荐指数
1
解决办法
1万
查看次数

如何使用ExpectedException规则在一个测试中测试多个异常?

对junit的ExpectedException规则的使用有疑问:

正如这里建议的那样:junit ExpectedException 从junit 4.7开始的规则可以测试这样的异常(这比@Test(expected = Exception.class)要好得多):

@Rule
public ExpectedException exception = ExpectedException.none();

@Test
public void testFailuresOfClass() {
 Foo foo = new Foo();
 exception.expect(Exception.class);
 foo.doStuff();
}
Run Code Online (Sandbox Code Playgroud)

现在我需要在一个测试方法中测试几个异常,并在运行以下测试后得到一个绿色条,因此认为每个测试都通过了.

@Test
public void testFailuresOfClass() {
 Foo foo = new Foo();

 exception.expect(IndexOutOfBoundsException.class);
 foo.doStuff();

 //this is not tested anymore and if the first passes everything looks fine
 exception.expect(NullPointerException.class);
 foo.doStuff(null);

 exception.expect(MyOwnException.class);
 foo.doStuff(null,"");

 exception.expect(DomainException.class);
 foo.doOtherStuff();
}
Run Code Online (Sandbox Code Playgroud)

但是过了一会儿,我意识到第一次检查通过后,测试方法就会退出.至少可以说这是模棱两可的.在junit 3中这很容易实现......所以这是我的问题:

如何使用ExpectedException规则在一个测试中测试多个异常?

java junit unit-testing junit4 expected-exception

7
推荐指数
1
解决办法
502
查看次数

CDI 容器如何适应 Java EE 服务器的 EJB/Web 容器?

Java EE 服务器具有不同的容器,如 EJB 或 Web 容器: 在此处输入图片说明

但是,我发现有关如何集成 CDI 组件的不同信息。在文学中,有人谈到 CDI 容器(“ CDI 容器为您自动管理范围内的所有 bean ”),但其他人将其定义为 Java EE 容器提供的上下文服务。在 stackoverflow 上,甚至还有一个名为“IOC-Container”的标签。那么,如果它是一个容器,它是 EJB/Web 容器的容器部分/内部吗?

因此,如果我绘制 Java EE 服务器及其组件和容器的图片(如上图所示),CDI 是如何适应的?它有自己的容器“矩形”还是某些 EJB/Web 容器的一部分?你会如何在建筑设计中绘制它,你会如何解释/描述它?

ioc-container cdi jakarta-ee

7
推荐指数
1
解决办法
1483
查看次数

Arquillian在WAS远程容器和@PersistenceContext上

对于我们的集成测试,我想使用Arquillian并遵循"入门教程 " 中的所有步骤.由于我有一个现有的应用程序,设置起来并不是那么简单,但我设法解决了大部分问题.现在我得到一个NPE异常,表明CDI无法正常工作.根据WAS 8.5远程容器文档,我意识到@PersistenceContext不支持.这是不幸的,因为那时我可能不能使用Arquillian或者还有其他可能吗?

简化的代码结构:

class MyTest{ 

  @Deployment
  public static EnterpriseArchive createDeployment() {
  ...
  }

  @Inject
  public Service2Inject service;

  @Test
  public void testService() {
    //Assert.assertNotNull("Dependency injection failed!",service);
    String s = service.getSomeString("10");
    ...
  }
}


public class Service2Inject implements SomeInterface {

  @Inject
  private SomeOtherService serviceOther;

  @Override
  public String getSomeString(String id) {
    String testString = serviceOther.getSomeOtherStrings();
    ...
  }
}

public class SomeOtherService implements SomeInterface2 {

  @Inject
  private EntityManager entityManager;

  @Override
  public List<String> getSomeOtherStrings(String Id) {
    Query query …
Run Code Online (Sandbox Code Playgroud)

integration-testing java-ee cdi jboss-arquillian

7
推荐指数
1
解决办法
313
查看次数

Java keytool /使用java生成密钥的安全性(一般)

我们使用与java安装捆绑在一起的keytool来生成用于执行非对称RSA加密的密钥.根据最近发生的事件,有人问我在java keytool的引擎下发生了什么.特别是关于结果数字的随机性.(例如"为什么没有任何随机用户输入像鼠标移动或键盘输入?"

那么创建密钥的java keytool的"随机源"是什么?

我自己做了一个快速的研究,但我发现的唯一信息是2000 年的帖子:

  • keytool.exe使用SecureRandom作为其随机数的基础.
  • SecureRandom的Sun提供商遵循IEEE P1363标准,
  • Sun SecureRandom提供商遵守NIST的FIPS PUB 140-1第4.11节.
  • SecureRandom的Sun提供程序将其他熵源与线程争用进程的结果混合在一起.其中包括当前时间,VM内存使用状态,系统属性和文件系统活动.
  • 在没有JIT的情况下,该算法可能表现不佳,因此我们正在考虑提供替代供应商,该供应商将利用对熵收集设备(例如/ dev/random或Pentium III热噪声RNG)的平台特定支持.

但是这已经回到了2K,所以你们中的某些人可能会对此有所了解并提供上述更新(在Java7中有所不同吗?).根据你的回答,如果你建议改用另一个像bouncycastle这样的提供商,我会被强调...

更新:我现在假设keytool使用java.security.SecureRandom(因此是默认提供程序)作为其随机数的基础.我发现了另一篇有趣的文章,它指出了控制SecureRandom API JAVA_HOME/lib/security/java.security配置的文件.

在那里它陈述如下:

选择SecureRandom的种子数据源.默认情况下,尝试使用securerandom.source属性指定的熵收集设备.如果在访问URL时发生异常,则使用传统的系统/线程活动算法.在Solaris和Linux系统上,如果指定了file:/ dev/urandom并且它存在,则默认情况下会激活特殊的SecureRandom实现.这个"NativePRNG"直接从/ dev/urandom读取随机字节.在Windows系统上,URL文件:/ dev/random和file:/ dev/urandom允许使用Microsoft CryptoAPI种子功能.

securerandom.source =文件是:/ dev/urandom的

由于我们在Windows系统上,我假设使用了Microsoft CryptoAPI.自从使用Win7以来,它就是CNG(CryptoAPI Next Generation).有没有人知道什么是'使用Microsoft CryptoAPI种子功能'.手段?最可能的方法似乎是:CryptGenRandom function

更新:Oracle似乎改进了Java 8的一些问题.

java security random encryption keytool

5
推荐指数
2
解决办法
3833
查看次数

性能iText vs.PdfBox

我正在尝试将pdf(我最喜欢的书籍Effective Java,如果它的问题)转换为文本,我检查了iText和Apache PdfBox.我发现性能有很大的不同:使用iText需要2:521,使用PdfBox:6:117.如果我的代码为PdfBOx

PDFTextStripper stripper = new PDFTextStripper();
BUFFER.append(stripper.getText(PDDocument.load(pdf)));
Run Code Online (Sandbox Code Playgroud)

这是针对iText的

PdfReader reader = new PdfReader(pdf);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
  BUFFER.append(PdfTextExtractor.getTextFromPage(reader, i));
}
Run Code Online (Sandbox Code Playgroud)

我的问题是性能取决于什么,有没有办法让PdfBox更快?或者只使用iText?你能解释一下策略如何影响绩效吗?

java performance itext pdfbox

5
推荐指数
1
解决办法
2万
查看次数

更新到wildfly 18后直接缓冲内存OutOfMemoryError

将环境从 更新到 后,Wildfly 13我们Wildfly 18.0.1经历了

A channel event listener threw an exception: java.lang.OutOfMemoryError: Direct buffer memory
at java.base/java.nio.Bits.reserveMemory(Bits.java:175)
at java.base/java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:118)
at java.base/java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:317)
at org.jboss.xnio@3.7.3.Final//org.xnio.BufferAllocator$2.allocate(BufferAllocator.java:57)
at org.jboss.xnio@3.7.3.Final//org.xnio.BufferAllocator$2.allocate(BufferAllocator.java:55)
at org.jboss.xnio@3.7.3.Final//org.xnio.ByteBufferSlicePool.allocateSlices(ByteBufferSlicePool.java:162)
at org.jboss.xnio@3.7.3.Final//org.xnio.ByteBufferSlicePool.allocate(ByteBufferSlicePool.java:149)
at io.undertow.core@2.0.27.Final//io.undertow.server.XnioByteBufferPool.allocate(XnioByteBufferPool.java:53)
at io.undertow.core@2.0.27.Final//io.undertow.server.protocol.http.HttpReadListener.handleEventWithNoRunningRequest(HttpReadListener.java:147)
at io.undertow.core@2.0.27.Final//io.undertow.server.protocol.http.HttpReadListener.handleEvent(HttpReadListener.java:136)
at io.undertow.core@2.0.27.Final//io.undertow.server.protocol.http.HttpReadListener.handleEvent(HttpReadListener.java:59)
at org.jboss.xnio@3.7.3.Final//org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92)
at org.jboss.xnio@3.7.3.Final//org.xnio.conduits.ReadReadyHandler$ChannelListenerHandler.readReady(ReadReadyHandler.java:66)
at org.jboss.xnio.nio@3.7.3.Final//org.xnio.nio.NioSocketConduit.handleReady(NioSocketConduit.java:89)
at org.jboss.xnio.nio@3.7.3.Final//org.xnio.nio.WorkerThread.run(WorkerThread.java:591)
Run Code Online (Sandbox Code Playgroud)

应用程序方面没有任何改变。我查看了缓冲池,似乎有些资源没有释放。我触发了几次手动 GC,但几乎什么也没发生。(正常运行时间2小时)

缓冲池稳步增长

之前在旧配置中它看起来像这样(正常运行时间> 250小时):

正确释放的缓冲池

现在我做了很多研究,我能找到的最接近的就是这篇关于 SO 的文章。然而,这是与 websockets 结合使用的,但没有使用websockets。我读了几篇(好)文章1、2、3、4、5、6 观看了有关主题视频我尝试了以下方法,但没有任何效果:

  1. OutOfMemoryError 发生在 5GB 处,因为堆是 5GB …

java out-of-memory wildfly undertow java-11

5
推荐指数
1
解决办法
4697
查看次数