Skip to content

Commit

Permalink
Review: Simplify test and add some comments as to why synthetic / bri…
Browse files Browse the repository at this point in the history
…dge members are created in the example class

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
  • Loading branch information
codecholeric committed Oct 20, 2019
1 parent 2826f8b commit e862fae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,14 @@ public void handles_static_modifier_of_nested_classes() throws Exception {
public void handles_synthetic_modifiers() throws Exception {
JavaClasses classes = classesIn("testexamples/syntheticimport").classes;

assertThat(classes.get(ClassWithSynthetics.ClassWithSyntheticField.class).getFields().size()).isEqualTo(1);
assertThat(classes.get(ClassWithSynthetics.ClassWithSyntheticField.class).getFields().toArray(new JavaField[]{})[0].getModifiers()).as("modifiers of field in ClassWithSynthetics.ClassWithSyntheticField").contains(SYNTHETIC);
JavaField syntheticField = getOnlyElement(classes.get(ClassWithSynthetics.ClassWithSyntheticField.class).getFields());
assertThat(syntheticField.getModifiers()).as("modifiers of field in ClassWithSynthetics.ClassWithSyntheticField").contains(SYNTHETIC);

assertThat(classes.get(ClassWithSynthetics.ClassWithSyntheticMethod.class).getMethods().size()).isEqualTo(1);
assertThat(classes.get(ClassWithSynthetics.ClassWithSyntheticMethod.class).getMethods().toArray(new JavaMethod[]{})[0].getModifiers()).as("modifiers of method in ClassWithSynthetics.ClassWithSyntheticMethod").contains(SYNTHETIC);
JavaMethod syntheticMethod = getOnlyElement(classes.get(ClassWithSynthetics.ClassWithSyntheticMethod.class).getMethods());
assertThat(syntheticMethod.getModifiers()).as("modifiers of method in ClassWithSynthetics.ClassWithSyntheticMethod").contains(SYNTHETIC);

assertThat(classes.get(ClassWithSynthetics.class).getMethod("compare", Object.class, Object.class).getModifiers()).as("modifiers of bridge method in ClassWithSynthetics").contains(BRIDGE);
assertThat(classes.get(ClassWithSynthetics.class).getMethod("compare", Object.class, Object.class).getModifiers()).as("modifiers of bridge method in ClassWithSynthetics").contains(SYNTHETIC);
JavaMethod compareMethod = classes.get(ClassWithSynthetics.class).getMethod("compare", Object.class, Object.class);
assertThat(compareMethod.getModifiers()).as("modifiers of bridge method in ClassWithSynthetics").contains(BRIDGE, SYNTHETIC);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import java.util.Comparator;

@SuppressWarnings({"unused", "InnerClassMayBeStatic"})
public class ClassWithSynthetics implements Comparator<String> {
// for (non-static) inner classes the compiler must create a synthetic field, holding a reference to the outer class
public class ClassWithSyntheticField {
}

// for accesses to private fields of inner classes, the compiler must create a synthetic method to allow access to this field
// thus together with the method 'getNestedField', this causes the existence of a synthetic method
public class ClassWithSyntheticMethod {
private String nestedField;
}
Expand All @@ -14,6 +18,7 @@ public String getNestedField() {
return new ClassWithSyntheticMethod().nestedField;
}

// to cover type erasure, the compiler must add a bridge method with signature compare(Object, Object) here
@Override
public int compare(String o1, String o2) {
return 0;
Expand Down

0 comments on commit e862fae

Please sign in to comment.