fix conflicts
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
@@ -0,0 +1,34 @@
|
||||
=========
|
||||
|
||||
## Core Java 9 Examples
|
||||
|
||||
[Java 9 New Features](http://www.baeldung.com/new-java-9)
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Java 9 New Features](https://www.baeldung.com/new-java-9)
|
||||
- [New Stream Collectors in Java 9](http://www.baeldung.com/java9-stream-collectors)
|
||||
- [Introduction to Project Jigsaw](http://www.baeldung.com/project-jigsaw-java-modularity)
|
||||
- [Java 9 Variable Handles Demystified](http://www.baeldung.com/java-variable-handles)
|
||||
- [Exploring the New HTTP Client in Java 9 and 11](http://www.baeldung.com/java-9-http-client)
|
||||
- [Method Handles in Java](http://www.baeldung.com/java-method-handles)
|
||||
- [Introduction to Chronicle Queue](http://www.baeldung.com/java-chronicle-queue)
|
||||
- [Optional orElse Optional](http://www.baeldung.com/java-optional-or-else-optional)
|
||||
- [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range)
|
||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
||||
- [Immutable Set in Java](https://www.baeldung.com/java-immutable-set)
|
||||
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
|
||||
- [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation)
|
||||
- [Java 9 Process API Improvements](https://www.baeldung.com/java-9-process-api)
|
||||
- [Guide to java.lang.Process API](https://www.baeldung.com/java-process-api)
|
||||
- [Java 9 java.util.Objects Additions](https://www.baeldung.com/java-9-objects-new)
|
||||
- [Java 9 Reactive Streams](https://www.baeldung.com/java-9-reactive-streams)
|
||||
- [Java 9 Optional API Additions](https://www.baeldung.com/java-9-optional)
|
||||
- [Java 9 CompletableFuture API Improvements](https://www.baeldung.com/java-9-completablefuture)
|
||||
- [Introduction to Java 9 StackWalking API](https://www.baeldung.com/java-9-stackwalking-api)
|
||||
- [Java 9 Convenience Factory Methods for Collections](https://www.baeldung.com/java-9-collections-factory-methods)
|
||||
- [Java 9 Stream API Improvements](https://www.baeldung.com/java-9-stream-api)
|
||||
- [A Guide to Java 9 Modularity](https://www.baeldung.com/java-9-modularity)
|
||||
- [Java 9 Platform Module API](https://www.baeldung.com/java-9-module-api)
|
||||
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
cd src/main/java
|
||||
javac com/baeldung/java9/aot/JaotCompilation.java
|
||||
jaotc --output jaotCompilation.so com/baeldung/java9/aot/JaotCompilation.class
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
javac --module-path mods -d mods/com.baeldung.httpclient^
|
||||
src/modules/com.baeldung.httpclient/module-info.java^
|
||||
src/modules/com.baeldung.httpclient/com/baeldung/httpclient/HttpClientExample.java
|
||||
@@ -0,0 +1 @@
|
||||
javac -d mods --module-source-path src/modules $(find src/modules -name "*.java")
|
||||
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env bash
|
||||
javac -d outDir --module-source-path src/simple-modules $(find src/simple-modules -name "*.java")
|
||||
@@ -0,0 +1,3 @@
|
||||
javac --module-path mods -d mods/com.baeldung.student.client^
|
||||
src/modules/com.baeldung.student.client/module-info.java^
|
||||
src/modules/com.baeldung.student.client/com/baeldung/student/client/StudentClient.java
|
||||
@@ -0,0 +1,2 @@
|
||||
javac -d mods/com.baeldung.student.model src/modules/com.baeldung.student.model/module-info.java^
|
||||
src/modules/com.baeldung.student.model/com/baeldung/student/model/Student.java
|
||||
@@ -0,0 +1,3 @@
|
||||
javac --module-path mods -d mods/com.baeldung.student.service.dbimpl^
|
||||
src/modules/com.baeldung.student.service.dbimpl/module-info.java^
|
||||
src/modules/com.baeldung.student.service.dbimpl/com/baeldung/student/service/dbimpl/StudentDbService.java
|
||||
@@ -0,0 +1,3 @@
|
||||
javac --module-path mods -d mods/com.baeldung.student.service^
|
||||
src/modules/com.baeldung.student.service/module-info.java^
|
||||
src/modules/com.baeldung.student.service/com/baeldung/student/service/StudentService.java
|
||||
@@ -0,0 +1,15 @@
|
||||
# compile logging module
|
||||
# javac --module-path mods -d mods/com.baeldung.logging src/modules/com.baeldung.logging/module-info.java src/modules/com.baeldung.logging/com/baeldung/logging/*.java
|
||||
|
||||
# compile logging slf4j module
|
||||
javac --module-path mods -d mods/com.baeldung.logging.slf4j src/modules/com.baeldung.logging.slf4j/module-info.java src/modules/com.baeldung.logging.slf4j/com/baeldung/logging/slf4j/*.java
|
||||
|
||||
|
||||
# compile logging main app module
|
||||
javac --module-path mods -d mods/com.baeldung.logging.app src/modules/com.baeldung.logging.app/module-info.java src/modules/com.baeldung.logging.app/com/baeldung/logging/app/*.java
|
||||
|
||||
# run logging main app
|
||||
# java --module-path mods -m com.baeldung.logging.app/com.baeldung.logging.app.MainApp
|
||||
|
||||
# run looging main app using logback
|
||||
java --module-path mods -Dlogback.configurationFile=mods/logback.xml -m com.baeldung.logging.app/com.baeldung.logging.app.MainApp
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<pattern>
|
||||
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -- %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root>
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,73 @@
|
||||
<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>
|
||||
<artifactId>core-java-9</artifactId>
|
||||
<version>0.2-SNAPSHOT</version>
|
||||
<name>core-java-9</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${awaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>core-java-9</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>apache.snapshots</id>
|
||||
<url>http://repository.apache.org/snapshots/</url>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.10.0</assertj.version>
|
||||
<junit.platform.version>1.2.0</junit.platform.version>
|
||||
<awaitility.version>1.7.0</awaitility.version>
|
||||
<maven.compiler.source>1.9</maven.compiler.source>
|
||||
<maven.compiler.target>1.9</maven.compiler.target>
|
||||
<guava.version>25.1-jre</guava.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env bash
|
||||
cd src/main/java
|
||||
java -XX:AOTLibrary=./jaotCompilation.so com/baeldung/java9/aot/JaotCompilation
|
||||
@@ -0,0 +1 @@
|
||||
java --module-path mods -m com.baeldung.httpclient/com.baeldung.httpclient.HttpClientExample
|
||||
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/env bash
|
||||
java --module-path outDir -m main.app/com.baeldung.modules.main.MainApp
|
||||
@@ -0,0 +1 @@
|
||||
java --module-path mods -m com.baeldung.student.client/com.baeldung.student.client.StudentClient
|
||||
@@ -0,0 +1 @@
|
||||
java --module-path mods -m com.baeldung.student.client/com.baeldung.student.client.StudentClient
|
||||
@@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.java9.aot;
|
||||
|
||||
public class JaotCompilation {
|
||||
|
||||
public static void main(String[] argv) {
|
||||
System.out.println(message());
|
||||
}
|
||||
|
||||
public static String message() {
|
||||
return "The JAOT compiler says 'Hello'";
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.java9.language;
|
||||
|
||||
public interface PrivateInterface {
|
||||
|
||||
private static String staticPrivate() {
|
||||
return "static private";
|
||||
}
|
||||
|
||||
private String instancePrivate() {
|
||||
return "instance private";
|
||||
}
|
||||
|
||||
public default void check() {
|
||||
String result = staticPrivate();
|
||||
if (!result.equals("static private"))
|
||||
throw new AssertionError("Incorrect result for static private interface method");
|
||||
PrivateInterface pvt = new PrivateInterface() {
|
||||
};
|
||||
result = pvt.instancePrivate();
|
||||
if (!result.equals("instance private"))
|
||||
throw new AssertionError("Incorrect result for instance private interface method");
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.java9.language.stream;
|
||||
|
||||
import static java.util.stream.Collectors.filtering;
|
||||
import static java.util.stream.Collectors.groupingBy;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class StreamsGroupingCollectionFilter {
|
||||
|
||||
static public Map<Integer, List<Integer>> findEvenNumbersAfterGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
|
||||
Function<Integer, Integer> getQuantityOfDigits = item -> (int) Math.log10(item) + 1;
|
||||
|
||||
return baseCollection.stream()
|
||||
.collect(groupingBy(getQuantityOfDigits, filtering(item -> item % 2 == 0, toList())));
|
||||
}
|
||||
|
||||
static public Map<Integer, List<Integer>> findEvenNumbersBeforeGroupingByQuantityOfDigits(Collection<Integer> baseCollection) {
|
||||
|
||||
return baseCollection.stream()
|
||||
.filter(item -> item % 2 == 0)
|
||||
.collect(groupingBy(item -> (int) Math.log10(item) + 1, toList()));
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.java9.maps.initialize;
|
||||
|
||||
import java.util.AbstractMap;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MapsInitializer {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void createMapWithMapOf() {
|
||||
Map<String, String> emptyMap = Map.of();
|
||||
Map<String, String> singletonMap = Map.of("key1", "value");
|
||||
Map<String, String> map = Map.of("key1","value1", "key2", "value2");
|
||||
}
|
||||
|
||||
public void createMapWithMapEntries() {
|
||||
Map<String, String> map = Map.ofEntries(
|
||||
new AbstractMap.SimpleEntry<String, String>("name", "John"),
|
||||
new AbstractMap.SimpleEntry<String, String>("city", "budapest"),
|
||||
new AbstractMap.SimpleEntry<String, String>("zip", "000000"),
|
||||
new AbstractMap.SimpleEntry<String, String>("home", "1231231231")
|
||||
);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public void createMutableMaps() {
|
||||
Map<String, String> map = new HashMap<String, String> (Map.of("key1","value1", "key2", "value2"));
|
||||
Map<String, String> map2 = new HashMap<String, String> ( Map.ofEntries(
|
||||
new AbstractMap.SimpleEntry<String, String>("name", "John"),
|
||||
new AbstractMap.SimpleEntry<String, String>("city", "budapest")));
|
||||
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.java9.methodhandles;
|
||||
|
||||
public class Book {
|
||||
|
||||
String id;
|
||||
String title;
|
||||
|
||||
public Book(String id, String title) {
|
||||
this.id = id;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private String formatBook() {
|
||||
return id + " > " + title;
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.java9.rangedates;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
|
||||
public class DatesCollectionIteration {
|
||||
|
||||
public void iteratingRangeOfDatesJava7(Collection<Date> dates) {
|
||||
|
||||
for (Date date : dates) {
|
||||
processDate(date);
|
||||
}
|
||||
}
|
||||
|
||||
public void iteratingRangeOfDatesJava8(Collection<Date> dates) {
|
||||
dates.stream()
|
||||
.forEach(this::processDate);
|
||||
}
|
||||
|
||||
private void processDate(Date date) {
|
||||
System.out.println(date);
|
||||
}
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.java9.rangedates;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
public class RangeDatesIteration {
|
||||
|
||||
public void iterateBetweenDatesJava9(LocalDate startDate, LocalDate endDate) {
|
||||
|
||||
startDate.datesUntil(endDate)
|
||||
.forEach(this::processDate);
|
||||
}
|
||||
|
||||
public void iterateBetweenDatesJava8(LocalDate start, LocalDate end) {
|
||||
for (LocalDate date = start; date.isBefore(end); date = date.plusDays(1)) {
|
||||
processDate(date);
|
||||
}
|
||||
}
|
||||
|
||||
public void iterateBetweenDatesJava7(Date start, Date end) {
|
||||
Date current = start;
|
||||
|
||||
while (current.before(end)) {
|
||||
processDate(current);
|
||||
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(current);
|
||||
|
||||
calendar.add(Calendar.DATE, 1);
|
||||
|
||||
current = calendar.getTime();
|
||||
}
|
||||
}
|
||||
|
||||
private void processDate(LocalDate date) {
|
||||
System.out.println(date);
|
||||
}
|
||||
|
||||
private void processDate(Date date) {
|
||||
System.out.println(date);
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
package com.baeldung.java9.reactive;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.Flow.Subscriber;
|
||||
import java.util.concurrent.Flow.Subscription;
|
||||
|
||||
public class BaeldungBatchSubscriberImpl<T> implements Subscriber<String> {
|
||||
private Subscription subscription;
|
||||
private boolean completed = false;
|
||||
private int counter;
|
||||
private ArrayList<String> buffer;
|
||||
public static final int BUFFER_SIZE = 5;
|
||||
|
||||
public BaeldungBatchSubscriberImpl() {
|
||||
buffer = new ArrayList<String>();
|
||||
}
|
||||
|
||||
public boolean isCompleted() {
|
||||
return completed;
|
||||
}
|
||||
|
||||
public void setCompleted(boolean completed) {
|
||||
this.completed = completed;
|
||||
}
|
||||
|
||||
public int getCounter() {
|
||||
return counter;
|
||||
}
|
||||
|
||||
public void setCounter(int counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription subscription) {
|
||||
this.subscription = subscription;
|
||||
subscription.request(BUFFER_SIZE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(String item) {
|
||||
buffer.add(item);
|
||||
// if buffer is full, process the items.
|
||||
if (buffer.size() >= BUFFER_SIZE) {
|
||||
processBuffer();
|
||||
}
|
||||
//request more items.
|
||||
subscription.request(1);
|
||||
}
|
||||
|
||||
private void processBuffer() {
|
||||
if (buffer.isEmpty())
|
||||
return;
|
||||
// Process all items in the buffer. Here, we just print it and sleep for 1 second.
|
||||
System.out.print("Processed items: ");
|
||||
buffer.stream()
|
||||
.forEach(item -> {
|
||||
System.out.print(" " + item);
|
||||
});
|
||||
System.out.println();
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
counter = counter + buffer.size();
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
completed = true;
|
||||
// process any remaining items in buffer before
|
||||
processBuffer();
|
||||
subscription.cancel();
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package com.baeldung.java9.reactive;
|
||||
|
||||
import java.util.concurrent.Flow.Subscriber;
|
||||
import java.util.concurrent.Flow.Subscription;
|
||||
|
||||
public class BaeldungSubscriberImpl<T> implements Subscriber<String> {
|
||||
private Subscription subscription;
|
||||
private boolean completed = false;
|
||||
private int counter;
|
||||
|
||||
public boolean isCompleted() {
|
||||
return completed;
|
||||
}
|
||||
|
||||
public void setCompleted(boolean completed) {
|
||||
this.completed = completed;
|
||||
}
|
||||
|
||||
public int getCounter() {
|
||||
return counter;
|
||||
}
|
||||
|
||||
public void setCounter(int counter) {
|
||||
this.counter = counter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSubscribe(Subscription subscription) {
|
||||
this.subscription = subscription;
|
||||
subscription.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(String item) {
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
counter++;
|
||||
System.out.println("Processed item : " + item);
|
||||
subscription.request(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
completed = true;
|
||||
subscription.cancel();
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.java9.set;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class UnmodifiableSet {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Set<String> set = new HashSet<>();
|
||||
set.add("Canada");
|
||||
set.add("USA");
|
||||
|
||||
coreJDK(set);
|
||||
guavaOf();
|
||||
copyOf(set);
|
||||
java9Of();
|
||||
}
|
||||
|
||||
private static void java9Of() {
|
||||
Set<String> immutable = Set.of("Canada", "USA");
|
||||
System.out.println(immutable);
|
||||
}
|
||||
|
||||
private static void guavaOf() {
|
||||
Set<String> immutable = ImmutableSet.of("Canada", "USA");
|
||||
System.out.println(immutable);
|
||||
}
|
||||
|
||||
private static void copyOf(Set<String> set) {
|
||||
Set<String> immutable = ImmutableSet.copyOf(set);
|
||||
set.add("Costa Rica");
|
||||
System.out.println(immutable);
|
||||
}
|
||||
|
||||
private static void coreJDK(Set<String> set) {
|
||||
Set<String> unmodifiableSet = Collections.unmodifiableSet(set);
|
||||
set.add("Costa Rica");
|
||||
System.out.println(unmodifiableSet);
|
||||
}
|
||||
}
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
package com.baeldung.java9.stackwalker;
|
||||
|
||||
import java.lang.StackWalker.StackFrame;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class StackWalkerDemo {
|
||||
|
||||
public void methodOne() {
|
||||
this.methodTwo();
|
||||
}
|
||||
|
||||
public void methodTwo() {
|
||||
this.methodThree();
|
||||
}
|
||||
|
||||
public void methodThree() {
|
||||
List<StackFrame> stackTrace = StackWalker.getInstance()
|
||||
.walk(this::walkExample);
|
||||
|
||||
printStackTrace(stackTrace);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
stackTrace = StackWalker.getInstance()
|
||||
.walk(this::walkExample2);
|
||||
|
||||
printStackTrace(stackTrace);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
String line = StackWalker.getInstance().walk(this::walkExample3);
|
||||
System.out.println(line);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
stackTrace = StackWalker.getInstance(StackWalker.Option.SHOW_REFLECT_FRAMES)
|
||||
.walk(this::walkExample);
|
||||
|
||||
printStackTrace(stackTrace);
|
||||
|
||||
System.out.println("---------------------------------------------");
|
||||
|
||||
Runnable r = () -> {
|
||||
List<StackFrame> stackTrace2 = StackWalker.getInstance(StackWalker.Option.SHOW_HIDDEN_FRAMES)
|
||||
.walk(this::walkExample);
|
||||
printStackTrace(stackTrace2);
|
||||
};
|
||||
r.run();
|
||||
}
|
||||
|
||||
public List<StackFrame> walkExample(Stream<StackFrame> stackFrameStream) {
|
||||
return stackFrameStream.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public List<StackFrame> walkExample2(Stream<StackFrame> stackFrameStream) {
|
||||
return stackFrameStream.filter(frame -> frame.getClassName()
|
||||
.contains("com.baeldung"))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public String walkExample3(Stream<StackFrame> stackFrameStream) {
|
||||
return stackFrameStream.filter(frame -> frame.getClassName()
|
||||
.contains("com.baeldung")
|
||||
&& frame.getClassName()
|
||||
.endsWith("Test"))
|
||||
.findFirst()
|
||||
.map(frame -> frame.getClassName() + "#" + frame.getMethodName() + ", Line " + frame.getLineNumber())
|
||||
.orElse("Unknown caller");
|
||||
}
|
||||
|
||||
public void findCaller() {
|
||||
Class<?> caller = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass();
|
||||
System.out.println(caller.getCanonicalName());
|
||||
}
|
||||
|
||||
public void printStackTrace(List<StackFrame> stackTrace) {
|
||||
for (StackFrame stackFrame : stackTrace) {
|
||||
System.out.println(stackFrame.getClassName()
|
||||
.toString() + "#" + stackFrame.getMethodName() + ", Line " + stackFrame.getLineNumber());
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user