Search in sources :

Example 16 with Function

use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.

the class InstanceImporter method importService.

@Override
public T importService(final ImportedServiceDescriptor serviceDescriptor) throws ServiceImporterException {
    T instance = null;
    Iterable<MetaInfoHolder> holders = Iterables.iterable(serviceDescriptor, module, layer, application);
    for (final MetaInfoHolder metaInfoHolder : holders) {
        Function<Class<?>, T> metaFinder = new Function<Class<?>, T>() {

            @Override
            public T map(Class<?> type) {
                return (T) metaInfoHolder.metaInfo(type);
            }
        };
        instance = first(filter(notNull(), map(metaFinder, serviceDescriptor.types())));
        if (instance != null) {
            break;
        }
    }
    return instance;
}
Also used : MetaInfoHolder(org.qi4j.api.structure.MetaInfoHolder) Function(org.qi4j.functional.Function)

Example 17 with Function

use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.

the class CompositeAssemblyImpl method constraintDeclarations.

private Iterable<Class<? extends Constraint<?, ?>>> constraintDeclarations(Iterable<? extends Class<?>> typess) {
    // Find constraint declarations
    List<Type> allTypes = new ArrayList<Type>();
    for (Class<?> type : typess) {
        Iterable<Type> types = typesOf(type);
        Iterables.addAll(allTypes, types);
    }
    // Find all constraints and flatten them into an iterable
    Function<Type, Iterable<Class<? extends Constraint<?, ?>>>> function = new Function<Type, Iterable<Class<? extends Constraint<?, ?>>>>() {

        @Override
        public Iterable<Class<? extends Constraint<?, ?>>> map(Type type) {
            Constraints constraints = Annotations.annotationOn(type, Constraints.class);
            if (constraints == null) {
                return Iterables.empty();
            } else {
                return iterable(constraints.value());
            }
        }
    };
    Iterable<Class<? extends Constraint<?, ?>>> flatten = Iterables.flattenIterables(Iterables.map(function, allTypes));
    return Iterables.toList(flatten);
}
Also used : Function(org.qi4j.functional.Function) Type(java.lang.reflect.Type) Constraints(org.qi4j.api.constraint.Constraints) Constraint(org.qi4j.api.constraint.Constraint) ArrayList(java.util.ArrayList)

Example 18 with Function

use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.

the class CompositeAssemblyImpl method concernDeclarations.

private Iterable<Class<?>> concernDeclarations(Iterable<? extends Class<?>> typess) {
    // Find concern declarations
    ArrayList<Type> allTypes = new ArrayList<Type>();
    for (Class<?> type : typess) {
        Iterable<Type> types;
        if (type.isInterface()) {
            types = typesOf(type);
        } else {
            types = Iterables.<Type>cast(classHierarchy(type));
        }
        Iterables.addAll(allTypes, types);
    }
    // Find all concerns and flattern them into an iterable
    Function<Type, Iterable<Class<?>>> function = new Function<Type, Iterable<Class<?>>>() {

        @Override
        public Iterable<Class<?>> map(Type type) {
            Concerns concerns = Annotations.annotationOn(type, Concerns.class);
            if (concerns == null) {
                return Iterables.empty();
            } else {
                return iterable(concerns.value());
            }
        }
    };
    Iterable<Class<?>> flatten = Iterables.flattenIterables(Iterables.map(function, allTypes));
    return Iterables.toList(flatten);
}
Also used : Function(org.qi4j.functional.Function) Type(java.lang.reflect.Type) ArrayList(java.util.ArrayList) Concerns(org.qi4j.api.concern.Concerns)

Example 19 with Function

use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.

the class CompositeAssemblyImpl method mixinDeclarations.

protected Iterable<Class<?>> mixinDeclarations(Iterable<? extends Class<?>> typess) {
    // Find mixin declarations
    ArrayList<Type> allTypes = new ArrayList<Type>();
    for (Class<?> type : typess) {
        Iterable<Type> types = typesOf(type);
        Iterables.addAll(allTypes, types);
    }
    // Find all mixins and flattern them into an iterable
    Function<Type, Iterable<Class<?>>> function = new Function<Type, Iterable<Class<?>>>() {

        @Override
        public Iterable<Class<?>> map(Type type) {
            Mixins mixins = Annotations.annotationOn(type, Mixins.class);
            if (mixins == null) {
                return Iterables.empty();
            } else {
                return iterable(mixins.value());
            }
        }
    };
    Iterable<Class<?>> flatten = Iterables.flattenIterables(Iterables.map(function, allTypes));
    return Iterables.toList(flatten);
}
Also used : Function(org.qi4j.functional.Function) Type(java.lang.reflect.Type) Mixins(org.qi4j.api.mixin.Mixins) ArrayList(java.util.ArrayList)

