Skip to content

Commit

Permalink
Support local variable assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
pontusmelke committed May 17, 2016
1 parent fd37acc commit 922e24c
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 83 deletions.
Expand Up @@ -108,22 +108,37 @@ public MethodReference generate( MethodTemplate template, Binding... bindings )
{
template.generate( generator );
}
return methodReference( handle, template.returnType(), template.name(), template.parameterTypes() );
return methodReference( handle, template.returnType(), template.name(), template.modifiers(), template.parameterTypes() );
}

public CodeBlock generateConstructor( Parameter... parameters )
{
return generate( constructor( handle, parameters,/*throws:*/NO_TYPES, NO_PARAMETERS ) );
return generate( constructor( handle, parameters,/*throws:*/NO_TYPES, Modifier.PUBLIC, NO_PARAMETERS ) );
}

public CodeBlock generateConstructor( int modifiers, Parameter... parameters )
{
return generate( constructor( handle, parameters,/*throws:*/NO_TYPES, modifiers, NO_PARAMETERS ) );
}

public CodeBlock generateMethod( Class<?> returnType, String name, Parameter... parameters )
{
return generateMethod( typeReference( returnType ), name, parameters );
return generateMethod( typeReference( returnType ), name, Modifier.PUBLIC, parameters );
}

public CodeBlock generateMethod( Class<?> returnType, String name, int modifiers, Parameter... parameters )
{
return generateMethod( typeReference( returnType ), name, modifiers, parameters );
}

public CodeBlock generateMethod( TypeReference returnType, String name, Parameter... parameters )
{
return generate( method( handle, returnType, name, parameters,/*throws:*/NO_TYPES, NO_PARAMETERS ) );
return generate( method( handle, returnType, name, parameters,/*throws:*/NO_TYPES, Modifier.PUBLIC, NO_PARAMETERS ) );
}

public CodeBlock generateMethod( TypeReference returnType, String name, int modifiers, Parameter... parameters )
{
return generate( method( handle, returnType, name, parameters,/*throws:*/NO_TYPES, modifiers, NO_PARAMETERS ) );
}

public CodeBlock generate( MethodDeclaration.Builder builder )
Expand Down
Expand Up @@ -104,7 +104,7 @@ public LocalVariable declare( TypeReference type, String name )

public void assign( LocalVariable local, Expression value )
{
emitter.assign( local, value );
emitter.assignVariableInScope( local, value );
}

public void assign( Class<?> type, String name, Expression value )
Expand All @@ -114,8 +114,9 @@ public void assign( Class<?> type, String name, Expression value )

public void assign( TypeReference type, String name, Expression value )
{
localVariables.put(name, localVariable( type, name ) );
emitter.assign( type, name, value );
LocalVariable variable = localVariable( type, name );
localVariables.put(name, variable );
emitter.assign( variable, value );
}

public void put( Expression target, FieldReference field, Expression value )
Expand Down
Expand Up @@ -19,6 +19,8 @@
*/
package org.neo4j.codegen;

import java.lang.reflect.Modifier;

import static org.neo4j.codegen.TypeReference.VOID;
import static org.neo4j.codegen.TypeReference.typeReference;

Expand Down Expand Up @@ -230,7 +232,7 @@ static ExpressionTemplate invokeSuperConstructor( final ExpressionTemplate[] par
void templateAccept( CodeBlock method, ExpressionVisitor visitor )
{
visitor.invoke( Expression.SUPER,
new MethodReference( method.clazz.handle().parent(), "<init>", VOID, parameterTypes),
new MethodReference( method.clazz.handle().parent(), "<init>", VOID, Modifier.PUBLIC, parameterTypes),
materialize( method, parameters ) );
}
};
Expand Down
Expand Up @@ -81,7 +81,7 @@ public void returns( Expression value )
}

@Override
public void assign( TypeReference type, String name, Expression value )
public void assign( LocalVariable variable, Expression value )
{
throw new IllegalStateException( reason );
}
Expand Down Expand Up @@ -135,7 +135,7 @@ public void declare( LocalVariable local )
}

