Search in sources :

Example 1 with Symbol

use of clojure.lang.Symbol in project invesdwin-context by invesdwin.

the class ClojureBindings method map.

private Map<String, Object> map() {
    final Map<String, Object> map = new HashMap<String, Object>();
    final Namespace ns = Namespace.find(namespaceIntern);
    for (final Object el : ns.getMappings()) {
        final MapEntry entry = (MapEntry) el;
        final Symbol key = (Symbol) entry.key();
        final Object valAt = ns.getMappings().valAt(key);
        final Var valVar = valAt instanceof Var ? ((Var) valAt) : null;
        if (valVar == null) {
            // skip non-variables
            continue;
        }
        if (valVar.ns != ns) {
            // skip non-user vars
            continue;
        }
        if (!valVar.isBound()) {
            // skip unbound vars
            continue;
        }
        map.put(key.getName(), valVar.get());
    }
    return map;
}
Also used : MapEntry(clojure.lang.MapEntry) HashMap(java.util.HashMap) Symbol(clojure.lang.Symbol) Var(clojure.lang.Var) Namespace(clojure.lang.Namespace)

Example 2 with Symbol

use of clojure.lang.Symbol in project spring-boost by jaju.

the class Boost method initialize.

private void initialize(String initSymbol) {
    logger.info(() -> "Initializing clojure code: " + initSymbol);
    Var var = (Var) Clojure.var(initSymbol);
    Symbol sym = var.toSymbol();
    String ns = sym.getNamespace();
    require.invoke(Clojure.read(ns));
    var.invoke(applicationContext);
}
Also used : Var(clojure.lang.Var) Symbol(clojure.lang.Symbol)

Example 3 with Symbol

use of clojure.lang.Symbol in project invesdwin-context by invesdwin.

the class ClojureBindings method clear.

@Override
public void clear() {
    final Symbol nsSymbol = namespaceIntern;
    final Namespace ns = Namespace.find(nsSymbol);
    for (final Object el : ns.getMappings()) {
        final MapEntry entry = (MapEntry) el;
        final Symbol key = (Symbol) entry.key();
        final Object valAt = ns.getMappings().valAt(key);
        final Var valVar = valAt instanceof Var ? ((Var) valAt) : null;
        if (valVar == null) {
            // skip non-variables
            continue;
        }
        if (valVar.ns != ns) {
            // skip non-user vars
            continue;
        }
        if (!valVar.isBound()) {
            // skip unbound vars
            continue;
        }
        RT.var(CORE_NS, "ns-unmap").invoke(nsSymbol, key);
    }
}
Also used : MapEntry(clojure.lang.MapEntry) Symbol(clojure.lang.Symbol) Var(clojure.lang.Var) Namespace(clojure.lang.Namespace)

Example 4 with Symbol

use of clojure.lang.Symbol in project tapestry-5 by apache.

the class ClojureBuilderImpl method build.

@Override
public <T> T build(final Class<T> interfaceType) {
    assert interfaceType != null;
    assert interfaceType.isInterface();
    Namespace annotation = interfaceType.getAnnotation(Namespace.class);
    if (annotation == null) {
        throw new IllegalArgumentException(String.format("Interface type %s does not have the Namespace annotation.", interfaceType.getName()));
    }
    final String namespace = annotation.value();
    ClassInstantiator<T> instantiator = proxyFactory.createProxy(interfaceType, new PlasticClassTransformer() {

        @Override
        public void transform(PlasticClass plasticClass) {
            for (final Method m : interfaceType.getMethods()) {
                bridgeToClojure(plasticClass, m);
            }
        }

        private void bridgeToClojure(final PlasticClass plasticClass, final Method method) {
            final MethodDescription desc = new MethodDescription(method);
            if (method.getReturnType() == void.class) {
                throw new IllegalArgumentException(String.format("Method %s may not be void when bridging to Clojure functions.", desc));
            }
            final Symbol symbol = mapper.mapMethod(namespace, method);
            tracker.run(String.format("Mapping %s method %s to Clojure function %s", interfaceType.getName(), desc.toShortString(), symbol.toString()), new Runnable() {

                @Override
                public void run() {
                    Symbol namespaceSymbol = Symbol.create(symbol.getNamespace());
                    REQUIRE.invoke(namespaceSymbol);
                    IFn clojureFunction = Clojure.var(symbol);
                    final PlasticField fnField = plasticClass.introduceField(IFn.class, method.getName() + "IFn").inject(clojureFunction);
                    plasticClass.introduceMethod(desc).changeImplementation(new InstructionBuilderCallback() {

                        @Override
                        public void doBuild(InstructionBuilder builder) {
                            bridgeToClojure(builder, desc, fnField);
                        }
                    });
                }
            });
        }

        private void bridgeToClojure(InstructionBuilder builder, MethodDescription description, PlasticField ifnField) {
            builder.loadThis().getField(ifnField);
            int count = description.argumentTypes.length;
            Class[] invokeParameterTypes = new Class[count];
            for (int i = 0; i < count; i++) {
                invokeParameterTypes[i] = Object.class;
                builder.loadArgument(i).boxPrimitive(description.argumentTypes[i]);
            }
            Method ifnMethod = null;
            try {
                ifnMethod = IFn.class.getMethod("invoke", invokeParameterTypes);
            } catch (NoSuchMethodException ex) {
                throw new RuntimeException(String.format("Unable to find correct IFn.invoke() method: %s", ExceptionUtils.toMessage(ex)), ex);
            }
            builder.invoke(ifnMethod);
            builder.castOrUnbox(description.returnType);
            builder.returnResult();
        }
    });
    return instantiator.newInstance();
}
Also used : Symbol(clojure.lang.Symbol) Method(java.lang.reflect.Method) Namespace(org.apache.tapestry5.clojure.Namespace) IFn(clojure.lang.IFn)

Aggregations

Symbol (clojure.lang.Symbol)4 Var (clojure.lang.Var)3 MapEntry (clojure.lang.MapEntry)2 Namespace (clojure.lang.Namespace)2 IFn (clojure.lang.IFn)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Namespace (org.apache.tapestry5.clojure.Namespace)1