Example 20 with Function

use of org.qi4j.functional.Function in project qi4j-sdk by Qi4j.

the class ClassScanner method findClasses.

/**
 * Get all classes from the same package of the given class, and recursively in all subpackages.
 * <p/>
 * This only works if the seed class is loaded from a file: URL. Jar files are possible as well. Abstract classes
 * are not included in the results. For further filtering use e.g. Iterables.filter.
 *
 * @param seedClass starting point for classpath scanning
 *
 * @return iterable of all concrete classes in the same package as the seedclass, and also all classes in subpackages.
 */
public static Iterable<Class<?>> findClasses(final Class<?> seedClass) {
    CodeSource codeSource = seedClass.getProtectionDomain().getCodeSource();
    if (codeSource == null) {
        return Iterables.empty();
    }
    URL location = codeSource.getLocation();
    if (!location.getProtocol().equals("file")) {
        throw new IllegalArgumentException("Can only enumerate classes from file system locations. URL is:" + location);
    }
    final File file;
    try {
        file = new File(location.toURI().getPath());
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException("The file location of codebase is invalid. Can not convert to URI. URL is:" + location);
    }
    if (file.getName().endsWith(".jar")) {
        try {
            final String packageName = seedClass.getPackage().getName().replace('.', '/');
            JarFile jarFile = new JarFile(file);
            Iterable<JarEntry> entries = Iterables.iterable(jarFile.entries());
            try {
                return Iterables.toList(filter(new ValidClass(), map(new Function<JarEntry, Class<?>>() {

                    @Override
                    public Class map(JarEntry jarEntry) {
                        String name = jarEntry.getName();
                        name = name.substring(0, name.length() - 6);
                        name = name.replace('/', '.');
                        try {
                            return seedClass.getClassLoader().loadClass(name);
                        } catch (ClassNotFoundException e) {
                            return null;
                        }
                    }
                }, filter(new Specification<JarEntry>() {

                    @Override
                    public boolean satisfiedBy(JarEntry jarEntry) {
                        return jarEntry.getName().startsWith(packageName) && jarEntry.getName().endsWith(".class");
                    }
                }, entries))));
            } finally {
                jarFile.close();
            }
        } catch (IOException e) {
            throw new IllegalArgumentException("Could not open jar file " + file, e);
        }
    } else {
        final File path = new File(file, seedClass.getPackage().getName().replace('.', File.separatorChar));
        Iterable<File> files = findFiles(path, new Specification<File>() {

            @Override
            public boolean satisfiedBy(File file) {
                return file.getName().endsWith(".class");
            }
        });
        return filter(new ValidClass(), map(new Function<File, Class<?>>() {

            @Override
            public Class<?> map(File f) {
                String fileName = f.getAbsolutePath().substring(file.toString().length() + 1);
                fileName = fileName.replace(File.separatorChar, '.').substring(0, fileName.length() - 6);
                try {
                    return seedClass.getClassLoader().loadClass(fileName);
                } catch (ClassNotFoundException e) {
                    return null;
                }
            }
        }, files));
    }
}
Also used : Specification(org.qi4j.functional.Specification) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) CodeSource(java.security.CodeSource) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) Function(org.qi4j.functional.Function) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

Function (org.qi4j.functional.Function)25 Type (java.lang.reflect.Type)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 Application (org.qi4j.api.structure.Application)7 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)6 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)5 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)4 GraphDAO (org.qi4j.sample.dcicargo.pathfinder.internal.GraphDAO)4 GraphTraversalServiceImpl (org.qi4j.sample.dcicargo.pathfinder.internal.GraphTraversalServiceImpl)4 OrgJsonValueSerializationService (org.qi4j.valueserialization.orgjson.OrgJsonValueSerializationService)4 EntityReference (org.qi4j.api.entity.EntityReference)3 Annotations.isType (org.qi4j.api.util.Annotations.isType)3 Classes.wrapperClass (org.qi4j.api.util.Classes.wrapperClass)3 MemoryEntityStoreService (org.qi4j.entitystore.memory.MemoryEntityStoreService)3 UuidIdentityGeneratorService (org.qi4j.spi.uuid.UuidIdentityGeneratorService)3 Principal (java.security.Principal)2 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)2 Concerns (org.qi4j.api.concern.Concerns)2 Constraint (org.qi4j.api.constraint.Constraint)2