use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.
the class ParameterAnnotationTerm method encode.
@Override
public int encode(AbstractTransformer gen, ListBuffer<JCExpression> instantiations) {
Parameter parameter = getSourceParameter();
int index = ((Functional) parameter.getDeclaration()).getFirstParameterList().getParameters().indexOf(parameter);
if (isSpread()) {
index += 256;
}
return index;
}
use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.
the class ModelLoaderTests method functionalParameterParameterNames.
@Test
public void functionalParameterParameterNames() {
compile("FunctionalParameterParameterNames.ceylon");
try {
verifyCompilerClassLoading("functionalparameterparameternamestest.ceylon", new RunnableTest() {
@Override
public void test(ModelLoader loader) {
Module mod = loader.getLoadedModule(moduleForJavaModelLoading(), moduleVersionForJavaModelLoading());
Assert.assertNotNull(mod);
Package p = mod.getDirectPackage(packageForJavaModelLoading());
Assert.assertNotNull(p);
Declaration fpClass = p.getDirectMember("FunctionalParameterParameterNames", Collections.<Type>emptyList(), false);
Assert.assertNotNull(fpClass);
{
// functionalParameter
Function fp = (Function) fpClass.getDirectMember("functionalParameter", null, false);
Assert.assertNotNull(fp);
Assert.assertEquals(1, fp.getParameterLists().size());
Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
Assert.assertEquals("Anything(Anything(String))", typeName(fp));
Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
Assert.assertTrue(paramF.isDeclaredAnything());
Assert.assertEquals("f", paramF.getName());
Assert.assertTrue(paramF.getModel() instanceof Function);
Function modelF = (Function) paramF.getModel();
Assert.assertTrue(modelF.isDeclaredVoid());
Assert.assertEquals("Anything(String)", typeName(modelF));
Assert.assertEquals("f", modelF.getName());
Assert.assertEquals(1, modelF.getParameterLists().size());
Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
Parameter paramS = modelF.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("s", paramS.getName());
Assert.assertTrue(paramS.getModel() instanceof Value);
Value modelS = (Value) paramS.getModel();
Assert.assertEquals("s", modelS.getName());
Assert.assertEquals("String", typeName(modelS));
}
{
// callableValueParameter
Function fp = (Function) fpClass.getDirectMember("callableValueParameter", null, false);
Assert.assertNotNull(fp);
Assert.assertEquals(1, fp.getParameterLists().size());
Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
Assert.assertEquals("Anything(Anything(String))", typeName(fp));
Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("f", paramF.getName());
Assert.assertFalse(paramF.isDeclaredAnything());
Assert.assertTrue(paramF.getModel() instanceof Value);
Value modelF = (Value) paramF.getModel();
Assert.assertEquals("f", modelF.getName());
Assert.assertEquals("Anything(String)", typeName(modelF));
}
{
// functionalParameterNested
Function fp = (Function) fpClass.getDirectMember("functionalParameterNested", null, false);
Assert.assertNotNull(fp);
Assert.assertEquals(1, fp.getParameterLists().size());
Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
Assert.assertEquals("Anything(Anything(Anything(String)))", typeName(fp));
Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("f", paramF.getName());
Assert.assertTrue(paramF.isDeclaredAnything());
Assert.assertTrue(paramF.getModel() instanceof Function);
Function modelF = (Function) paramF.getModel();
Assert.assertEquals("Anything(Anything(String))", typeName(modelF));
Assert.assertEquals("f", modelF.getName());
Assert.assertTrue(modelF.isDeclaredVoid());
Assert.assertEquals(1, modelF.getParameterLists().size());
Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
Parameter paramF2 = modelF.getParameterLists().get(0).getParameters().get(0);
Assert.assertTrue(paramF2.isDeclaredAnything());
Assert.assertEquals("f2", paramF2.getName());
Assert.assertTrue(paramF2.getModel() instanceof Function);
Function modelF2 = (Function) paramF2.getModel();
Assert.assertEquals("Anything(String)", typeName(modelF2));
Assert.assertEquals("f2", modelF2.getName());
Assert.assertTrue(modelF2.isDeclaredVoid());
Assert.assertEquals(1, modelF2.getParameterLists().size());
Assert.assertEquals(1, modelF2.getParameterLists().get(0).getParameters().size());
Parameter paramS = modelF2.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("s", paramS.getName());
Assert.assertTrue(paramS.getModel() instanceof Value);
Value modelS = (Value) paramS.getModel();
Assert.assertEquals("s", modelS.getName());
Assert.assertEquals("String", typeName(modelS));
}
{
// functionalParameterNested2
Function fp = (Function) fpClass.getDirectMember("functionalParameterNested2", null, false);
Assert.assertNotNull(fp);
Assert.assertEquals(1, fp.getParameterLists().size());
Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
Assert.assertEquals("Anything(Anything(Anything(String, Anything(Boolean, Integer))))", typeName(fp));
Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("f", paramF.getName());
Assert.assertTrue(paramF.getModel() instanceof Function);
Function modelF = (Function) paramF.getModel();
Assert.assertEquals("f", modelF.getName());
Assert.assertEquals("Anything(Anything(String, Anything(Boolean, Integer)))", typeName(modelF));
Assert.assertEquals(1, modelF.getParameterLists().size());
Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
Parameter paramF2 = modelF.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("f2", paramF2.getName());
Assert.assertTrue(paramF2.getModel() instanceof Function);
Function modelF2 = (Function) paramF2.getModel();
Assert.assertEquals("Anything(String, Anything(Boolean, Integer))", typeName(modelF2));
Assert.assertEquals("f2", modelF2.getName());
Assert.assertEquals(1, modelF2.getParameterLists().size());
Assert.assertEquals(2, modelF2.getParameterLists().get(0).getParameters().size());
Parameter paramS = modelF2.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("s", paramS.getName());
Assert.assertTrue(paramS.getModel() instanceof Value);
Value modelS = (Value) paramS.getModel();
Assert.assertEquals("String", typeName(modelS));
Assert.assertEquals("s", modelS.getName());
Parameter paramF3 = modelF2.getParameterLists().get(0).getParameters().get(1);
Assert.assertEquals("f3", paramF3.getName());
Assert.assertTrue(paramF3.getModel() instanceof Function);
Function modelF3 = (Function) paramF3.getModel();
Assert.assertEquals("Anything(Boolean, Integer)", typeName(modelF3));
Assert.assertEquals("f3", modelF3.getName());
Assert.assertEquals(1, modelF3.getParameterLists().size());
Assert.assertEquals(2, modelF3.getParameterLists().get(0).getParameters().size());
Parameter paramB1 = modelF3.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("b1", paramB1.getName());
Assert.assertTrue(paramB1.getModel() instanceof Value);
Value modelB1 = (Value) paramB1.getModel();
Assert.assertEquals("Boolean", typeName(modelB1));
Assert.assertEquals("b1", modelB1.getName());
Parameter paramI2 = modelF3.getParameterLists().get(0).getParameters().get(1);
Assert.assertEquals("i2", paramI2.getName());
Assert.assertTrue(paramI2.getModel() instanceof Value);
Value modelI2 = (Value) paramI2.getModel();
Assert.assertEquals("i2", modelI2.getName());
Assert.assertEquals("Integer", typeName(modelI2));
}
{
// functionalParameterMpl
Function fp = (Function) fpClass.getDirectMember("functionalParameterMpl", null, false);
Assert.assertNotNull(fp);
Assert.assertEquals(1, fp.getParameterLists().size());
Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
Assert.assertEquals("Anything(Anything(Integer)(String))", typeName(fp));
Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
Assert.assertTrue(paramF.isDeclaredAnything());
Assert.assertEquals("mpl", paramF.getName());
Assert.assertTrue(paramF.getModel() instanceof Function);
Function modelF = (Function) paramF.getModel();
Assert.assertTrue(modelF.isDeclaredVoid());
Assert.assertEquals("Anything(Integer)(String)", typeName(modelF));
Assert.assertEquals("mpl", modelF.getName());
Assert.assertEquals(2, modelF.getParameterLists().size());
Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
Assert.assertEquals(1, modelF.getParameterLists().get(1).getParameters().size());
Parameter paramS = modelF.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("s", paramS.getName());
Assert.assertTrue(paramS.getModel() instanceof Value);
Value modelS = (Value) paramS.getModel();
Assert.assertEquals("s", modelS.getName());
Assert.assertEquals("String", typeName(modelS));
Parameter paramS2 = modelF.getParameterLists().get(1).getParameters().get(0);
Assert.assertEquals("i2", paramS2.getName());
Assert.assertTrue(paramS2.getModel() instanceof Value);
Value modelS2 = (Value) paramS2.getModel();
Assert.assertEquals("i2", modelS2.getName());
Assert.assertEquals("Integer", typeName(modelS2));
}
{
// functionalParameterMpl2
Function fp = (Function) fpClass.getDirectMember("functionalParameterMpl2", null, false);
Assert.assertNotNull(fp);
Assert.assertEquals(1, fp.getParameterLists().size());
Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
Parameter paramMpl = fp.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("mpl", paramMpl.getName());
Assert.assertTrue(paramMpl.getModel() instanceof Function);
Function modelMpl = (Function) paramMpl.getModel();
Assert.assertEquals("mpl", modelMpl.getName());
Assert.assertEquals(2, modelMpl.getParameterLists().size());
Assert.assertEquals(1, modelMpl.getParameterLists().get(0).getParameters().size());
Assert.assertEquals(1, modelMpl.getParameterLists().get(1).getParameters().size());
Parameter paramS = modelMpl.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("s", paramS.getName());
Assert.assertTrue(paramS.getModel() instanceof Value);
Value modelS = (Value) paramS.getModel();
Assert.assertEquals("s", modelS.getName());
Assert.assertEquals("String", typeName(modelS));
Parameter paramF = modelMpl.getParameterLists().get(1).getParameters().get(0);
Assert.assertEquals("f", paramF.getName());
Assert.assertTrue(paramF.getModel() instanceof Function);
Function modelF = (Function) paramF.getModel();
Assert.assertEquals("f", modelF.getName());
Assert.assertEquals(1, modelF.getParameterLists().size());
Assert.assertEquals(2, modelF.getParameterLists().get(0).getParameters().size());
Parameter paramB1 = modelF.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("b1", paramB1.getName());
Assert.assertTrue(paramB1.getModel() instanceof Value);
Value modelB1 = (Value) paramB1.getModel();
Assert.assertEquals("b1", modelB1.getName());
Assert.assertEquals("Boolean", typeName(modelB1));
Parameter paramI2 = modelF.getParameterLists().get(0).getParameters().get(1);
Assert.assertEquals("i2", paramI2.getName());
Assert.assertTrue(paramI2.getModel() instanceof Value);
Value modelI2 = (Value) paramI2.getModel();
Assert.assertEquals("i2", modelI2.getName());
Assert.assertEquals("Integer", typeName(modelI2));
}
{
// functionalParameterMpl3
Function fp = (Function) fpClass.getDirectMember("functionalParameterMpl3", null, false);
Assert.assertNotNull(fp);
Assert.assertEquals(1, fp.getParameterLists().size());
Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
Parameter paramMpl = fp.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("mpl", paramMpl.getName());
Assert.assertTrue(paramMpl.getModel() instanceof Function);
Function modelMpl = (Function) paramMpl.getModel();
Assert.assertEquals("mpl", modelMpl.getName());
Assert.assertEquals(2, modelMpl.getParameterLists().size());
Assert.assertEquals(1, modelMpl.getParameterLists().get(0).getParameters().size());
Assert.assertEquals(1, modelMpl.getParameterLists().get(1).getParameters().size());
Parameter paramF = modelMpl.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("f", paramF.getName());
Assert.assertTrue(paramF.getModel() instanceof Function);
Function modelF = (Function) paramF.getModel();
Assert.assertEquals("f", modelF.getName());
Assert.assertEquals(1, modelF.getParameterLists().size());
Assert.assertEquals(2, modelF.getParameterLists().get(0).getParameters().size());
Parameter paramB1 = modelF.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("b1", paramB1.getName());
Assert.assertTrue(paramB1.getModel() instanceof Value);
Value modelB1 = (Value) paramB1.getModel();
Assert.assertEquals("b1", modelB1.getName());
Assert.assertEquals("Boolean", typeName(modelB1));
Parameter paramI2 = modelF.getParameterLists().get(0).getParameters().get(1);
Assert.assertEquals("i2", paramI2.getName());
Assert.assertTrue(paramI2.getModel() instanceof Value);
Value modelI2 = (Value) paramI2.getModel();
Assert.assertEquals("i2", modelI2.getName());
Assert.assertEquals("Integer", typeName(modelI2));
Parameter paramS = modelMpl.getParameterLists().get(1).getParameters().get(0);
Assert.assertEquals("s", paramS.getName());
Assert.assertTrue(paramS.getModel() instanceof Value);
Value modelS = (Value) paramS.getModel();
Assert.assertEquals("s", modelS.getName());
Assert.assertEquals("String", typeName(modelS));
}
{
// functionalParameterReturningCallable
Function fp = (Function) fpClass.getDirectMember("functionalParameterReturningCallable", null, false);
Assert.assertNotNull(fp);
Assert.assertEquals(1, fp.getParameterLists().size());
Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("f", paramF.getName());
Assert.assertTrue(paramF.getModel() instanceof Function);
Function modelF = (Function) paramF.getModel();
Assert.assertEquals("f", modelF.getName());
Assert.assertEquals("Anything()", modelF.getType().asString());
Assert.assertEquals(1, modelF.getParameterLists().size());
Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
Parameter paramS = modelF.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("s", paramS.getName());
Assert.assertTrue(paramS.getModel() instanceof Value);
Value modelS = (Value) paramS.getModel();
Assert.assertEquals("s", modelS.getName());
Assert.assertEquals("String", modelS.getType().asString());
}
{
// functionalParameterReturningCallable
Function fp = (Function) fpClass.getDirectMember("functionalParameterTakingCallable", null, false);
Assert.assertNotNull(fp);
Assert.assertEquals(1, fp.getParameterLists().size());
Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("f", paramF.getName());
Assert.assertTrue(paramF.getModel() instanceof Function);
Function modelF = (Function) paramF.getModel();
Assert.assertEquals("f", modelF.getName());
Assert.assertEquals("Anything", modelF.getType().asString());
Assert.assertEquals(1, modelF.getParameterLists().size());
Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
Parameter paramF2 = modelF.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("f2", paramF2.getName());
Assert.assertTrue(paramF2.getModel() instanceof Value);
Value modelF2 = (Value) paramF2.getModel();
Assert.assertEquals("f2", modelF2.getName());
Assert.assertEquals("Anything(String)", modelF2.getType().asString());
}
{
// functionalParameterVariadicStar
Function fp = (Function) fpClass.getDirectMember("functionalParameterVariadicStar", null, false);
Assert.assertNotNull(fp);
Assert.assertEquals(1, fp.getParameterLists().size());
Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
Assert.assertEquals("Anything(Anything(String*))", typeName(fp));
Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
Assert.assertTrue(paramF.isDeclaredAnything());
Assert.assertEquals("f", paramF.getName());
Assert.assertTrue(paramF.getModel() instanceof Function);
Function modelF = (Function) paramF.getModel();
Assert.assertTrue(modelF.isDeclaredVoid());
Assert.assertEquals("Anything(String*)", typeName(modelF));
Assert.assertEquals("f", modelF.getName());
Assert.assertEquals(1, modelF.getParameterLists().size());
Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
Parameter paramS = modelF.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("s", paramS.getName());
Assert.assertTrue(paramS.isSequenced());
Assert.assertFalse(paramS.isAtLeastOne());
Assert.assertTrue(paramS.getModel() instanceof Value);
Value modelS = (Value) paramS.getModel();
Assert.assertEquals("s", modelS.getName());
Assert.assertEquals("String[]", typeName(modelS));
}
{
// functionalParameterVariadicPlus
Function fp = (Function) fpClass.getDirectMember("functionalParameterVariadicPlus", null, false);
Assert.assertNotNull(fp);
Assert.assertEquals(1, fp.getParameterLists().size());
Assert.assertEquals(1, fp.getParameterLists().get(0).getParameters().size());
Assert.assertEquals("Anything(Anything(String+))", typeName(fp));
Parameter paramF = fp.getParameterLists().get(0).getParameters().get(0);
Assert.assertTrue(paramF.isDeclaredAnything());
Assert.assertEquals("f", paramF.getName());
Assert.assertTrue(paramF.getModel() instanceof Function);
Function modelF = (Function) paramF.getModel();
Assert.assertTrue(modelF.isDeclaredVoid());
Assert.assertEquals("Anything(String+)", typeName(modelF));
Assert.assertEquals("f", modelF.getName());
Assert.assertEquals(1, modelF.getParameterLists().size());
Assert.assertEquals(1, modelF.getParameterLists().get(0).getParameters().size());
Parameter paramS = modelF.getParameterLists().get(0).getParameters().get(0);
Assert.assertEquals("s", paramS.getName());
Assert.assertTrue(paramS.isSequenced());
Assert.assertTrue(paramS.isAtLeastOne());
Assert.assertTrue(paramS.getModel() instanceof Value);
Value modelS = (Value) paramS.getModel();
Assert.assertEquals("s", modelS.getName());
Assert.assertEquals("[String+]", typeName(modelS));
}
}
private String typeName(FunctionOrValue fp) {
if (fp instanceof Function) {
return fp.appliedTypedReference(null, Collections.<Type>emptyList()).getFullType().asString();
} else if (fp instanceof Value) {
return fp.getType().asString();
}
return null;
}
});
} catch (RuntimeException e) {
if (e.getCause() instanceof org.junit.ComparisonFailure) {
throw (org.junit.ComparisonFailure) e.getCause();
} else if (e.getCause() instanceof java.lang.AssertionError) {
throw (java.lang.AssertionError) e.getCause();
}
throw e;
}
}
use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.
the class CallableBuilder method makeCallTypedMethod.
/**
* Makes the {@code call$typed()} method, using the given body.
*/
private MethodDefinitionBuilder makeCallTypedMethod(List<JCStatement> body) {
// make the method
MethodDefinitionBuilder methodBuilder = MethodDefinitionBuilder.systemMethod(gen, Naming.getCallableTypedMethodName());
methodBuilder.noAnnotations();
methodBuilder.modifiers(Flags.PRIVATE);
Type returnType = gen.getReturnTypeOfCallable(typeModel);
methodBuilder.resultType(gen.makeJavaType(returnType, JT_NO_PRIMITIVES), null);
// add all parameters
int i = 0;
for (Parameter param : paramLists.getParameters()) {
ParameterDefinitionBuilder parameterBuilder = ParameterDefinitionBuilder.systemParameter(gen, Naming.getAliasedParameterName(param));
JCExpression paramType = gen.makeJavaType(parameterTypes.get(i));
parameterBuilder.modifiers(Flags.FINAL);
parameterBuilder.type(paramType, null);
methodBuilder.parameter(parameterBuilder);
i++;
}
// Return the call result, or null if a void method
methodBuilder.body(body);
return methodBuilder;
}
use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.
the class CallableBuilder method getParameterTypesFromParameterModels.
private java.util.List<Type> getParameterTypesFromParameterModels() {
java.util.List<Type> parameterTypes = new ArrayList<Type>(numParams);
// get them from our declaration
for (Parameter p : paramLists.getParameters()) {
Type pt;
FunctionOrValue pm = p.getModel();
if (pm instanceof Function && ((Function) pm).isParameter())
pt = gen.getTypeForFunctionalParameter((Function) pm);
else
pt = p.getType();
parameterTypes.add(pt);
}
return parameterTypes;
}
use of com.redhat.ceylon.model.typechecker.model.Parameter in project ceylon-compiler by ceylon.
the class CallableBuilder method unboundFunctionalMemberReference.
/**
* Used for "static" method or class references. For example:
* <pre>
* value x = Integer.plus;
* value y = Foo.method;
* value z = Outer.Inner;
* </pre>
*/
public static CallableBuilder unboundFunctionalMemberReference(CeylonTransformer gen, Tree.QualifiedMemberOrTypeExpression qmte, Type typeModel, final Functional methodClassOrCtor, Reference producedReference) {
final ParameterList parameterList = methodClassOrCtor.getFirstParameterList();
Type type = typeModel;
JCExpression target;
boolean hasOuter = !(Decl.isConstructor((Declaration) methodClassOrCtor) && gen.getNumParameterLists(typeModel) == 1);
if (!hasOuter) {
type = typeModel;
target = null;
} else {
type = gen.getReturnTypeOfCallable(type);
Type qualifyingType = qmte.getTarget().getQualifyingType();
target = gen.naming.makeUnquotedIdent(Unfix.$instance$);
target = gen.expressionGen().applyErasureAndBoxing(target, producedReference.getQualifyingType(), true, BoxingStrategy.BOXED, qualifyingType);
}
CallableBuilder inner = new CallableBuilder(gen, null, type, parameterList);
//FromParameterModels();
inner.parameterTypes = inner.getParameterTypesFromCallableModel();
if (hasOuter) {
inner.defaultValueCall = inner.new MemberReferenceDefaultValueCall(methodClassOrCtor);
}
CallBuilder callBuilder = CallBuilder.instance(gen);
Type accessType = gen.getParameterTypeOfCallable(typeModel, 0);
if (Decl.isConstructor((Declaration) methodClassOrCtor)) {
Constructor ctor = Decl.getConstructor((Declaration) methodClassOrCtor);
Class cls = Decl.getConstructedClass(ctor);
if (Strategy.generateInstantiator(ctor)) {
callBuilder.invoke(gen.naming.makeInstantiatorMethodName(target, cls));
} else {
callBuilder.instantiate(gen.makeJavaType(gen.getReturnTypeOfCallable(typeModel), JT_CLASS_NEW));
if (!ctor.isShared()) {
accessType = Decl.getPrivateAccessType(qmte);
}
}
} else if (methodClassOrCtor instanceof Function && ((Function) methodClassOrCtor).isParameter()) {
callBuilder.invoke(gen.naming.makeQualifiedName(target, (Function) methodClassOrCtor, Naming.NA_MEMBER));
} else if (methodClassOrCtor instanceof Function) {
callBuilder.invoke(gen.naming.makeQualifiedName(target, (Function) methodClassOrCtor, Naming.NA_MEMBER));
if (!((TypedDeclaration) methodClassOrCtor).isShared()) {
accessType = Decl.getPrivateAccessType(qmte);
}
} else if (methodClassOrCtor instanceof Class) {
Class cls = (Class) methodClassOrCtor;
if (Strategy.generateInstantiator(cls)) {
callBuilder.invoke(gen.naming.makeInstantiatorMethodName(target, cls));
} else {
callBuilder.instantiate(new ExpressionAndType(target, null), gen.makeJavaType(cls.getType(), JT_CLASS_NEW | AbstractTransformer.JT_NON_QUALIFIED));
if (!cls.isShared()) {
accessType = Decl.getPrivateAccessType(qmte);
}
}
} else {
throw BugException.unhandledDeclarationCase((Declaration) methodClassOrCtor, qmte);
}
ListBuffer<ExpressionAndType> reified = ListBuffer.lb();
DirectInvocation.addReifiedArguments(gen, producedReference, reified);
for (ExpressionAndType reifiedArgument : reified) {
callBuilder.argument(reifiedArgument.expression);
}
if (Decl.isConstructor((Declaration) methodClassOrCtor) && !Decl.isDefaultConstructor(Decl.getConstructor((Declaration) methodClassOrCtor))) {
// invoke the param class ctor
Constructor ctor = Decl.getConstructor((Declaration) methodClassOrCtor);
callBuilder.argument(gen.naming.makeNamedConstructorName(ctor, false));
}
for (Parameter parameter : parameterList.getParameters()) {
callBuilder.argument(gen.naming.makeQuotedIdent(Naming.getAliasedParameterName(parameter)));
}
JCExpression innerInvocation = callBuilder.build();
// Need to worry about boxing for Function and FunctionalParameter
if (methodClassOrCtor instanceof TypedDeclaration && !Decl.isConstructor((Declaration) methodClassOrCtor)) {
// use the method return type since the function is actually applied
Type returnType = gen.getReturnTypeOfCallable(type);
innerInvocation = gen.expressionGen().applyErasureAndBoxing(innerInvocation, returnType, // expression is a Callable
CodegenUtil.hasTypeErased((TypedDeclaration) methodClassOrCtor), !CodegenUtil.isUnBoxed((TypedDeclaration) methodClassOrCtor), BoxingStrategy.BOXED, returnType, 0);
} else if (methodClassOrCtor instanceof Class && Strategy.isInstantiatorUntyped((Class) methodClassOrCtor)) {
// $new method declared to return Object, so needs typecast
innerInvocation = gen.make().TypeCast(gen.makeJavaType(((Class) methodClassOrCtor).getType()), innerInvocation);
}
List<JCStatement> innerBody = List.<JCStatement>of(gen.make().Return(innerInvocation));
inner.useDefaultTransformation(innerBody);
if (!hasOuter) {
return inner;
}
ParameterList outerPl = new ParameterList();
Parameter instanceParameter = new Parameter();
instanceParameter.setName(Naming.name(Unfix.$instance$));
Value valueModel = new Value();
instanceParameter.setModel(valueModel);
valueModel.setName(instanceParameter.getName());
valueModel.setInitializerParameter(instanceParameter);
valueModel.setType(accessType);
valueModel.setUnboxed(false);
outerPl.getParameters().add(instanceParameter);
CallableBuilder outer = new CallableBuilder(gen, null, typeModel, outerPl);
outer.parameterTypes = outer.getParameterTypesFromParameterModels();
List<JCStatement> outerBody = List.<JCStatement>of(gen.make().Return(inner.build()));
outer.useDefaultTransformation(outerBody);
outer.companionAccess = Decl.isPrivateAccessRequiringCompanion(qmte);
return outer;
}
Aggregations