@Override
public void assign( LocalVariable local, Expression value )
public void assignVariableInScope( LocalVariable local, Expression value )
{
throw new IllegalStateException( reason );
}
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/
package org.neo4j.codegen;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand All @@ -45,7 +46,7 @@ public static Builder method( final TypeReference returnType, final String name,
@Override
MethodDeclaration build( TypeReference owner )
{
return method( owner, returnType, name, parameters, exceptions(), typeParameters() );
return method( owner, returnType, name, parameters, exceptions(), modifiers(), typeParameters() );
}
};
}
Expand All @@ -57,7 +58,7 @@ static Builder constructor( Parameter... parameters )
@Override
MethodDeclaration build( TypeReference owner )
{
return constructor( owner, parameters, exceptions(), typeParameters() );
return constructor( owner, parameters, exceptions(), modifiers(), typeParameters() );
}
};
}
Expand Down Expand Up @@ -105,10 +106,22 @@ public Builder throwsException( TypeReference type )
return this;
}

public Builder modifiers(int modifiers)
{
this.modifiers = modifiers;
return this;
}

public int modifiers()
{
return modifiers;
}

abstract MethodDeclaration build( TypeReference owner );

final Parameter[] parameters;
private List<TypeReference> exceptions;
private int modifiers = Modifier.PUBLIC;

private Builder( Parameter[] parameters )
{
Expand Down Expand Up @@ -143,21 +156,23 @@ TypeParameter[] typeParameters()
private final Parameter[] parameters;
private final TypeReference[] exceptions;
private final TypeParameter[] typeParameters;
private final int modifiers;

MethodDeclaration( TypeReference owner, Parameter[] parameters, TypeReference[] exceptions,
TypeParameter[] typeParameters )
int modifiers, TypeParameter[] typeParameters )
{
this.owner = owner;
this.parameters = parameters;
this.exceptions = exceptions;
this.modifiers = modifiers;
this.typeParameters = typeParameters;
}

public abstract boolean isConstructor();

public boolean isStatic()
{
return false;
return Modifier.isStatic( modifiers );
}

public boolean isGeneric()
Expand All @@ -182,6 +197,11 @@ public TypeReference declaringClass()
return owner;
}

public int modifiers()
{
return modifiers;
}

public abstract TypeReference returnType();

public abstract String name();
Expand Down Expand Up @@ -215,7 +235,7 @@ public MethodDeclaration erased()
String newName = name();
boolean newIsConstrucor = isConstructor();

return new MethodDeclaration( owner, newParameters, newExceptions, typeParameters )
return new MethodDeclaration( owner, newParameters, newExceptions, modifiers, typeParameters )
{
@Override
public boolean isConstructor()
Expand Down Expand Up @@ -245,9 +265,9 @@ private TypeReference erase( TypeReference reference, Map<String,TypeReference>
}

static MethodDeclaration method( TypeReference owner, final TypeReference returnType, final String name,
Parameter[] parameters, TypeReference[] exceptions, TypeParameter[] typeParameters )
Parameter[] parameters, TypeReference[] exceptions, int modifiers, TypeParameter[] typeParameters )
{
return new MethodDeclaration( owner, parameters, exceptions, typeParameters )
return new MethodDeclaration( owner, parameters, exceptions, modifiers, typeParameters )
{
@Override
public boolean isConstructor()
Expand All @@ -270,9 +290,9 @@ public String name()
}

static MethodDeclaration constructor( TypeReference owner, Parameter[] parameters, TypeReference[] exceptions,
TypeParameter[] typeParameters )
int modifiers, TypeParameter[] typeParameters )
{
return new MethodDeclaration( owner, parameters, exceptions, typeParameters )
return new MethodDeclaration( owner, parameters, exceptions ,modifiers, typeParameters )
{
@Override
public boolean isConstructor()
Expand Down
Expand Up @@ -31,7 +31,7 @@ public interface MethodEmitter

void returns( Expression value );

void assign( TypeReference type, String name, Expression value );
void assign( LocalVariable local, Expression value );

void beginWhile( Expression test );

Expand All @@ -49,7 +49,7 @@ public interface MethodEmitter

void declare( LocalVariable local );

void assign( LocalVariable local, Expression value );
void assignVariableInScope( LocalVariable local, Expression value );

void beginForEach( Parameter local, Expression iterable );
}
Expand Up @@ -19,33 +19,37 @@
*/
package org.neo4j.codegen;

import java.lang.reflect.Modifier;

import static org.neo4j.codegen.TypeReference.typeReference;
import static org.neo4j.codegen.TypeReference.typeReferences;

public class MethodReference
{
public static MethodReference methodReference( Class<?> owner, Class<?> returns, String name,
Class<?>... parameters )
Class<?>... parameters ) throws NoSuchMethodException
{
return methodReference( typeReference( owner ), typeReference( returns ), name, typeReferences( parameters ) );
int modifiers = owner.getMethod( name, parameters ).getModifiers();
return methodReference( typeReference( owner ), typeReference( returns ), name, modifiers, typeReferences( parameters ) );
}

public static MethodReference methodReference( Class<?> owner, TypeReference returns, String name,
Class<?>... parameters )
Class<?>... parameters ) throws NoSuchMethodException
{
return methodReference( owner, returns, name, typeReferences( parameters ) );
int modifiers = owner.getMethod( name, parameters ).getModifiers();
return methodReference( owner, returns, name, modifiers, typeReferences( parameters ) );
}

