Search in sources :

Example 16 with TypeInfo

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

the class ClassOrInterfaceDeclarationImpl method memberApply.

@SuppressWarnings("rawtypes")
@TypeInfo("ceylon.language.meta.model::Member<Container,ceylon.language.meta.model::ClassOrInterface<Type>>&ceylon.language.meta.model::ClassOrInterface<Type>")
@TypeParameters({ @TypeParameter("Container"), @TypeParameter("Type") })
@Override
public <Container, Type extends Object> java.lang.Object memberApply(@Ignore TypeDescriptor $reifiedContainer, @Ignore TypeDescriptor $reifiedType, @Name("containerType") ceylon.language.meta.model.Type<? extends Object> containerType, @Name("typeArguments") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments) {
    if (getToplevel())
        throw new ceylon.language.meta.model.TypeApplicationException("Cannot apply a toplevel declaration to a container type: use apply");
    ceylon.language.meta.model.Member<? extends Container, ceylon.language.meta.model.ClassOrInterface<?>> member = getAppliedClassOrInterface(null, null, typeArguments, containerType);
    // This is all very ugly but we're trying to make it cheaper and friendlier than just checking the full type and showing
    // implementation types to the user, such as AppliedMemberClass
    TypeDescriptor actualReifiedContainer;
    if (member instanceof MemberClassImpl)
        actualReifiedContainer = ((MemberClassImpl) member).$reifiedContainer;
    else
        actualReifiedContainer = ((MemberInterfaceImpl) member).$reifiedContainer;
    org.eclipse.ceylon.model.typechecker.model.Type actualType = Metamodel.getModel((ceylon.language.meta.model.Type<?>) member);
    Metamodel.checkReifiedTypeArgument("memberApply", "Member<$1,ClassOrInterface<$2>>&ClassOrInterface<$2>", Variance.IN, Metamodel.getProducedType(actualReifiedContainer), $reifiedContainer, Variance.OUT, actualType, $reifiedType);
    return member;
}
Also used : ClassOrInterface(ceylon.language.meta.model.ClassOrInterface) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) MemberInterfaceImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberInterfaceImpl) Type(org.eclipse.ceylon.model.typechecker.model.Type) Metamodel(org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel) MemberClassImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl) TypeParameters(org.eclipse.ceylon.compiler.java.metadata.TypeParameters) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 17 with TypeInfo

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

the class FunctionDeclarationImpl method apply.

@Override
@TypeInfo("ceylon.language.meta.model::Function<Return,Arguments>")
@TypeParameters({ @TypeParameter("Return"), @TypeParameter(value = "Arguments", satisfies = "ceylon.language::Sequential<ceylon.language::Anything>") })
public <Return extends Object, Arguments extends Sequential<? extends Object>> ceylon.language.meta.model.Function<Return, Arguments> apply(@Ignore TypeDescriptor $reifiedReturn, @Ignore TypeDescriptor $reifiedArguments, @Name("typeArguments") @TypeInfo("ceylon.language::Sequential<ceylon.language.meta.model::Type<ceylon.language::Anything>>") @Sequenced Sequential<? extends ceylon.language.meta.model.Type<?>> typeArguments) {
    if (!getToplevel()) {
        String msg;
        if (getStatic()) {
            msg = "Cannot apply a static declaration with no container type: use staticApply";
        } else {
            msg = "Cannot apply a member declaration with no container type: use memberApply";
        }
        throw new ceylon.language.meta.model.TypeApplicationException(msg);
    }
    List<org.eclipse.ceylon.model.typechecker.model.Type> producedTypes = Metamodel.getProducedTypes(typeArguments);
    Metamodel.checkTypeArguments(null, declaration, producedTypes);
    org.eclipse.ceylon.model.typechecker.model.Reference appliedFunction = declaration.appliedReference(null, producedTypes);
    TypeDescriptor reifiedType = Metamodel.getTypeDescriptorForFunction(appliedFunction);
    TypeDescriptor reifiedArguments = Metamodel.getTypeDescriptorForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction);
    Metamodel.checkReifiedTypeArgument("apply", "Function<$1,$2>", Variance.OUT, declaration.getUnit().getCallableReturnType(appliedFunction.getFullType()), $reifiedReturn, Variance.IN, Metamodel.getProducedTypeForArguments(declaration.getUnit(), (Functional) declaration, appliedFunction), $reifiedArguments);
    return new FunctionImpl<Return, Arguments>(reifiedType, reifiedArguments, appliedFunction, this, null, null);
}
Also used : Functional(org.eclipse.ceylon.model.typechecker.model.Functional) OpenType(ceylon.language.meta.declaration.OpenType) TypeDescriptor(org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor) FunctionImpl(org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.FunctionImpl) 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)

Example 18 with TypeInfo

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

the class ModuleImpl method resourceByPath.

