Skip to content

Commit 5fa6d19

Browse files
committed
Merge branch 'masterupstream' into tutorials/jerseyFiltersInterceptors
2 parents 5cbfd34 + 9801a08 commit 5fa6d19

15 files changed

Lines changed: 540 additions & 4 deletions

File tree

‎core-groovy/build.gradle‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
group 'com.baeldung'
2+
version '1.0-SNAPSHOT'
3+
4+
apply plugin: 'groovy'
5+
6+
repositories {
7+
mavenCentral()
8+
}
9+
10+
dependencies {
11+
compile 'org.codehaus.groovy:groovy-all:2.5.0-alpha-1'
12+
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
13+
}

‎core-groovy/pom.xml‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@
2424
<dependency>
2525
<groupId>org.codehaus.groovy</groupId>
2626
<artifactId>groovy</artifactId>
27-
<version>2.4.13</version>
27+
<version>2.5.0-alpha-1</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.codehaus.groovy</groupId>
31+
<artifactId>groovy-all</artifactId>
32+
<version>2.5.0-alpha-1</version>
2833
</dependency>
2934
<dependency>
3035
<groupId>org.codehaus.groovy</groupId>
3136
<artifactId>groovy-sql</artifactId>
32-
<version>2.4.13</version>
37+
<version>2.5.0-alpha-1</version>
3338
</dependency>
3439
<dependency>
3540
<groupId>org.junit.jupiter</groupId>
@@ -49,6 +54,12 @@
4954
<version>2.4.0</version>
5055
<scope>test</scope>
5156
</dependency>
57+
<dependency>
58+
<groupId>org.spockframework</groupId>
59+
<artifactId>spock-core</artifactId>
60+
<version>1.1-groovy-2.4</version>
61+
<scope>test</scope>
62+
</dependency>
5263
</dependencies>
5364

