Skip to content

Commit

Permalink
Java stacktraces
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit committed May 25, 2024
1 parent 76bb26d commit 795fb12
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 0 deletions.
Empty file.
48 changes: 48 additions & 0 deletions java-exception-stacktraces/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<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>com.pivovarit</groupId>
<version>1.0</version>
<artifactId>java-exception-stacktraces</artifactId>

<name>java-exception-stacktraces</name>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.22.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.pivovarit.exception;

import java.util.Collections;
import java.util.IdentityHashMap;

class ExceptionCacheExample {

public static void main(String[] args) {
var exceptions = Collections.newSetFromMap(new IdentityHashMap<>());

String foo = null;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
try {
foo.toUpperCase();
} catch (NullPointerException e) {
exceptions.add(e);
}
}

System.out.println(exceptions.size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.pivovarit.exception;

import java.util.Arrays;

class StacktraceDropExample {

public static void main(String[] args) {
NullPointerException previous = null;

String foo = null;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
try {
foo.toUpperCase();
} catch (NullPointerException e) {
if (e.getStackTrace().length == 0) {
System.out.printf("Stacktrace dropped at iteration %d%n", i);
if (previous != null) {
System.out.printf("Last stacktrace: %s%n",
Arrays.toString(previous.getStackTrace()));
}
System.out.printf("New stacktrace: %s%n",
Arrays.toString(e.getStackTrace()));
return;
}
previous = e;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.pivovarit.exception;

class StaticStacklessExceptionExample {

private static final NullPointerException NULL_POINTER_EXCEPTION = new NullPointerException();

static {
NULL_POINTER_EXCEPTION.setStackTrace(new StackTraceElement[0]);
}

public static void main(String[] args) {
throw NULL_POINTER_EXCEPTION;
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<module>kotlin-collections</module>
<module>hamming-error-correction</module>
<module>kotlin-type-inference</module>
<module>java-exception-stacktraces</module>
<module>java-sneaky-throws-lambda</module>
<module>java-priorityqueue-stream-order</module>
<module>java-stream-mapmulti</module>
Expand Down

0 comments on commit 795fb12

Please sign in to comment.