use of com.redhat.ceylon.compiler.java.codegen.MethodDefinitionBuilder.WideningRules in project ceylon-compiler by ceylon.
the class ClassTransformer method makeParamDefaultValueMethod.
/**
* Creates a (possibly abstract) method for retrieving the value for a
* defaulted parameter
* @param typeParameterList
*/
MethodDefinitionBuilder makeParamDefaultValueMethod(boolean noBody, Declaration container, Tree.ParameterList params, Tree.Parameter currentParam) {
at(currentParam);
Parameter parameter = currentParam.getParameterModel();
if (!Strategy.hasDefaultParameterValueMethod(parameter)) {
throw new BugException();
}
MethodDefinitionBuilder methodBuilder = MethodDefinitionBuilder.systemMethod(this, Naming.getDefaultedParamMethodName(container, parameter));
methodBuilder.ignoreModelAnnotations();
if (container != null && Decl.isAnnotationConstructor(container)) {
AnnotationInvocation ac = (AnnotationInvocation) ((Function) container).getAnnotationConstructor();
for (AnnotationConstructorParameter acp : ac.getConstructorParameters()) {
if (acp.getParameter().equals(parameter) && acp.getDefaultArgument() != null) {
methodBuilder.userAnnotations(acp.getDefaultArgument().makeDpmAnnotations(expressionGen()));
}
}
}
int modifiers = 0;
if (noBody) {
modifiers |= PUBLIC | ABSTRACT;
} else if (container == null || !(container instanceof Class && Strategy.defaultParameterMethodStatic(container))) {
// initializers can override parameter defaults
modifiers |= FINAL;
}
if (container != null && container.isShared()) {
modifiers |= PUBLIC;
} else if (container == null || (!container.isToplevel() && !noBody)) {
modifiers |= PRIVATE;
}
boolean staticMethod = Strategy.defaultParameterMethodStatic(container);
if (staticMethod) {
// static default parameter methods should be consistently public so that if non-shared class Top and
// shared class Bottom which extends Top both have the same default param name, we don't get an error
// if the Bottom class tries to "hide" a static public method with a private one
modifiers &= ~PRIVATE;
modifiers |= STATIC | PUBLIC;
}
methodBuilder.modifiers(modifiers);
if (container instanceof Constructor) {
copyTypeParameters((Class) container.getContainer(), methodBuilder);
methodBuilder.reifiedTypeParameters(((Class) container.getContainer()).getTypeParameters());
} else if (container instanceof Generic) {
// make sure reified type parameters are accepted
copyTypeParameters((Generic) container, methodBuilder);
methodBuilder.reifiedTypeParameters(((Generic) container).getTypeParameters());
}
WideningRules wideningRules = !staticMethod && container instanceof Class ? WideningRules.CAN_WIDEN : WideningRules.NONE;
// Add any of the preceding parameters as parameters to the method
for (Tree.Parameter p : params.getParameters()) {
if (p.equals(currentParam)) {
break;
}
at(p);
methodBuilder.parameter(p.getParameterModel(), null, 0, wideningRules);
}
// The method's return type is the same as the parameter's type
NonWideningParam nonWideningParam = methodBuilder.getNonWideningParam(currentParam.getParameterModel().getModel(), wideningRules);
methodBuilder.resultType(nonWideningParam.nonWideningDecl, nonWideningParam.nonWideningType, nonWideningParam.flags);
// The implementation of the method
if (noBody) {
methodBuilder.noBody();
} else {
HasErrorException error = errors().getFirstExpressionErrorAndMarkBrokenness(Decl.getDefaultArgument(currentParam).getExpression());
if (error != null) {
methodBuilder.body(this.makeThrowUnresolvedCompilationError(error));
} else {
java.util.List<TypeParameter> copiedTypeParameters = null;
if (container instanceof Generic) {
copiedTypeParameters = ((Generic) container).getTypeParameters();
if (copiedTypeParameters != null)
addTypeParameterSubstitution(copiedTypeParameters);
}
try {
JCExpression expr = expressionGen().transform(currentParam);
JCBlock body = at(currentParam).Block(0, List.<JCStatement>of(at(currentParam).Return(expr)));
methodBuilder.block(body);
} finally {
if (copiedTypeParameters != null)
popTypeParameterSubstitution();
}
}
}
return methodBuilder;
}
Aggregations