Skip to content

Commit

Permalink
Added codegen support for depth 1 arrays of inner classes
Browse files Browse the repository at this point in the history
  • Loading branch information
fickludd committed Mar 8, 2017
1 parent 68b045e commit 93823c7
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 221 deletions.
Expand Up @@ -39,7 +39,7 @@ public static String byteCodeName( TypeReference reference )
{
builder.append( reference.declaringClassName() ).append( '$' );
}
builder.append( reference.simpleName() );
builder.append( reference.name() );
return builder.toString();
}

Expand Down Expand Up @@ -155,15 +155,10 @@ private static String internalSignature( TypeReference reference )
private static StringBuilder internalType( StringBuilder builder, TypeReference reference,
boolean showErasure )
{
String name;
String name = reference.name();
if ( reference.isArray() )
{
builder.append( "[" );
name = reference.simpleName().substring( 0, reference.simpleName().length() - 2 );
}
else
{
name = reference.simpleName();
}

switch ( name )
Expand Down
Expand Up @@ -54,7 +54,7 @@ public Object newInstance() throws CompilationFailureException, IllegalAccessExc

public Class<?> loadClass() throws CompilationFailureException
{
return generator.loadClass( name(), generation );
return generator.loadClass( fullName(), generation );
}

public TypeReference parent()
Expand Down
65 changes: 34 additions & 31 deletions community/codegen/src/main/java/org/neo4j/codegen/Expression.java
Expand Up @@ -511,35 +511,38 @@ public void accept( ExpressionVisitor visitor )
/** box expression */
public static Expression box( final Expression expression )
{
TypeReference type;
switch ( expression.type.simpleName() )
{
case "byte":
type = TypeReference.typeReference( Byte.class );
break;
case "short":
type = TypeReference.typeReference( Short.class );
break;
case "int":
type = TypeReference.typeReference( Integer.class );
break;
case "long":
type = TypeReference.typeReference( Long.class );
break;
case "char":
type = TypeReference.typeReference( Character.class );
break;
case "boolean":
type = TypeReference.typeReference( Boolean.class );
break;
case "float":
type = TypeReference.typeReference( Float.class );
break;
case "double":
type = TypeReference.typeReference( Double.class );
break;
default:
type = expression.type();
TypeReference type = expression.type;
if ( type.isPrimitive() )
{
switch ( type.name() )
{
case "byte":
type = TypeReference.typeReference( Byte.class );
break;
case "short":
type = TypeReference.typeReference( Short.class );
break;
case "int":
type = TypeReference.typeReference( Integer.class );
break;
case "long":
type = TypeReference.typeReference( Long.class );
break;
case "char":
type = TypeReference.typeReference( Character.class );
break;
case "boolean":
type = TypeReference.typeReference( Boolean.class );
break;
case "float":
type = TypeReference.typeReference( Float.class );
break;
case "double":
type = TypeReference.typeReference( Double.class );
break;
default:
break;
}
}
return new Expression( type )
{
Expand All @@ -555,7 +558,7 @@ public void accept( ExpressionVisitor visitor )
public static Expression unbox( final Expression expression )
{
TypeReference type;
switch ( expression.type.name() )
switch ( expression.type.fullName() )
{
case "java.lang.Byte":
type = TypeReference.typeReference( byte.class );
Expand All @@ -582,7 +585,7 @@ public static Expression unbox( final Expression expression )
type = TypeReference.typeReference( double.class );
break;
default:
throw new IllegalStateException( "Cannot unbox " + expression.type.name() );
throw new IllegalStateException( "Cannot unbox " + expression.type.fullName() );
}
return new Expression( type )
{
Expand Down
Expand Up @@ -241,7 +241,7 @@ public MethodDeclaration erased()

private TypeReference erase( TypeReference reference, Map<String,TypeReference> table )
{
TypeReference erasedReference = table.get( reference.name() );
TypeReference erasedReference = table.get( reference.fullName() );

return erasedReference != null ? erasedReference : reference;
}
Expand Down
Expand Up @@ -61,44 +61,43 @@ public static TypeReference typeReference( Class<?> type )
{
return OBJECT;
}
String packageName = "", simpleName, declaringClassName = "";
if ( type.isArray() )
{
simpleName = type.getComponentType().getCanonicalName() + "[]";
}
else if (type.isPrimitive())
String packageName = "", name, declaringClassName = "";

Class<?> innerType = type.isArray() ? type.getComponentType() : type;

if (innerType.isPrimitive())
{
simpleName = type.getName();
switch ( simpleName )
name = innerType.getName();
switch ( name )
{
case "boolean":
return BOOLEAN;
return type.isArray() ? BOOLEAN_ARRAY : BOOLEAN;
case "int":
return INT;
return type.isArray() ? INT_ARRAY : INT;
case "long":
return LONG;
return type.isArray() ? LONG_ARRAY : LONG;
case "double":
return DOUBLE;
return type.isArray() ? DOUBLE_ARRAY : DOUBLE;
default:
// continue through the normal path
}
}
else
{
packageName = type.getPackage().getName();
String canonicalName = type.getCanonicalName();
Class<?> declaringClass = type.getDeclaringClass();
packageName = innerType.getPackage().getName();
String canonicalName = innerType.getCanonicalName();
Class<?> declaringClass = innerType.getDeclaringClass();
if ( declaringClass != null)
{
declaringClassName = declaringClass.getSimpleName();
simpleName = canonicalName.substring( packageName.length() + declaringClassName.length() + 2 );
name = canonicalName.substring( packageName.length() + declaringClassName.length() + 2 );
}
else
{
simpleName = canonicalName.substring( packageName.length() + 1 );
name = canonicalName.substring( packageName.length() + 1 );
}
}
return new TypeReference( packageName, simpleName, type.isPrimitive(), type.isArray(), false,
return new TypeReference( packageName, name, type.isPrimitive(), type.isArray(), false,
declaringClassName, type.getModifiers() );
}

Expand All @@ -109,7 +108,7 @@ public static TypeReference typeParameter( String name )

public static TypeReference arrayOf( TypeReference type )
{
return new TypeReference( type.packageName, type.simpleName + "[]", false, true, false, type.declaringClassName, type.modifiers );
return new TypeReference( type.packageName, type.name, false, true, false, type.declaringClassName, type.modifiers );
}

public static TypeReference parameterizedType( Class<?> base, Class<?>... parameters )
Expand All @@ -124,7 +123,7 @@ public static TypeReference parameterizedType( Class<?> base, TypeReference... p

public static TypeReference parameterizedType( TypeReference base, TypeReference... parameters )
{
return new TypeReference( base.packageName, base.simpleName, false, base.isArray(), false,
return new TypeReference( base.packageName, base.name, false, base.isArray(), false,
base.declaringClassName,
base.modifiers, parameters );
}
Expand All @@ -151,7 +150,7 @@ public static TypeReference[] typeReferences( Class<?>[] types )
}

private final String packageName;
private final String simpleName;
private final String name;
private final TypeReference[] parameters;
private final boolean isPrimitive;
private final boolean isArray;
Expand All @@ -165,13 +164,17 @@ public static TypeReference[] typeReferences( Class<?>[] types )
public static final TypeReference INT = new TypeReference( "", "int", true, false, false, "", int.class.getModifiers() );
public static final TypeReference LONG = new TypeReference( "", "long", true, false, false, "", long.class.getModifiers() );
public static final TypeReference DOUBLE = new TypeReference( "", "double", true, false, false, "", double.class.getModifiers() );
public static final TypeReference BOOLEAN_ARRAY = new TypeReference( "", "boolean", false, true, false, "", boolean.class.getModifiers() );
public static final TypeReference INT_ARRAY = new TypeReference( "", "int", false, true, false, "", int.class.getModifiers() );
public static final TypeReference LONG_ARRAY = new TypeReference( "", "long", false, true, false, "", long.class.getModifiers() );
public static final TypeReference DOUBLE_ARRAY = new TypeReference( "", "double", false, true, false, "", double.class.getModifiers() );
static final TypeReference[] NO_TYPES = new TypeReference[0];

TypeReference( String packageName, String simpleName, boolean isPrimitive, boolean isArray,
TypeReference( String packageName, String name, boolean isPrimitive, boolean isArray,
boolean isTypeParameter, String declaringClassName, int modifiers, TypeReference... parameters )
{
this.packageName = packageName;
this.simpleName = simpleName;
this.name = name;
this.isPrimitive = isPrimitive;
this.isArray = isArray;
this.isTypeParameter = isTypeParameter;
Expand All @@ -185,9 +188,14 @@ public String packageName()
return packageName;
}

public String name()
{
return name;
}

public String simpleName()
{
return simpleName;
return isArray ? name + "[]" : name;
}

public boolean isPrimitive()
Expand All @@ -210,7 +218,7 @@ public List<TypeReference> parameters()
return unmodifiableList( asList( parameters ) );
}

public String name()
public String fullName()
{
return writeTo( new StringBuilder() ).toString();
}
Expand Down Expand Up @@ -260,7 +268,7 @@ public boolean equals( Object o )
{ return false; }
if ( packageName != null ? !packageName.equals( reference.packageName ) : reference.packageName != null )
{ return false; }
if ( simpleName != null ? !simpleName.equals( reference.simpleName ) : reference.simpleName != null )
if ( name != null ? !name.equals( reference.name ) : reference.name != null )
{ return false; }
// Probably incorrect - comparing Object[] arrays with Arrays.equals
if ( !Arrays.equals( parameters, reference.parameters ) )
Expand All @@ -274,7 +282,7 @@ public boolean equals( Object o )
public int hashCode()
{
int result = packageName != null ? packageName.hashCode() : 0;
result = 31 * result + (simpleName != null ? simpleName.hashCode() : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + Arrays.hashCode( parameters );
result = 31 * result + (isPrimitive ? 1 : 0);
result = 31 * result + (isArray ? 1 : 0);
Expand All @@ -300,7 +308,11 @@ StringBuilder writeTo( StringBuilder result )
{
result.append( declaringClassName ).append( '.' );
}
result.append( simpleName );
result.append( name );
if ( isArray )
{
result.append( "[]" );
}
if ( !(parameters == null || parameters.length == 0) )
{
result.append( '<' );
Expand Down

0 comments on commit 93823c7

Please sign in to comment.