Skip to content

Commit

Permalink
Merge pull request #6748 from pontusmelke/3.0-synthetic-fields
Browse files Browse the repository at this point in the history
Allow synthetic fields in procedures.
  • Loading branch information
pontusmelke committed Mar 21, 2016
2 parents 962cbf2 + ffc8f45 commit a7f45f5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public List<FieldSetter> setters( Class<?> cls ) throws ProcedureException
{
for ( Field field : currentClass.getDeclaredFields() )
{
//ignore synthetic fields
if (field.isSynthetic())
{
continue;
}
if ( Modifier.isStatic( field.getModifiers() ) )
{
if( field.isAnnotationPresent( Context.class ))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,27 @@ public void inheritanceIsAllowed() throws Throwable
assertEquals( 1337, childProcedure.parentField );
}

@Test
public void syntheticsAllowed() throws Throwable
{
// Given
ComponentRegistry components = new ComponentRegistry();
components.register( int.class, (ctx) -> 1337 );
FieldInjections injections = new FieldInjections( components );

// When
List<FieldInjections.FieldSetter> setters = injections.setters( Outer.ClassWithSyntheticField.class );

// Then
Outer.ClassWithSyntheticField syntheticField = new Outer().classWithSyntheticField();
for ( FieldInjections.FieldSetter setter : setters )
{
setter.apply( null, syntheticField );
}

assertEquals( 1337, syntheticField.innerField );
}

public static class ProcedureWithNonInjectedMemberFields
{
public boolean someState = false;
Expand Down Expand Up @@ -127,4 +148,25 @@ public static class ChildProcedure extends ParentProcedure
@Context
public int childField;
}

//The outer class is just here to force a synthetic field in the inner class.
//This is not a realistic scenario but we merely want to make sure the loader
//does not choke on synthetic fields since compilers, e.g. groovy, can generate
//these.
public static class Outer
{
ClassWithSyntheticField classWithSyntheticField()
{
return new ClassWithSyntheticField();
}

public class ClassWithSyntheticField
{
//this class will have a generated field:
//synthetic Outer this$0;

@Context
public int innerField;
}
}
}

0 comments on commit a7f45f5

Please sign in to comment.