Search in sources :

Example 16 with TypeParameters

use of org.eclipse.ceylon.compiler.java.metadata.TypeParameters in project ceylon by eclipse.

the class Integer method min.

@TypeParameters(@TypeParameter(value = "Absent", satisfies = "ceylon.language::Null"))
@TypeInfo("ceylon.language::Integer|Absent")
public static <Absent> java.lang.Object min(@Ignore TypeDescriptor $reifiedAbsent, @TypeInfo("ceylon.language::Iterable<ceylon.language::Integer,Absent>") @Name("integers") Iterable<? extends Integer, ? extends java.lang.Object> integers) {
    if (integers instanceof Array) {
        java.lang.Object array = ((Array<?>) integers).toArray();
        if (array instanceof int[]) {
            int[] intArray = (int[]) array;
            int s = intArray.length;
            if (s == 0) {
                return null;
            }
            int min = intArray[0];
            for (int i = 1; i < s; i++) {
                min = Math.min(min, intArray[i]);
            }
            return instance(min);
        }
        if (array instanceof long[]) {
            long[] longArray = (long[]) array;
            int s = longArray.length;
            if (s == 0) {
                return null;
            }
            long min = longArray[0];
            for (int i = 1; i < s; i++) {
                min = Math.min(min, longArray[i]);
            }
            return instance(min);
        }
    }
    if (integers instanceof List) {
        @SuppressWarnings("unchecked") List<? extends Integer> list = (List<? extends Integer>) integers;
        long size = list.getSize();
        if (size == 0) {
            return null;
        }
        long min = integers.getFromFirst(0).value;
        for (int i = 1; i < size; i++) {
            min = Math.min(min, list.getFromFirst(i).value);
        }
        return instance(min);
    }
    long min = 0l;
    boolean first = true;
    Iterator<? extends Integer> it = integers.iterator();
    java.lang.Object o;
    while ((o = it.next()) != finished_.get_()) {
        long x = ((Integer) o).value;
        if (first) {
            min = x;
            first = false;
        } else {
            min = Math.min(min, x);
        }
    }
    return first ? null : instance(min);
}
Also used : Character.codepoint(ceylon.language.Character.codepoint) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 17 with TypeParameters

use of org.eclipse.ceylon.compiler.java.metadata.TypeParameters in project ceylon by eclipse.

the class ValueTypeTests method findStaticCompanionMethod.

// Tries to find the companion method for the given class method
// by looking for a static method with the same parameters plus
// an extra first parameter. Also the static version will always
// have the "unboxed" version of any Value Type that appears as
// either parameter or return type
private Method findStaticCompanionMethod(Class<?> clazz, Method classMethod, Method typedMethod) {
    Class<?>[] instancePTs = classMethod.getParameterTypes();
    Annotation[][] instanceAnnos = typedMethod.getParameterAnnotations();
    TypeParameters tps = typedMethod.getAnnotation(TypeParameters.class);
    int tpsCount = (tps != null) ? tps.value().length : 0;
    Class<?>[] staticPTs = new Class<?>[instancePTs.length + 1];
    staticPTs[tpsCount] = getUnboxedType(clazz, clazz.getAnnotation(TypeInfo.class));
    for (int i = 0; i < instancePTs.length; i++) {
        TypeInfo typeInfo = findAnnotation(instanceAnnos[i], TypeInfo.class);
        int idx = (i < tpsCount) ? i : i + 1;
        staticPTs[idx] = getUnboxedType(instancePTs[i], typeInfo);
    }
    try {
        return clazz.getMethod(classMethod.getName(), staticPTs);
    } catch (NoSuchMethodException | SecurityException e) {
        return null;
    }
}
Also used : TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 18 with TypeParameters

use of org.eclipse.ceylon.compiler.java.metadata.TypeParameters in project ceylon by eclipse.

the class flatten_ method flatten.

