Skip to content

Commit 2a66773

Browse files
auraspherepedja4
authored andcommitted
[BAEL-1935] Synthetic Class in Java (eugenp#5122)
* BAEL-1935: Synthetic Class in Java * Converted tabs to spaces
1 parent 276fbd8 commit 2a66773

5 files changed

Lines changed: 226 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.synthetic;
2+
3+
import java.util.Comparator;
4+
5+
/**
6+
* Class which contains a synthetic bridge method.
7+
*
8+
* @author Donato Rimenti
9+
*
10+
*/
11+
public class BridgeMethodDemo implements Comparator<Integer> {
12+
13+
/*
14+
* (non-Javadoc)
15+
*
16+
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
17+
*/
18+
@Override
19+
public int compare(Integer o1, Integer o2) {
20+
return 0;
21+
}
22+
23+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.synthetic;
2+
3+
/**
4+
* Wrapper for a class which contains a synthetic constructor.
5+
*
6+
* @author Donato Rimenti
7+
*
8+
*/
9+
public class SyntheticConstructorDemo {
10+
11+
/**
12+
* We need to instantiate the {@link NestedClass} using a private
13+
* constructor from the enclosing instance in order to generate a synthetic
14+
* constructor.
15+
*/
16+
private NestedClass nestedClass = new NestedClass();
17+
18+
/**
19+
* Class which contains a synthetic constructor.
20+
*
21+
* @author Donato Rimenti
22+
*
23+
*/
24+
class NestedClass {
25+
26+
/**
27+
* In order to generate a synthetic constructor, this class must have a
28+
* private constructor.
29+
*/
30+
private NestedClass() {
31+
}
32+
}
33+
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.synthetic;
2+
3+
/**
4+
* Wrapper for a class which contains a synthetic field reference to the outer
5+
* class.
6+
*
7+
* @author Donato Rimenti
8+
*
9+
*/
10+
public class SyntheticFieldDemo {
11+
12+
/**
13+
* Class which contains a synthetic field reference to the outer class.
14+
*
15+
* @author Donato Rimenti
16+
*
17+
*/
18+
class NestedClass {
19+
20+
}
21+
22+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.synthetic;
2+
3+
/**
4+
* Wrapper for a class which contains two synthetic methods accessors to a
5+
* private field.
6+
*
7+
* @author Donato Rimenti
8+
*
9+
*/
10+
public class SyntheticMethodDemo {
11+
12+
/**
13+
* Class which contains two synthetic methods accessors to a private field.
14+
*
15+
* @author Donato Rimenti
16+
*
17+
*/
18+
class NestedClass {
19+
20+
/**
21+
* Field for which will be generated synthetic methods accessors. It's
22+
* important that this field is private for this purpose.
23+
*/
24+
private String nestedField;
25+
}
26+
27+
/**
28+
* Gets the private nested field. We need to read the nested field in order
29+
* to generate the synthetic getter.
30+
*
31+
* @return the {@link NestedClass#nestedField}
32+
*/
33+
public String getNestedField() {
34+
return new NestedClass().nestedField;
35+
}
36+
37+
/**
38+
* Sets the private nested field. We need to write the nested field in order
39+
* to generate the synthetic setter.
40+
*
41+
* @param nestedField
42+
* the {@link NestedClass#nestedField}
43+
*/
44+
public void setNestedField(String nestedField) {
45+
new NestedClass().nestedField = nestedField;
46+
}
47+
48+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.baeldung.synthetic;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.Field;
5+
import java.lang.reflect.Method;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
/**
11+
* Unit test for {@link SyntheticFieldDemo}, {@link SyntheticMethodDemo},
12+
* {@link SyntheticConstructorDemo} and {@link BridgeMethodDemo} classes.
13+
*
14+
* @author Donato Rimenti
15+
*
16+
*/
17+
public class SyntheticUnitTest {
18+
19+
/**
20+
* Tests that the {@link SyntheticMethodDemo.NestedClass} contains two synthetic
21+
* methods.
22+
*/
23+
@Test
24+
public void givenSyntheticMethod_whenIsSinthetic_thenTrue() {
25+
// Checks that the nested class contains exactly two synthetic methods.
26+
Method[] methods = SyntheticMethodDemo.NestedClass.class.getDeclaredMethods();
27+
Assert.assertEquals("This class should contain only two methods", 2, methods.length);
28+
29+
for (Method m : methods) {
30+
System.out.println("Method: " + m.getName() + ", isSynthetic: " + m.isSynthetic());
31+
Assert.assertTrue("All the methods of this class should be synthetic", m.isSynthetic());
32+
}
33+
}
34+
35+
/**
36+
* Tests that {@link SyntheticConstructorDemo.NestedClass} contains a synthetic
37+
* constructor.
38+
*/
39+
@Test
40+
public void givenSyntheticConstructor_whenIsSinthetic_thenTrue() {
41+
// Checks that the nested class contains exactly a synthetic
42+
// constructor.
43+
int syntheticConstructors = 0;
44+
Constructor<?>[] constructors = SyntheticConstructorDemo.NestedClass.class.getDeclaredConstructors();
45+
Assert.assertEquals("This class should contain only two constructors", 2, constructors.length);
46+
47+
for (Constructor<?> c : constructors) {
48+
System.out.println("Constructor: " + c.getName() + ", isSynthetic: " + c.isSynthetic());
49+
50+
// Counts the synthetic constructors.
51+
if (c.isSynthetic()) {
52+
syntheticConstructors++;
53+
}
54+
}
55+
56+
// Checks that there's exactly one synthetic constructor.
57+
Assert.assertEquals(1, syntheticConstructors);
58+
}
59+
60+
/**
61+
* Tests that {@link SyntheticFieldDemo.NestedClass} contains a synthetic field.
62+
*/
63+
@Test
64+
public void givenSyntheticField_whenIsSinthetic_thenTrue() {
65+
// This class should contain exactly one synthetic field.
66+
Field[] fields = SyntheticFieldDemo.NestedClass.class.getDeclaredFields();
67+
Assert.assertEquals("This class should contain only one field", 1, fields.length);
68+
69+
for (Field f : fields) {
70+
System.out.println("Field: " + f.getName() + ", isSynthetic: " + f.isSynthetic());
71+
Assert.assertTrue("All the fields of this class should be synthetic", f.isSynthetic());
72+
}
73+
}
74+
75+
/**
76+
* Tests that {@link BridgeMethodDemo} contains a synthetic bridge method.
77+
*/
78+
@Test
79+
public void givenBridgeMethod_whenIsBridge_thenTrue() {
80+
// This class should contain exactly one synthetic bridge method.
81+
int syntheticMethods = 0;
82+
Method[] methods = BridgeMethodDemo.class.getDeclaredMethods();
83+
for (Method m : methods) {
84+
System.out.println(
85+
"Method: " + m.getName() + ", isSynthetic: " + m.isSynthetic() + ", isBridge: " + m.isBridge());
86+
87+
// Counts the synthetic methods and checks that they are also bridge
88+
// methods.
89+
if (m.isSynthetic()) {
90+
syntheticMethods++;
91+
Assert.assertTrue("The synthetic method in this class should also be a bridge method", m.isBridge());
92+
}
93+
}
94+
95+
// Checks that there's exactly one synthetic bridge method.
96+
Assert.assertEquals("There should be exactly 1 synthetic bridge method in this class", 1, syntheticMethods);
97+
}
98+
99+
}

0 commit comments

Comments
 (0)