@Test public void test() { List<Foo> foos = C.newList(); foos.add(new Bar()); foos.add(new Zee()); C.List<Foo> l = C.list(foos); yes(l.size() == 2); yes(l.is(C.Feature.READONLY)); }
/** The base implementation of {@link NetworkService} */ public abstract class NetworkServiceBase implements NetworkService { private volatile boolean started; private Map<Integer, NetworkClient> registry = C.newMap(); public synchronized void register(int port, NetworkClient client) { E.NPE(client); E.illegalArgumentIf(registry.containsKey(port), "Port %s has been registered already", port); registry.put(port, client); if (started) { clientRegistered(client, port); } } @Override public void start() { bootUp(); for (int port : registry.keySet()) { NetworkClient client = registry.get(port); clientRegistered(client, port); } } @Override public void shutdown() { close(); registry.clear(); } protected abstract void clientRegistered(NetworkClient client, int port); protected abstract void bootUp(); protected abstract void close(); }
@ActComponent public class StringValueResolverManager extends AppServiceBase<StringValueResolverManager> { private Map<Class, StringValueResolver> resolvers = C.newMap(); public StringValueResolverManager(App app) { super(app); registerPredefinedResolvers(); registerBuiltInResolvers(app.config()); } @Override protected void releaseResources() { resolvers.clear(); } public <T> StringValueResolverManager register( Class<T> targetType, StringValueResolver<T> resolver) { resolvers.put(targetType, resolver); return this; } public Object resolve(String strVal, Class<?> targetType) { StringValueResolver r = resolvers.get(targetType); if (null != r) { return r.resolve(strVal); } if (null != strVal && Enum.class.isAssignableFrom(targetType)) { return Enum.valueOf(((Class<Enum>) targetType), strVal); } return null; } private void registerPredefinedResolvers() { resolvers.putAll(StringValueResolver.predefined()); } private void registerBuiltInResolvers(AppConfig config) { resolvers.put(Date.class, new DateResolver(config)); resolvers.put(DateTime.class, new JodaDateResolver(config)); } }
/** * Base class for Act class loaders */ public abstract class BootstrapClassLoader extends ClassLoader implements PluginClassProvider, ActClassLoader { protected static final Logger logger = L.get(BootstrapClassLoader.class); private BytecodeEnhancerManager enhancerManager = new BytecodeEnhancerManager(); private ClassInfoRepository classInfoRepository = new ClassInfoRepository(); protected BootstrapClassLoader(ClassLoader parent) { super(parent); } protected BootstrapClassLoader() { super(_getParent()); } @Override public ClassInfoRepository classInfoRepository() { return classInfoRepository; } public Class<?> loadedClass(String name) { Class<?> c = findLoadedClass(name); if (null == c) { ClassLoader p = getParent(); if (null != p && p instanceof ActClassLoader) { return ((ActClassLoader)p).loadedClass(name); } } return c; } private static ClassLoader _getParent() { ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (null == cl) { cl = ClassLoader.getSystemClassLoader(); } return cl; } protected Class<?> defineClassX(String name, byte[] b, int off, int len, ProtectionDomain protectionDomain) { int i = name.lastIndexOf('.'); if (i != -1) { String pkgName = name.substring(0, i); // Check if package already loaded. if (getPackage(pkgName) == null) { try { definePackage(pkgName, null, null, null, null, null, null, null); } catch (IllegalArgumentException iae) { throw new AssertionError("Cannot find package " + pkgName); } } } return super.defineClass(name, b, off, len, protectionDomain); } protected Class<?> defineClass(String name, byte[] ba) { Class<?> c; _.Var<ClassWriter> cw = _.val(null); ByteCodeVisitor enhancer = enhancerManager.generalEnhancer(name, cw); if (null == enhancer) { c = defineClassX(name, ba, 0, ba.length, DOMAIN); } else { ClassWriter w = new ClassWriter(ClassWriter.COMPUTE_MAXS); cw.set(w); enhancer.commitDownstream(); ClassReader r; r = new ClassReader(ba); try { r.accept(enhancer, 0); byte[] baNew = w.toByteArray(); c = defineClassX(name, baNew, 0, baNew.length, DOMAIN); } catch (RuntimeException e) { throw e; } catch (Error e) { throw e; } catch (Exception e) { throw E.unexpected("Error processing class " + name); } } return c; } protected static java.security.ProtectionDomain DOMAIN; static { DOMAIN = (java.security.ProtectionDomain) java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Object run() { return BootstrapClassLoader.class.getProtectionDomain(); } }); } protected static final Set<String> protectedClasses = C.set( BootstrapClassLoader.class.getName(), ClassInfoRepository.class.getName(), ClassNode.class.getName(), ServerBootstrapClassLoader.class.getName(), FullStackAppBootstrapClassLoader.class.getName(), ActClassLoader.class.getName(), PluginClassProvider.class.getName() //Plugin.class.getName(), //ClassFilter.class.getName() ); }