@Override
@TypeInfo("ceylon.language::Resource")
public Resource resourceByPath(@Name("path") String path) {
    String fullPath = path;
    if (!fullPath.startsWith("/")) {
        String modPath = ("default".equals(getName())) ? "" : getName().replace('.', '/') + "/";
        fullPath = modPath + path;
    } else {
        fullPath = fullPath.substring(1);
    }
    fullPath = JVMModuleUtil.quoteJavaKeywordsInFilename(fullPath);
    // First lets ask the module manager for the contents of the resource
    RuntimeModuleManager moduleManager = Metamodel.getModuleManager();
    if (moduleManager != null) {
        RuntimeModelLoader modelLoader = moduleManager.getModelLoader();
        if (modelLoader != null) {
            byte[] contents = modelLoader.getContents(declaration, fullPath);
            if (contents != null) {
                URI uri = modelLoader.getContentUri(declaration, fullPath);
                return new ByteArrayResource(contents, uri);
            }
        }
    }
    // Second let's see if we can find the on-disk location of the module
    String moduleUnitFullPath = declaration.getUnit().getFullPath();
    if (moduleUnitFullPath != null) {
        final File car = new File(moduleUnitFullPath);
        // Then let's look inside the car
        try (ZipFile zip = new ZipFile(car)) {
            ZipEntry e = zip.getEntry(fullPath);
            if (e != null && !e.isDirectory()) {
                return new ZipResource(car, fullPath);
            }
        } catch (IOException ex) {
            throw new ceylon.language.Exception(new ceylon.language.String("Searching for resource " + path), ex);
        }
        // And finally as a fall-back let's look in the module's resource dir...
        final File target = new File(new File(car.getParentFile(), "module-resources"), fullPath);
        if (target.exists() && target.isFile() && target.canRead()) {
            return new FileResource(target);
        }
    }
    // One last shot: we might be in a fat jar
    try (InputStream stream = getClass().getClassLoader().getResourceAsStream(fullPath)) {
        if (stream != null) {
            byte[] buf = new byte[16384];
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            int bytesRead = stream.read(buf);
            while (bytesRead > 0) {
                bout.write(buf, 0, bytesRead);
                bytesRead = stream.read(buf);
            }
            return new ByteArrayResource(bout.toByteArray(), new URI("classpath:" + fullPath));
        }
    } catch (IOException | URISyntaxException ex) {
        throw new ceylon.language.Exception(new ceylon.language.String("Searching for resource " + path), ex);
    }
    return null;
}
Also used : RuntimeModuleManager(org.eclipse.ceylon.compiler.java.runtime.model.RuntimeModuleManager) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) FileResource(org.eclipse.ceylon.compiler.java.language.FileResource) ByteArrayResource(org.eclipse.ceylon.compiler.java.language.ByteArrayResource) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ZipResource(org.eclipse.ceylon.compiler.java.language.ZipResource) ZipFile(java.util.zip.ZipFile) RuntimeModelLoader(org.eclipse.ceylon.compiler.java.runtime.model.RuntimeModelLoader) ZipFile(java.util.zip.ZipFile) File(java.io.File) TypeInfo(org.eclipse.ceylon.compiler.java.metadata.TypeInfo)

Example 19 with TypeInfo

use of org.eclipse.ceylon.compiler.java.metadata.TypeInfo 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 20 with TypeInfo

use of org.eclipse.ceylon.compiler.java.metadata.TypeInfo 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)

Aggregations

TypeInfo (org.eclipse.ceylon.compiler.java.metadata.TypeInfo)38 TypeParameters (org.eclipse.ceylon.compiler.java.metadata.TypeParameters)32 Metamodel (org.eclipse.ceylon.compiler.java.runtime.metamodel.Metamodel)25 TypeDescriptor (org.eclipse.ceylon.compiler.java.runtime.model.TypeDescriptor)18 Sequential (ceylon.language.Sequential)12 ArrayList (java.util.ArrayList)10 ObjectArrayIterable (org.eclipse.ceylon.compiler.java.language.ObjectArrayIterable)10 ReifiedType (org.eclipse.ceylon.compiler.java.runtime.model.ReifiedType)9 ClassOrInterfaceDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassOrInterfaceDeclarationImpl)8 IncompatibleTypeException (ceylon.language.meta.model.IncompatibleTypeException)7 ClassDeclarationImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.decl.ClassDeclarationImpl)7 ClassDeclaration (ceylon.language.meta.declaration.ClassDeclaration)5 Type (org.eclipse.ceylon.model.typechecker.model.Type)5 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 MemberClassImpl (org.eclipse.ceylon.compiler.java.runtime.metamodel.meta.MemberClassImpl)4 AssertionError (ceylon.language.AssertionError)3 CallableConstructorDeclaration (ceylon.language.meta.declaration.CallableConstructorDeclaration)3 ClassOrInterfaceDeclaration (ceylon.language.meta.declaration.ClassOrInterfaceDeclaration)3