-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ClassGenerator.java
162 lines (140 loc) · 5.48 KB
/
ClassGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.codegen;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static org.neo4j.codegen.MethodDeclaration.TypeParameter.NO_PARAMETERS;
import static org.neo4j.codegen.MethodDeclaration.constructor;
import static org.neo4j.codegen.MethodDeclaration.method;
import static org.neo4j.codegen.MethodReference.methodReference;
import static org.neo4j.codegen.TypeReference.NO_TYPES;
import static org.neo4j.codegen.TypeReference.typeReference;
public class ClassGenerator implements AutoCloseable
{
private final ClassHandle handle;
private ClassEmitter emitter;
private Map<String,FieldReference> fields;
private boolean hasConstructor = false;
ClassGenerator( ClassHandle handle, ClassEmitter emitter )
{
this.handle = handle;
this.emitter = emitter;
}
@Override
public void close()
{
if (!hasConstructor)
{
generate( MethodTemplate.constructor().invokeSuper().build() );
}
emitter.done();
handle.generator.closeClass();
emitter = InvalidState.CLASS_DONE;
}
public ClassHandle handle()
{
return handle;
}
public FieldReference field( Class<?> type, String name )
{
return field( typeReference( type ), name );
}
public FieldReference field( TypeReference type, String name )
{
return emitField( Modifier.PUBLIC, type, name, null );
}
public FieldReference staticField( Class<?> type, String name, Expression value )
{
return staticField( typeReference( type ), name, value );
}
public FieldReference staticField( TypeReference type, String name )
{
return emitField( Modifier.PUBLIC | Modifier.STATIC, type, name, null );
}
public FieldReference staticField( TypeReference type, String name, Expression value )
{
return emitField( Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL, type, name, Objects.requireNonNull( value ) );
}
private FieldReference emitField( int modifiers, TypeReference type, String name, Expression value )
{
if ( fields == null )
{
fields = new HashMap<>();
}
else if ( fields.containsKey( name ) )
{
throw new IllegalArgumentException( handle + " already has a field '" + name + "'" );
}
FieldReference field = new FieldReference( modifiers, handle, type, name );
fields.put( name, field );
emitter.field( field, value );
return field;
}
public MethodReference generate( MethodTemplate template, Binding... bindings )
{
try ( CodeBlock generator = generate( template.declaration( handle ) ) )
{
template.generate( generator );
}
return methodReference( handle, template.returnType(), template.name(), template.modifiers(), template.parameterTypes() );
}
public CodeBlock generateConstructor( Parameter... 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, 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, 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 )
{
return generate( builder.build( handle ) );
}
private CodeBlock generate( MethodDeclaration declaration )
{
if (declaration.isConstructor())
{
hasConstructor = true;
}
return new CodeBlock( this, emitter.method( declaration ), declaration.parameters() );
}
FieldReference getField( String name )
{
return fields == null ? null : fields.get( name );
}
}