Skip to content

Commit

Permalink
Support for ternary operator
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed May 17, 2016
1 parent fad8d41 commit 09d7860
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
Expand Up @@ -162,7 +162,15 @@ public void not( Expression expression )
@Override @Override
public void ternary( Expression test, Expression onTrue, Expression onFalse ) public void ternary( Expression test, Expression onTrue, Expression onFalse )
{ {

test.accept( this );
Label l0 = new Label();
methodVisitor.visitJumpInsn( IFEQ, l0 );
onTrue.accept( this );
Label l1 = new Label();
methodVisitor.visitJumpInsn( GOTO, l1 );
methodVisitor.visitLabel( l0 );
onFalse.accept( this );
methodVisitor.visitLabel( l1 );
} }


@Override @Override
Expand Down
Expand Up @@ -60,6 +60,7 @@
import static org.neo4j.codegen.Expression.newInstance; import static org.neo4j.codegen.Expression.newInstance;
import static org.neo4j.codegen.Expression.not; import static org.neo4j.codegen.Expression.not;
import static org.neo4j.codegen.Expression.or; import static org.neo4j.codegen.Expression.or;
import static org.neo4j.codegen.Expression.ternary;
import static org.neo4j.codegen.ExpressionTemplate.cast; import static org.neo4j.codegen.ExpressionTemplate.cast;
import static org.neo4j.codegen.ExpressionTemplate.load; import static org.neo4j.codegen.ExpressionTemplate.load;
import static org.neo4j.codegen.ExpressionTemplate.self; import static org.neo4j.codegen.ExpressionTemplate.self;
Expand Down Expand Up @@ -644,7 +645,6 @@ public void shouldGenerateMethodUsingOr() throws Throwable
handle = simple.handle(); handle = simple.handle();
} }



// when // when
MethodHandle conditional = MethodHandle conditional =
instanceMethod( handle.newInstance(), "conditional", boolean.class, boolean.class); instanceMethod( handle.newInstance(), "conditional", boolean.class, boolean.class);
Expand All @@ -657,7 +657,7 @@ public void shouldGenerateMethodUsingOr() throws Throwable
} }


@Test @Test
public void shouldGenerateMethodUsingNot() throws Throwable public void shouldHandleNot() throws Throwable
{ {
// given // given
ClassHandle handle; ClassHandle handle;
Expand All @@ -672,7 +672,6 @@ public void shouldGenerateMethodUsingNot() throws Throwable
handle = simple.handle(); handle = simple.handle();
} }



// when // when
MethodHandle conditional = MethodHandle conditional =
instanceMethod( handle.newInstance(), "conditional", boolean.class); instanceMethod( handle.newInstance(), "conditional", boolean.class);
Expand All @@ -682,6 +681,62 @@ public void shouldGenerateMethodUsingNot() throws Throwable
assertThat(conditional.invoke( false), equalTo(true)); assertThat(conditional.invoke( false), equalTo(true));
} }


@Test
public void shouldHandleTernaryOperator() throws Throwable
{
// given
ClassHandle handle;
try ( ClassGenerator simple = generateClass( "SimpleClass" ) )
{
try ( CodeBlock conditional = simple.generateMethod( String.class, "ternary",
param( boolean.class, "test"), param( TernaryChecker.class, "check" )) )
{
conditional.returns(
ternary( conditional.load( "test" ),
invoke( conditional.load("check"), methodReference( TernaryChecker.class, String.class, "onTrue" ) ),
invoke( conditional.load("check"), methodReference( TernaryChecker.class, String.class, "onFalse" ) )));
}

handle = simple.handle();
}

// when
MethodHandle ternary =
instanceMethod( handle.newInstance(), "ternary", boolean.class, TernaryChecker.class);

// then
TernaryChecker checker1 = new TernaryChecker();
assertThat(ternary.invoke( true, checker1), equalTo("on true"));
assertTrue(checker1.ranOnTrue);
assertFalse(checker1.ranOnFalse);


TernaryChecker checker2 = new TernaryChecker();
assertThat(ternary.invoke( false, checker2), equalTo("on false"));
assertFalse(checker2.ranOnTrue);
assertTrue(checker2.ranOnFalse);
}

public static class TernaryChecker
{
private boolean ranOnTrue = false;
private boolean ranOnFalse = false;

public String onTrue()
{
ranOnTrue = true;
return "on true";
}

public String onFalse()
{
ranOnFalse = true;
return "on false";
}


}



public static class ResourceFactory public static class ResourceFactory
{ {
Expand Down

0 comments on commit 09d7860

Please sign in to comment.