5465
<build>
@@ -114,5 +125,5 @@
114125
<junit.vintage.version>4.12.0</junit.vintage.version>
115126
<junit4.version>4.12</junit4.version>
116127
</properties>
117-
118128
</project>
129+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.json
2+
3+
class Account {
4+
String id
5+
BigDecimal value
6+
Date createdAt
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.json
2+
3+
import groovy.json.JsonGenerator
4+
import groovy.json.JsonOutput
5+
import groovy.json.JsonParserType
6+
import groovy.json.JsonSlurper
7+
8+
class JsonParser {
9+
10+
Account toObject(String json) {
11+
JsonSlurper jsonSlurper = new JsonSlurper()
12+
jsonSlurper.parseText(json) as Account
13+
}
14+
15+
Account toObjectWithIndexOverlay(String json) {
16+
JsonSlurper jsonSlurper = new JsonSlurper(type: JsonParserType.INDEX_OVERLAY)
17+
jsonSlurper.parseText(json) as Account
18+
}
19+
20+
String toJson(Account account) {
21+
JsonOutput.toJson(account)
22+
}
23+
24+
String toJson(Account account, String dateFormat, String... fieldsToExclude) {
25+
JsonGenerator generator = new JsonGenerator.Options()
26+
.dateFormat(dateFormat)
27+
.excludeFieldsByName(fieldsToExclude)
28+
.build()
29+
generator.toJson(account)
30+
}
31+
32+
String prettyfy(String json) {
33+
JsonOutput.prettyPrint(json)
34+
}
35+
36+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.json
2+
3+
import spock.lang.Specification
4+
5+
import java.text.SimpleDateFormat
6+
7+
class JsonParserTest extends Specification {
8+
9+
JsonParser jsonParser
10+
11+
void setup () {
12+
jsonParser = new JsonParser()
13+
}
14+
15+
def 'Should parse to Account given Json String' () {
16+
given:
17+
def json = '{"id":"1234","value":15.6}'
18+
when:
19+
def account = jsonParser.toObject(json)
20+
then:
21+
account
22+
account instanceof Account
23+
account.id == '1234'
24+
account.value == 15.6
25+
}
26+
27+
def 'Should parse to Account given Json String with date property' () {
28+
given:
29+
def json = '{"id":"1234","value":15.6,"createdAt":"2018-01-01T00:00:00+0000"}'
30+
when:
31+
def account = jsonParser.toObjectWithIndexOverlay(json)
32+
then:
33+
account
34+
account instanceof Account
35+
account.id == '1234'
36+
account.value == 15.6
37+
println account.createdAt
38+
account.createdAt == Date.parse('yyyy-MM-dd', '2018-01-01')
39+
}
40+
41+
def 'Should parse to Json given an Account object' () {
42+
given:
43+
Account account = new Account(
44+
id: '123',
45+
value: 15.6,
46+
createdAt: new SimpleDateFormat('MM/dd/yyyy').parse('01/01/2018')
47+
)
48+
when:
49+
def json = jsonParser.toJson(account)
50+
then:
51+
json
52+
json == '{"value":15.6,"createdAt":"2018-01-01T00:00:00+0000","id":"123"}'
53+
}
54+
55+
def 'Should parse to Json given an Account object, a date format and fields to exclude' () {
56+
given:
57+
Account account = new Account(
58+
id: '123',
59+
value: 15.6,
60+
createdAt: new SimpleDateFormat('MM/dd/yyyy').parse('01/01/2018')
61+
)
62+
when:
63+
def json = jsonParser.toJson(account, 'MM/dd/yyyy', 'value')
64+
then:
65+
json
66+
json == '{"createdAt":"01/01/2018","id":"123"}'
67+
}
68+
69+
def 'Should prettify given a json string' () {
70+
given:
71+
String json = '{"value":15.6,"createdAt":"01/01/2018","id":"123456"}'
72+
when:
73+
def jsonPretty = jsonParser.prettyfy(json)
74+
then:
75+
jsonPretty
76+
jsonPretty == '{\n "value": 15.6,\n "createdAt": "01/01/2018",\n "id": "123456"\n}'
77+
}
78+
79+
80+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.java9.methodhandles;
2+
3+
public class Book {
4+
5+
String id;
6+
String title;
7+
8+
public Book(String id, String title) {
9+
this.id = id;
10+
this.title = title;
11+
}
12+
13+
@SuppressWarnings("unused")
14+
private String formatBook() {
15+
return id + " > " + title;
16+
}
17+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.baeldung.java9.methodhandles;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.*;
5+
6+
import java.lang.invoke.MethodHandle;
7+
import java.lang.invoke.MethodHandles;
8+
import java.lang.invoke.MethodType;
9+
import java.lang.invoke.WrongMethodTypeException;
10+
import java.lang.reflect.InvocationTargetException;
11+
import java.lang.reflect.Method;
12+
import java.util.Arrays;
13+
import java.util.List;
14+
15+
import org.junit.Test;
16+
17+
/**
18+
* Test case for the {@link MethodHandles} API
19+
*/
20+
public class MethodHandlesTest {
21+
22+
@Test
23+
public void givenConcatMethodHandle_whenInvoked_thenCorrectlyConcatenated() throws Throwable {
24+
25+
MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();
26+
MethodType mt = MethodType.methodType(String.class, String.class);
27+
MethodHandle concatMH = publicLookup.findVirtual(String.class, "concat", mt);
28+
29+
String output = (String) concatMH.invoke("Effective ", "Java");
30+
31+
assertEquals("Effective Java", output);
32+
}
33+
34+
@Test
35+
public void givenAsListMethodHandle_whenInvokingWithArguments_thenCorrectlyInvoked() throws Throwable {
36+
MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();
37+
MethodType mt = MethodType.methodType(List.class, Object[].class);
38+
MethodHandle asListMH = publicLookup.findStatic(Arrays.class, "asList", mt);
39+
40+
List<Integer> list = (List<Integer>) asListMH.invokeWithArguments(1, 2);
41+
42+
assertThat(Arrays.asList(1, 2), is(list));
43+
}
44+
45+
@Test
46+
public void givenConstructorMethodHandle_whenInvoked_thenObjectCreatedCorrectly() throws Throwable {
47+
MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();
48+
MethodType mt = MethodType.methodType(void.class, String.class);
49+
MethodHandle newIntegerMH = publicLookup.findConstructor(Integer.class, mt);
50+
51+
Integer integer = (Integer) newIntegerMH.invoke("1");
52+
53+
assertEquals(1, integer.intValue());
54+
}
55+
56+
@Test
57+
public void givenAFieldWithoutGetter_whenCreatingAGetter_thenCorrectlyInvoked() throws Throwable {
58+
MethodHandles.Lookup lookup = MethodHandles.lookup();
59+
MethodHandle getTitleMH = lookup.findGetter(Book.class, "title", String.class);
60+
61+
Book book = new Book("ISBN-1234", "Effective Java");
62+
63+
assertEquals("Effective Java", getTitleMH.invoke(book));
64+
}
65+
66+
@Test
67+
public void givenPrivateMethod_whenCreatingItsMethodHandle_thenCorrectlyInvoked() throws Throwable {
68+
MethodHandles.Lookup lookup = MethodHandles.lookup();
69+
Method formatBookMethod = Book.class.getDeclaredMethod("formatBook");
70+
formatBookMethod.setAccessible(true);
71+
72+
MethodHandle formatBookMH = lookup.unreflect(formatBookMethod);
73+
74+
Book book = new Book("ISBN-123", "Java in Action");
75+
76+
assertEquals("ISBN-123 > Java in Action", formatBookMH.invoke(book));
77+
}
78+
79+
@Test
80+
public void givenReplaceMethod_whenUsingReflectionAndInvoked_thenCorrectlyReplaced() throws Throwable {
81+
Method replaceMethod = String.class.getMethod("replace", char.class, char.class);
82+
83+
String string = (String) replaceMethod.invoke("jovo", 'o', 'a');
84+
85+
assertEquals("java", string);
86+
}
87+
88+
@Test
89+
public void givenReplaceMethodHandle_whenInvoked_thenCorrectlyReplaced() throws Throwable {
90+
MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();
91+
MethodType mt = MethodType.methodType(String.class, char.class, char.class);
92+
MethodHandle replaceMH = publicLookup.findVirtual(String.class, "replace", mt);
93+
94+
String replacedString = (String) replaceMH.invoke("jovo", Character.valueOf('o'), 'a');
95+
96+
assertEquals("java", replacedString);
97+
}
98+
99+
@Test
100+
public void givenReplaceMethodHandle_whenInvokingExact_thenCorrectlyReplaced() throws Throwable {
101+
MethodHandles.Lookup lookup = MethodHandles.lookup();
102+
MethodType mt = MethodType.methodType(String.class, char.class, char.class);
103+
MethodHandle replaceMH = lookup.findVirtual(String.class, "replace", mt);
104+
105+
String s = (String) replaceMH.invokeExact("jovo", 'o', 'a');
106+
107+
assertEquals("java", s);
108+
}
109+
110+
@Test
111+
public void givenSumMethodHandle_whenInvokingExact_thenSumIsCorrect() throws Throwable {
112+
MethodHandles.Lookup lookup = MethodHandles.lookup();
113+
MethodType mt = MethodType.methodType(int.class, int.class, int.class);
114+
MethodHandle sumMH = lookup.findStatic(Integer.class, "sum", mt);
115+
116+
int sum = (int) sumMH.invokeExact(1, 11);
117+
118+
assertEquals(12, sum);
119+
}
120+
121+
@Test(expected = WrongMethodTypeException.class)
122+
public void givenSumMethodHandleAndIncompatibleArguments_whenInvokingExact_thenException() throws Throwable {
123+
MethodHandles.Lookup lookup = MethodHandles.lookup();
124+
MethodType mt = MethodType.methodType(int.class, int.class, int.class);
125+
MethodHandle sumMH = lookup.findStatic(Integer.class, "sum", mt);
126+
127+
sumMH.invokeExact(Integer.valueOf(1), 11);
128+
}
129+
130+
@Test
131+
public void givenSpreadedEqualsMethodHandle_whenInvokedOnArray_thenCorrectlyEvaluated() throws Throwable {
132+
MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();
133+
MethodType mt = MethodType.methodType(boolean.class, Object.class);
134+
MethodHandle equalsMH = publicLookup.findVirtual(String.class, "equals", mt);
135+
136+
MethodHandle methodHandle = equalsMH.asSpreader(Object[].class, 2);
137+
138+
assertTrue((boolean) methodHandle.invoke(new Object[] { "java", "java" }));
139+
assertFalse((boolean) methodHandle.invoke(new Object[] { "java", "jova" }));
140+
}
141+
142+
@Test
143+
public void givenConcatMethodHandle_whenBindToAString_thenCorrectlyConcatenated() throws Throwable {
144+
MethodHandles.Lookup publicLookup = MethodHandles.publicLookup();
145+
MethodType mt = MethodType.methodType(String.class, String.class);
146+
MethodHandle concatMH = publicLookup.findVirtual(String.class, "concat", mt);
147+
148+
MethodHandle bindedConcatMH = concatMH.bindTo("Hello ");
149+
150+
assertEquals("Hello World!", bindedConcatMH.invoke("World!"));
151+
}
152+
}

‎core-java/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,4 @@
128128
- [Recursion In Java](http://www.baeldung.com/java-recursion)
129129
- [A Guide to the finalize Method in Java](http://www.baeldung.com/java-finalize)
130130
- [Compiling Java *.class Files with javac](http://www.baeldung.com/javac)
131+
- [Method Overloading and Overriding in Java](http://www.baeldung.com/java-method-overload-override)

‎core-kotlin/pom.xml‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,18 @@
7676
<artifactId>mockito-kotlin</artifactId>
7777
<version>${mockito-kotlin.version}</version>
7878
<scope>test</scope>
79-
</dependency>
79+
</dependency>
80+
<dependency>
81+
<groupId>com.github.salomonbrys.kodein</groupId>
82+
<artifactId>kodein</artifactId>
83+
<version>${kodein.version}</version>
84+
</dependency>
85+
<dependency>
86+
<groupId>org.assertj</groupId>
87+
<artifactId>assertj-core</artifactId>
88+
<version>${assertj.version}</version>
89+
<scope>test</scope>
90+
</dependency>
8091
</dependencies>
8192

8293
<build>
@@ -189,11 +200,13 @@
189200
<kotlin-reflect.version>1.1.2</kotlin-reflect.version>
190201
<kotlinx.version>0.15</kotlinx.version>
191202
<mockito-kotlin.version>1.5.0</mockito-kotlin.version>
203+
<kodein.version>4.1.0</kodein.version>
192204

193205
<junit.jupiter.version>5.0.0</junit.jupiter.version>
194206
<junit.platform.version>1.0.0</junit.platform.version>
195207
<junit.vintage.version>4.12.0</junit.vintage.version>
196208
<junit4.version>4.12</junit4.version>
209+
<assertj.version>3.9.1</assertj.version>
197210
</properties>
198211

199212
</project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.baeldung.kotlin.kodein
2+
3+
class Controller(private val service : Service)

0 commit comments

Comments
 (0)