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;
}
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);
}
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);
}
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);
}
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));
}
}
Aggregations