use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement in project ceylon by eclipse.
the class InitializerBuilder method build.
/**
* Only called for classes with parameter lists
*/
JCMethodDecl build() {
if (delegateCall != null) /* && !isAlias*/
{
init.add(0, delegateCall);
}
List<JCStatement> body = statementsBetween(null, null);
int index = 0;
for (JCStatement stmt : body) {
if (stmt instanceof JCThrow) {
ListBuffer<JCStatement> filtered = new ListBuffer<JCStatement>();
filtered.addAll(body.subList(0, index + 1));
body = filtered.toList();
break;
}
index++;
}
MethodDefinitionBuilder constructor = MethodDefinitionBuilder.constructor(gen, deprecated);
constructor.modifiers(modifiers).userAnnotations(userAnnos.toList()).parameters(params.toList()).body(body);
return constructor.build();
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement in project ceylon by eclipse.
the class ClassTransformer method serializationConstructor.
/**
* <p>Generates the serialization constructor
* with signature {@code ($Serialization$)} which:</p>
* <ul>
* <li>invokes {@code super()}, if the super class is also
* serializable,</li>
* <li>initializes all companion instance fields to a
* newly instantiated companion instance,</li>
* <li>initializes all reified type argument fields to null,</li>
* <li>initializes all reference attribute fields to null,</li>
* <li>initializesall primitive attribute fields to a default
* value (basically some kind of 0)</li>
* </ul>
*/
private void serializationConstructor(Class model, ClassDefinitionBuilder classBuilder) {
MethodDefinitionBuilder ctor = classBuilder.addConstructor(model.isDeprecated());
ctor.ignoreModelAnnotations();
ctor.modifiers(PUBLIC);
ParameterDefinitionBuilder serializationPdb = ParameterDefinitionBuilder.systemParameter(this, "ignored");
serializationPdb.modifiers(FINAL);
serializationPdb.type(new TransformedType(make().Type(syms().ceylonSerializationType), null));
ctor.parameter(serializationPdb);
for (TypeParameter tp : model.getTypeParameters()) {
ctor.reifiedTypeParameter(tp);
}
final ListBuffer<JCStatement> stmts = new ListBuffer<JCStatement>();
if (extendsSerializable(model)) {
// invoke super
ListBuffer<JCExpression> superArgs = new ListBuffer<JCExpression>();
superArgs.add(naming.makeUnquotedIdent("ignored"));
for (JCExpression ta : makeReifiedTypeArguments(model.getExtendedType())) {
superArgs.add(ta);
}
stmts.add(make().Exec(make().Apply(null, naming.makeSuper(), superArgs.toList())));
}
buildFieldInits(model, classBuilder, stmts);
ctor.body(stmts.toList());
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement in project ceylon by eclipse.
the class ClassTransformer method buildJpaConstructor.
protected void buildJpaConstructor(Class model, ClassDefinitionBuilder classBuilder) {
MethodDefinitionBuilder ctor = classBuilder.addConstructor(model.isDeprecated());
ctor.modelAnnotations(makeAtJpa());
ctor.modelAnnotations(makeAtIgnore());
ctor.modifiers(modifierTransformation().jpaConstructor(model));
for (TypeParameter tp : model.getTypeParameters()) {
ctor.reifiedTypeParameter(tp);
}
final ListBuffer<JCStatement> stmts = new ListBuffer<JCStatement>();
// invoke super (or this if
ListBuffer<JCExpression> superArgs = new ListBuffer<JCExpression>();
if (model.isSerializable()) {
superArgs.add(make().TypeCast(make().QualIdent(syms().ceylonSerializationType.tsym), makeNull()));
for (JCExpression ta : makeReifiedTypeArguments(model.getType())) {
superArgs.add(ta);
}
} else {
for (JCExpression ta : makeReifiedTypeArguments(model.getExtendedType())) {
superArgs.add(ta);
}
}
stmts.add(make().Exec(make().Apply(null, model.isSerializable() ? naming.makeThis() : naming.makeSuper(), superArgs.toList())));
if (!model.isSerializable()) {
buildFieldInits(model, classBuilder, stmts);
}
ctor.body(stmts.toList());
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement in project ceylon by eclipse.
the class ClassTransformer method makeDeserializationAssignment.
private JCStatement makeDeserializationAssignment(Value value, boolean[] requiredLookup) {
boolean isValueType = Decl.isValueTypeDecl(simplifyType(value.getType()));
Naming.SyntheticName n = naming.synthetic(Unfix.instance);
JCExpression newValue = make().TypeCast(makeJavaType(value.getType(), JT_NO_PRIMITIVES), n.makeIdent());
if (isValueType) {
// FIXME: check strings
BoxingStrategy boxingStrategy = useJavaBox(value, value.getType()) ? BoxingStrategy.JAVA : CodegenUtil.getBoxingStrategy(value);
Type simpleType = simplifyType(value.getType());
newValue = expressionGen().applyErasureAndBoxing(newValue, simpleType, true, boxingStrategy, simpleType);
if (boxingStrategy == BoxingStrategy.JAVA && isOptional(value.getType()) && !isJavaString(simpleType)) {
newValue = make().Conditional(make().Binary(JCTree.Tag.EQ, n.makeIdent(), makeNull()), makeNull(), newValue);
}
} else {
// We need to obtain the instance from the reference
// but we don't need the instance to be fully deserialized
}
final JCStatement assignment;
if (value.isToplevel() || value.isLate()) {
// XXX duplicates logic in AttributeDefinitionBuilder
// We use the setter for late values, since that will allocate
// the array if needed.
assignment = make().Exec(make().Apply(null, naming.makeQualifiedName(naming.makeThis(), value, Naming.NA_MEMBER | Naming.NA_SETTER), List.of(newValue)));
} else {
// We bypass the setter
if (value.isVariable()) {
assignment = make().Exec(make().Assign(naming.makeQualifiedName(naming.makeThis(), value, Naming.NA_IDENT), newValue));
} else {
// The field will have final modifier, so we need some
// jiggery pokery to reset it.
requiredLookup[0] = true;
String fieldName = value.getName();
// TODO probably wrong
JCExpression fieldType = makeJavaType(value.getType());
assignment = makeReassignFinalField(fieldType, fieldName, newValue);
}
}
return assignment;
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCStatement in project ceylon by eclipse.
the class ClassTransformer method transformMethodBody.
private List<JCStatement> transformMethodBody(Tree.AnyMethod def) {
List<JCStatement> body = null;
final Function model = def.getDeclarationModel();
if (model.isDeferred()) {
// Uninitialized or deferred initialized method => Make a Callable field
String fieldName = naming.selector(model);
final Parameter initializingParameter = CodegenUtil.findParamForDecl(def);
int mods = PRIVATE;
JCExpression initialValue;
if (initializingParameter != null) {
mods |= FINAL;
initialValue = makeUnquotedIdent(Naming.getAliasedParameterName(initializingParameter));
} else {
// The field isn't initialized by a parameter, but later in the block
initialValue = makeNull();
}
Type callableType = model.getReference().getFullType();
current().field(mods, fieldName, makeJavaType(callableType), initialValue, false);
Invocation invocation = new CallableSpecifierInvocation(this, model, makeUnquotedIdent(fieldName), // but with deferred methods we can't define them so that they are erased so we're good
null, def);
invocation.handleBoxing(true);
JCExpression call = expressionGen().transformInvocation(invocation);
JCStatement stmt;
if (!isVoid(def) || !Decl.isUnboxedVoid(model) || Strategy.useBoxedVoid((Function) model)) {
stmt = make().Return(call);
} else {
stmt = make().Exec(call);
}
JCStatement result;
if (initializingParameter == null) {
// If the field isn't initialized by a parameter we have to
// cope with the possibility that it's never initialized
final JCBinary cond = make().Binary(JCTree.Tag.EQ, makeUnquotedIdent(fieldName), makeNull());
final JCStatement throw_ = make().Throw(make().NewClass(null, null, makeIdent(syms().ceylonUninitializedMethodErrorType), List.<JCExpression>nil(), null));
result = make().If(cond, throw_, stmt);
} else {
result = stmt;
}
return List.<JCStatement>of(result);
} else if (def instanceof Tree.MethodDefinition) {
body = transformMethodBlock((Tree.MethodDefinition) def);
} else if (def instanceof MethodDeclaration && ((MethodDeclaration) def).getSpecifierExpression() != null) {
body = transformSpecifiedMethodBody((MethodDeclaration) def, ((MethodDeclaration) def).getSpecifierExpression());
}
return body;
}
Aggregations