public static MethodReference methodReference( Class<?> owner, TypeReference returns, String name,
private static MethodReference methodReference( Class<?> owner, TypeReference returns, String name, int modifiers,
TypeReference... parameters )
{
return methodReference( typeReference( owner ), returns, name, parameters );
return methodReference( typeReference( owner ), returns, name, modifiers, parameters );
}

public static MethodReference methodReference( TypeReference owner, TypeReference returns, String name,
TypeReference... parameters )
int modifiers, TypeReference... parameters )
{
return new MethodReference( owner, name, returns, parameters );
return new MethodReference( owner, name, returns, modifiers, parameters );
}

public static MethodReference constructorReference( Class<?> owner, Class<?> firstParameter, Class<?>... parameters )
Expand All @@ -60,20 +64,22 @@ public static MethodReference constructorReference( Class<?> owner, TypeReferenc

public static MethodReference constructorReference( TypeReference owner, TypeReference... parameters )
{
return new MethodReference( owner, "<init>", TypeReference.VOID, parameters );
return new MethodReference( owner, "<init>", TypeReference.VOID, Modifier.PUBLIC, parameters );
}

private final TypeReference owner;
private final String name;
private final TypeReference returns;
private final TypeReference[] parameters;
private final int modifiers;

MethodReference( TypeReference owner, String name, TypeReference returns, TypeReference[] parameters )
MethodReference( TypeReference owner, String name, TypeReference returns, int modifiers, TypeReference[] parameters)
{
this.owner = owner;

this.name = name;
this.returns = returns;
this.modifiers = modifiers;
this.parameters = parameters;
}

Expand Down Expand Up @@ -102,6 +108,11 @@ public boolean isConstructor()
return "<init>".equals( name );
}

public int modifiers()
{
return modifiers;
}

@Override
public String toString()
{
Expand Down
Expand Up @@ -19,6 +19,7 @@
*/
package org.neo4j.codegen;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -120,6 +121,7 @@ public abstract static class Builder
final Parameter[] parameters;
private final Map<String,TypeReference> locals = new HashMap<>();
private final List<Statement> statements = new ArrayList<>();
private int modifiers = Modifier.PUBLIC;

Builder( Parameter[] parameters )
{
Expand Down Expand Up @@ -162,6 +164,12 @@ public Builder put( ExpressionTemplate target, TypeReference fieldType, String f
return this;
}

public Builder modiferes(int modifiers)
{
this.modifiers = modifiers;
return this;
}

public Builder returns( ExpressionTemplate value )
{
statements.add( Statement.returns( value ) );
Expand All @@ -181,6 +189,11 @@ public String name()
return name;
}

public int modifiers()
{
return modifiers;
}

public TypeReference[] parameterTypes()
{
if ( parameters.length == 0 )
Expand Down Expand Up @@ -223,6 +236,7 @@ private static MethodTemplate buildConstructor( Builder builder )
private final Statement[] statements;
private final TypeReference returnType;
private final String name;
private final int modifiers;

private MethodTemplate( Builder builder, TypeReference returnType, String name )
{
Expand All @@ -231,5 +245,6 @@ private MethodTemplate( Builder builder, TypeReference returnType, String name )
this.declaration = builder.declaration();
this.parameters = builder.parameters;
this.statements = builder.statements.toArray( new Statement[builder.statements.size()] );
this.modifiers = builder.modifiers;
}
}

0 comments on commit 922e24c

Please sign in to comment.