@TypeParameters({ @TypeParameter(value = "Return"), @TypeParameter(value = "Args", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>") })
@TypeInfo("ceylon.language::Callable<Return,Args>")
public static <Return, Args> Callable<Return> flatten(@Ignore final TypeDescriptor $reifiedReturn, @Ignore final TypeDescriptor $reifiedArgs, @Name("tupleFunction") @FunctionalParameter("(tuple)") @TypeInfo("ceylon.language::Callable<Return,ceylon.language::Tuple<Args,Args,ceylon.language::Empty>>") final Callable<? extends Return> tupleFunction) {
    return new AbstractCallable<Return>($reifiedReturn, $reifiedArgs, null, (short) -1) {

        private TypeDescriptor getElementType(java.lang.Object[] args, Sequential<?> tail) {
            if ($reifiedArgs instanceof TypeDescriptor.Class) {
                // optimization for common cases
                TypeDescriptor.Class reifiedClass = (TypeDescriptor.Class) $reifiedArgs;
                TypeDescriptor sequenceElement = reifiedClass.getSequenceElement();
                if (sequenceElement != null)
                    return sequenceElement;
            }
            TypeDescriptor[] types = new TypeDescriptor[args.length + (tail != null ? 1 : 0)];
            for (int i = 0; i < args.length; i++) {
                types[i] = getArgType(args[i]);
            }
            if (tail != null) {
                TypeDescriptor restType = getTypeDescriptor(tail);
                TypeDescriptor elementType = Metamodel.getIteratedTypeDescriptor(restType);
                types[args.length] = elementType;
            }
            return TypeDescriptor.union(types);
        }

        private TypeDescriptor getArgType(java.lang.Object arg) {
            return getTypeDescriptor(arg);
        }

        @SuppressWarnings("rawtypes")
        private Tuple tuple(java.lang.Object[] args) {
            return new Tuple(getElementType(args, null), args);
        }

        @SuppressWarnings("rawtypes")
        private Tuple tuple(java.lang.Object[] args, Sequential<?> tail) {
            return Tuple.instance(getElementType(args, tail), args, tail);
        }

        @Override
        public Return $call$() {
            return tupleFunction.$call$(empty_.get_());
        }

        @Override
        public Return $call$(java.lang.Object arg0) {
            return tupleFunction.$call$(tuple(new java.lang.Object[] { arg0 }));
        }

        @Override
        public Return $call$(java.lang.Object arg0, java.lang.Object arg1) {
            return tupleFunction.$call$(tuple(new java.lang.Object[] { arg0, arg1 }));
        }

        @Override
        public Return $call$(java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2) {
            return tupleFunction.$call$(tuple(new java.lang.Object[] { arg0, arg1, arg2 }));
        }

        @Override
        public Return $call$(java.lang.Object... args) {
            return tupleFunction.$call$(tuple(args));
        }

        @Override
        public Return $callvariadic$(Sequential<?> arg0) {
            return tupleFunction.$call$(arg0);
        }

        @Override
        public Return $callvariadic$(java.lang.Object arg0, Sequential<?> arg1) {
            return tupleFunction.$call$(tuple(new java.lang.Object[] { arg0 }, arg1));
        }

        @Override
        public Return $callvariadic$(java.lang.Object arg0, java.lang.Object arg1, Sequential<?> arg2) {
            return tupleFunction.$call$(tuple(new java.lang.Object[] { arg0, arg1 }, arg2));
        }

        @Override
        public Return $callvariadic$(java.lang.Object... args) {
            // it is an array of the first args.length-1 params followed by a Sequential last
            java.lang.Object[] first = new java.lang.Object[args.length - 1];
            System.arraycopy(args, 0, first, 0, args.length - 1);
            return tupleFunction.$call$(tuple(first, (Sequential<?>) args[args.length - 1]));
        }

        @Override
        public java.lang.String toString() {
            return $getType$().toString();
        }
    };
}
Also used : AbstractCallable(org.eclipse.ceylon.compiler.java.language.AbstractCallable) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Metamodel.getTypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel.getTypeDescriptor) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 19 with TypeParameters

use of org.eclipse.ceylon.compiler.java.metadata.TypeParameters in project ceylon by eclipse.

the class Integer method max.

@TypeParameters(@TypeParameter(value = "Absent", satisfies = "ceylon.language::Null"))
@TypeInfo("ceylon.language::Integer|Absent")
public static <Absent> java.lang.Object max(@Ignore TypeDescriptor $reifiedAbsent, @TypeInfo("ceylon.language::Iterable<ceylon.language::Integer,Absent>") @Name("integers") Iterable<? extends Integer, ? extends java.lang.Object> integers) {
    if (integers instanceof Array) {
        java.lang.Object array = ((Array<?>) integers).toArray();
        if (array instanceof int[]) {
            int[] intArray = (int[]) array;
            int s = intArray.length;
            if (s == 0) {
                return null;
            }
            int max = intArray[0];
            for (int i = 1; i < s; i++) {
                max = Math.max(max, intArray[i]);
            }
            return instance(max);
        }
        if (array instanceof long[]) {
            long[] longArray = (long[]) array;
            int s = longArray.length;
            if (s == 0) {
                return null;
            }
            long max = longArray[0];
            for (int i = 1; i < s; i++) {
                max = Math.max(max, longArray[i]);
            }
            return instance(max);
        }
    }
    if (integers instanceof List) {
        @SuppressWarnings("unchecked") List<? extends Integer> list = (List<? extends Integer>) integers;
        long size = list.getSize();
        if (size == 0) {
            return null;
        }
        long max = integers.getFromFirst(0).value;
        for (int i = 1; i < size; i++) {
            max = Math.max(max, list.getFromFirst(i).value);
        }
        return instance(max);
    }
    long max = 0l;
    boolean first = true;
    Iterator<? extends Integer> it = integers.iterator();
    java.lang.Object o;
    while ((o = it.next()) != finished_.get_()) {
        long x = ((Integer) o).value;
        if (first) {
            max = x;
            first = false;
        } else {
            max = Math.max(max, x);
        }
    }
    return first ? null : instance(max);
}
Also used : Character.codepoint(ceylon.language.Character.codepoint) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 20 with TypeParameters

use of org.eclipse.ceylon.compiler.java.metadata.TypeParameters in project ceylon by eclipse.

the class ValueConstructorDeclarationImpl method apply.

@Override
@TypeInfo("ceylon.language.meta.model::Value<Get>")
@TypeParameters({ @TypeParameter("Get") })
public <Get> ceylon.language.meta.model.ValueConstructor<Get> apply(@Ignore TypeDescriptor $reifiedGet) {
    // TODO if(!getToplevel())
    // throw new ceylon.language.meta.model.TypeApplicationException("Cannot apply a member declaration with no container type: use memberApply");
    // TODO what is Set is anything other than Nothing?
    // c.f. classApply with an incorect parameters
    org.eclipse.ceylon.model.typechecker.model.Value modelDecl = (org.eclipse.ceylon.model.typechecker.model.Value) declaration;
    org.eclipse.ceylon.model.typechecker.model.TypedReference typedReference = modelDecl.appliedTypedReference(null, Collections.<Type>emptyList());
    org.eclipse.ceylon.model.typechecker.model.Type getType = typedReference.getType();
    TypeDescriptor reifiedGet = Metamodel.getTypeDescriptorForProducedType(getType.getQualifyingType());
    // immutable values have Set=Nothing
    // org.eclipse.ceylon.model.typechecker.model.Type setType = getVariable() ?
    // getType : modelDecl.getUnit().getNothingType();
    // TypeDescriptor reifiedSet = getVariable() ? reifiedGet : TypeDescriptor.NothingType;
    Metamodel.checkReifiedTypeArgument("apply", "Value<$1>", Variance.OUT, getType, $reifiedGet);
    // XXX This is a lie, and we only get away with it due to erasure
    ClassDeclaration clsDecl = getContainer();
    ceylon.language.meta.model.Class<? extends Get, ?> cls = clsDecl.<Get, Sequential<? extends java.lang.Object>>classApply($reifiedGet, Nothing.NothingType, (Sequential) empty_.get_());
    return (ceylon.language.meta.model.ValueConstructor<Get>) new ValueConstructorImpl<Get>(reifiedGet, this, typedReference, (ClassImpl) cls, null);
}
Also used : MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) ClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.ClassImpl) Type(org.eclipse.ceylon.model.typechecker.model.Type) Value(org.eclipse.ceylon.model.typechecker.model.Value) ClassDeclaration(ceylon.language.meta.declaration.ClassDeclaration) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) Sequential(ceylon.language.Sequential) Value(org.eclipse.ceylon.model.typechecker.model.Value) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Aggregations

TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)32 TypeParameters (org.eclipse.ceylon.compiler.java.metadata.TypeParameters)32 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)23 TypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)17 Sequential (ceylon.language.Sequential)10 ArrayList (java.util.ArrayList)8 ObjectArrayIterable (org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable)8 ClassOrInterfaceDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl)8 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)8 IncompatibleTypeException (ceylon.language.meta.model.IncompatibleTypeException)7 ClassDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl)6 Type (org.eclipse.ceylon.model.typechecker.model.Type)5 ClassDeclaration (ceylon.language.meta.declaration.ClassDeclaration)4 OpenType (ceylon.language.meta.declaration.OpenType)4 FunctionDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.FunctionDeclarationImpl)4 InterfaceDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.InterfaceDeclarationImpl)4 ValueDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ValueDeclarationImpl)4 MemberClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl)4 Value (org.eclipse.ceylon.model.typechecker.model.Value)3 AssertionError (ceylon.language.AssertionError)2