use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.
the class ClassTransformer method addAtMembers.
private void addAtMembers(ClassDefinitionBuilder classBuilder, ClassOrInterface model, Tree.ClassOrInterface def) {
List<JCExpression> members = List.nil();
for (Declaration member : model.getMembers()) {
if (member instanceof ClassOrInterface == false && member instanceof TypeAlias == false) {
continue;
}
TypeDeclaration innerType = (TypeDeclaration) member;
Tree.Declaration innerTypeTree = findInnerType(def, innerType.getName());
if (innerTypeTree != null) {
TransformationPlan plan = errors().hasDeclarationAndMarkBrokenness(innerTypeTree);
if (plan instanceof Drop) {
continue;
}
}
if (innerType.isAlias() && innerTypeTree != null && Decl.isAncestorLocal(innerTypeTree.getDeclarationModel()))
// for the same reason we do not generate aliases in transform(ClassOrInterface def) let's not list them
continue;
JCAnnotation atMember;
// interfaces are moved to toplevel so they can lose visibility of member types if they are local
if (Decl.isLocal(model) && model instanceof Interface)
atMember = makeAtMember(innerType.getName());
else
atMember = makeAtMember(innerType.getType());
members = members.prepend(atMember);
}
classBuilder.annotations(makeAtMembers(members));
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.
the class ClassTransformer method transformMethod.
private List<MethodDefinitionBuilder> transformMethod(final Function methodModel, Tree.TypeParameterList typeParameterList, Tree.AnyMethod node, java.util.List<Tree.ParameterList> parameterLists, Tree.Declaration annotated, boolean transformMethod, boolean actual, boolean includeAnnotations, List<JCStatement> body, DaoBody daoTransformation, boolean defaultValuesBody) {
ListBuffer<MethodDefinitionBuilder> lb = new ListBuffer<MethodDefinitionBuilder>();
Declaration refinedDeclaration = methodModel.getRefinedDeclaration();
final MethodDefinitionBuilder methodBuilder = MethodDefinitionBuilder.method(this, methodModel);
// do the reified type param arguments
if (AbstractTransformer.supportsReified(methodModel)) {
methodBuilder.reifiedTypeParameters(Strategy.getEffectiveTypeParameters(methodModel));
}
if (methodModel.getParameterLists().size() > 1) {
methodBuilder.mpl(methodModel.getParameterLists());
}
boolean hasOverloads = false;
Tree.ParameterList parameterList = parameterLists.get(0);
int flags = 0;
if (rawParameters(methodModel)) {
flags |= JT_RAW;
}
for (final Tree.Parameter parameter : parameterList.getParameters()) {
Parameter parameterModel = parameter.getParameterModel();
List<JCAnnotation> annotations = null;
if (includeAnnotations) {
Tree.TypedDeclaration typedDeclaration = Decl.getMemberDeclaration(annotated, parameter);
// f = function(Integer param) => 2;
if (typedDeclaration != null)
annotations = expressionGen().transformAnnotations(OutputElement.PARAMETER, typedDeclaration);
}
// methodModel.getTypedReference().getTypedParameter(parameterModel).getType()
// parameterModel.getModel().getTypedReference().getType()
methodBuilder.parameter(parameter, parameterModel, annotations, flags, WideningRules.CAN_WIDEN);
if (Strategy.hasDefaultParameterValueMethod(parameterModel) || Strategy.hasDefaultParameterOverload(parameterModel)) {
if (Decl.equal(refinedDeclaration, methodModel) || (!methodModel.isInterfaceMember() && body != null) || methodModel.isInterfaceMember() && daoTransformation instanceof DaoCompanion == false) {
if (daoTransformation != null && (daoTransformation instanceof DaoCompanion == false || body != null)) {
DaoBody daoTrans = (body == null && !methodModel.isJavaNative()) ? daoAbstract : new DaoThis(node, parameterList);
MethodDefinitionBuilder overloadedMethod = new DefaultedArgumentMethod(daoTrans, MethodDefinitionBuilder.method(this, methodModel), methodModel).makeOverload(parameterList.getModel(), parameter.getParameterModel(), Strategy.getEffectiveTypeParameters(methodModel));
overloadedMethod.location(null);
lb.append(overloadedMethod);
}
if (Decl.equal(refinedDeclaration, methodModel) && Strategy.hasDefaultParameterValueMethod(parameterModel)) {
lb.append(makeParamDefaultValueMethod(defaultValuesBody, methodModel, parameterList, parameter));
}
}
hasOverloads = true;
}
}
// Determine if we need to generate a "canonical" method
boolean createCanonical = hasOverloads && methodModel.isClassOrInterfaceMember() && (body != null || methodModel.isJavaNative());
if (createCanonical) {
// Creates the private "canonical" method containing the actual body
MethodDefinitionBuilder canonicalMethod = new CanonicalMethod(daoTransformation, methodModel, body).makeOverload(parameterList.getModel(), null, Strategy.getEffectiveTypeParameters(methodModel));
lb.append(canonicalMethod);
}
if (transformMethod) {
methodBuilder.modifiers(modifierTransformation().method(methodModel) | (methodModel.isJavaNative() && !createCanonical ? Flags.NATIVE : 0));
if (actual) {
methodBuilder.isOverride(methodModel.isActual());
}
if (includeAnnotations) {
methodBuilder.userAnnotations(expressionGen().transformAnnotations(OutputElement.METHOD, annotated));
methodBuilder.modelAnnotations(methodModel.getAnnotations());
if (!methodModel.isDefault() && isEe(methodModel)) {
methodBuilder.modelAnnotations(makeAtFinal());
}
} else {
methodBuilder.ignoreModelAnnotations();
}
methodBuilder.resultType(methodModel, 0);
copyTypeParameters(methodModel, methodBuilder);
if (createCanonical) {
// Creates method that redirects to the "canonical" method containing the actual body
MethodDefinitionBuilder overloadedMethod = new CanonicalMethod(new DaoThis(node, parameterList), methodBuilder, methodModel).makeOverload(parameterList.getModel(), null, Strategy.getEffectiveTypeParameters(methodModel));
lb.append(overloadedMethod);
} else {
if (body != null) {
// Construct the outermost method using the body we've built so far
methodBuilder.body(body);
} else {
methodBuilder.noBody();
}
lb.append(methodBuilder);
}
}
return lb.toList();
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.
the class AttributeDefinitionBuilder method makeValueField.
/**
* Make the declaration of the value field
*/
private JCStatement makeValueField() {
List<JCAnnotation> annos = fieldAnnotations != null ? fieldAnnotations.toList() : List.<JCAnnotation>nil();
JCExpression type = valueFieldType();
if (fieldNullability != null && !TransformedType.isPrimitive(type)) {
annos = annos.append(fieldNullability);
}
return owner.make().VarDef(owner.make().Modifiers(valueFieldModifiers(), annos), owner.names().fromString(Naming.quoteFieldName(fieldName)), type, null);
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.
the class CeylonTransformer method transformAttribute.
public List<JCTree> transformAttribute(TypedDeclaration declarationModel, String attrName, String attrClassName, final Tree.Declaration annotated, final Tree.Block block, final Tree.SpecifierOrInitializerExpression expression, final Tree.TypedDeclaration decl, final Tree.AttributeSetterDefinition setterDecl) {
// For everything else generate a getter/setter method
ClassDefinitionBuilder classBuilder = ClassDefinitionBuilder.klass(this, attrClassName, null, false);
final HasErrorException expressionError;
if (expression != null) {
expressionError = errors().getFirstExpressionErrorAndMarkBrokenness(expression.getExpression());
} else {
expressionError = null;
}
BoxingStrategy boxingStrategy = null;
final JCExpression initialValue;
if (expressionError != null) {
initialValue = make().Erroneous();
} else {
if (Decl.isNonTransientValue(declarationModel) && !(expression instanceof Tree.LazySpecifierExpression)) {
if (expression != null) {
boxingStrategy = useJavaBox(declarationModel, declarationModel.getType()) && javaBoxExpression(expression.getExpression().getTypeModel(), declarationModel.getType()) ? BoxingStrategy.JAVA : CodegenUtil.getBoxingStrategy(declarationModel);
initialValue = expressionGen().transform(expression, boxingStrategy, declarationModel.getType());
} else {
Parameter p = CodegenUtil.findParamForDecl(attrName, declarationModel);
if (p != null) {
boxingStrategy = CodegenUtil.getBoxingStrategy(p.getModel());
initialValue = naming.makeName(p.getModel(), Naming.NA_MEMBER | Naming.NA_ALIASED);
} else {
initialValue = null;
}
}
} else {
initialValue = null;
}
}
boolean memoized = declarationModel.isLate() && expression != null;
AttributeDefinitionBuilder builder = AttributeDefinitionBuilder.wrapped(this, attrName, declarationModel, declarationModel.isToplevel(), memoized ? initialValue : null);
builder.is(Flags.PUBLIC, declarationModel.isShared());
if (isJavaStrictfp(declarationModel)) {
builder.is(Flags.STRICTFP, true);
}
if (isJavaSynchronized(declarationModel)) {
builder.is(Flags.SYNCHRONIZED, true);
}
if (isJavaNative(declarationModel)) {
builder.is(Flags.NATIVE, true);
builder.isJavaNative(true);
}
// For captured local variable Values, use a VariableBox
if (Decl.isBoxedVariable(declarationModel)) {
classBuilder.restoreClassBuilder();
if (expressionError != null) {
return List.<JCTree>of(this.makeThrowUnresolvedCompilationError(expressionError));
} else {
return List.<JCTree>of(makeVariableBoxDecl(initialValue, declarationModel));
}
}
// For late-bound getters we only generate a declaration
if (block == null && expression == null && !declarationModel.isToplevel()) {
JCExpression typeExpr = makeJavaType(getGetterInterfaceType(declarationModel));
JCTree.JCVariableDecl var = makeVar(attrClassName, typeExpr, null);
classBuilder.restoreClassBuilder();
return List.<JCTree>of(var);
}
// Set the local declarations annotation
List<JCAnnotation> scopeAnnotations = null;
if (decl != null) {
scopeAnnotations = declarationModel.isToplevel() && setterDecl != null ? makeAtLocalDeclarations(decl, setterDecl) : makeAtLocalDeclarations(decl);
} else if (block != null) {
scopeAnnotations = makeAtLocalDeclarations(block);
}
if (scopeAnnotations != null)
builder.classAnnotations(scopeAnnotations);
// Remember the setter class if we generate a getter
if (Decl.isGetter(declarationModel) && declarationModel.isVariable() && Decl.isLocal(declarationModel)) {
// we must have a setter class
Setter setter = ((Value) declarationModel).getSetter();
if (setter != null) {
String setterClassName = Naming.getAttrClassName(setter, 0);
JCExpression setterClassNameExpr = naming.makeUnquotedIdent(setterClassName);
builder.setterClass(makeSelect(setterClassNameExpr, "class"));
}
}
if (declarationModel instanceof Setter || (declarationModel instanceof FunctionOrValue && ((FunctionOrValue) declarationModel).isParameter())) {
// For local setters
JCBlock setterBlock = makeSetterBlock(declarationModel, block, expression);
builder.setterBlock(setterBlock);
builder.skipGetter();
if (Decl.isLocal(decl.getDeclarationModel())) {
// we need to find back the Setter model for local setters, because
// in transformAttribute(Tree.TypedDeclaration decl, Tree.AttributeSetterDefinition setterDecl)
// we turn the declaration model from the Setter to its single parameter
Setter setter = (Setter) declarationModel.getContainer();
String getterClassName = Naming.getAttrClassName(setter.getGetter(), 0);
JCExpression getterClassNameExpr = naming.makeUnquotedIdent(getterClassName);
builder.isSetter(makeSelect(getterClassNameExpr, "class"));
}
} else {
if (Decl.isValue(declarationModel)) {
// For local and toplevel value attributes
if (!declarationModel.isVariable() && !declarationModel.isLate()) {
builder.immutable();
}
} else {
// For local and toplevel getters
boolean prevSyntheticClassBody = Decl.isLocal(declarationModel) ? expressionGen().withinSyntheticClassBody(true) : expressionGen().isWithinSyntheticClassBody();
JCBlock getterBlock = makeGetterBlock(declarationModel, block, expression);
prevSyntheticClassBody = expressionGen().withinSyntheticClassBody(prevSyntheticClassBody);
builder.getterBlock(getterBlock);
if (Decl.isLocal(declarationModel)) {
// For local getters
builder.immutable();
} else {
// For toplevel getters
if (setterDecl != null) {
JCBlock setterBlock = makeSetterBlock(setterDecl.getDeclarationModel(), setterDecl.getBlock(), setterDecl.getSpecifierExpression());
builder.setterBlock(setterBlock);
// builder.userAnnotationsSetter(expressionGen().transformAnnotations(true, OutputElement.METHOD, setterDecl));
builder.userAnnotationsSetter(expressionGen().transformAnnotations(OutputElement.SETTER, setterDecl));
} else {
builder.immutable();
}
}
}
}
if (annotated != null) {
builder.userAnnotations(expressionGen().transformAnnotations(OutputElement.GETTER, annotated));
builder.fieldAnnotations(expressionGen().transformAnnotations(OutputElement.FIELD, annotated));
}
if (Decl.isLocal(declarationModel)) {
if (expressionError != null) {
classBuilder.restoreClassBuilder();
return List.<JCTree>of(this.makeThrowUnresolvedCompilationError(expressionError));
}
builder.classAnnotations(makeAtLocalDeclaration(declarationModel.getQualifier(), false));
if (initialValue != null)
builder.valueConstructor();
JCExpression typeExpr;
if (declarationModel instanceof Setter || (declarationModel instanceof FunctionOrValue && ((FunctionOrValue) declarationModel).isParameter())) {
typeExpr = makeQuotedIdent(attrClassName);
} else {
typeExpr = makeJavaType(getGetterInterfaceType(declarationModel));
}
return builder.buildWithWrapperClass(classBuilder).append(makeLocalIdentityInstance(typeExpr, attrClassName, attrClassName, declarationModel.isShared(), initialValue));
} else {
if (expressionError != null) {
builder.initialValueError(expressionError);
} else if (!memoized && initialValue != null) {
builder.initialValue(initialValue, boxingStrategy);
}
builder.is(Flags.STATIC, true);
return builder.buildWithWrapperClass(classBuilder);
}
}
use of org.eclipse.ceylon.langtools.tools.javac.tree.JCTree.JCAnnotation in project ceylon by eclipse.
the class CollectionLiteralAnnotationTerm method makeDpmAnnotations.
@Override
public org.eclipse.ceylon.langtools.tools.javac.util.List<JCAnnotation> makeDpmAnnotations(ExpressionTransformer exprGen) {
if (factory == null) {
// A tuple
// TODO @TupleValue({elements...})
} else {
// A sequence
ListBuffer<JCExpression> lb = new ListBuffer<JCExpression>();
for (AnnotationTerm term : elements) {
lb.add(((LiteralAnnotationTerm) term).makeLiteral(exprGen));
}
JCNewArray array = exprGen.make().NewArray(null, null, lb.toList());
return factory.makeAtValue(exprGen, null, array);
}
return org.eclipse.ceylon.langtools.tools.javac.util.List.<JCAnnotation>nil();
}
Aggregations