Search in sources :

Example 1 with MissingPropertyException

use of groovy.lang.MissingPropertyException in project groovy by apache.

the class GroovyResultSetExtension method setProperty.

/**
     * Updates the designated column with an <code>Object</code> value.
     *
     * @param columnName the SQL name of the column
     * @param newValue   the updated value
     * @throws MissingPropertyException if an SQLException happens while setting the new value
     * @see groovy.lang.GroovyObject#setProperty(java.lang.String, java.lang.Object)
     * @see ResultSet#updateObject(java.lang.String, java.lang.Object)
     */
public void setProperty(String columnName, Object newValue) {
    try {
        getResultSet().updateObject(columnName, newValue);
        updated = true;
    } catch (SQLException e) {
        throw new MissingPropertyException(columnName, GroovyResultSetProxy.class, e);
    }
}
Also used : SQLException(java.sql.SQLException) MissingPropertyException(groovy.lang.MissingPropertyException)

Example 2 with MissingPropertyException

use of groovy.lang.MissingPropertyException in project groovy by apache.

the class GroovyRowResult method getAt.

/**
     * Retrieve the value of the property by its index.
     * A negative index will count backwards from the last column.
     *
     * @param index is the number of the column to look at
     * @return the value of the property
     */
public Object getAt(int index) {
    try {
        // a negative index will count backwards from the last column.
        if (index < 0)
            index += result.size();
        Iterator it = result.values().iterator();
        int i = 0;
        Object obj = null;
        while ((obj == null) && (it.hasNext())) {
            if (i == index)
                obj = it.next();
            else
                it.next();
            i++;
        }
        return obj;
    } catch (Exception e) {
        throw new MissingPropertyException(Integer.toString(index), GroovyRowResult.class, e);
    }
}
Also used : Iterator(java.util.Iterator) MissingPropertyException(groovy.lang.MissingPropertyException) MissingPropertyException(groovy.lang.MissingPropertyException)

Example 3 with MissingPropertyException

use of groovy.lang.MissingPropertyException in project groovy by apache.

the class GroovyScriptEngineImpl method eval.

// package-privates
Object eval(Class<?> scriptClass, final ScriptContext ctx) throws ScriptException {
    /*
         * We use the following Binding instance so that global variable lookup
         * will be done in the current ScriptContext instance.
         */
    Binding binding = new Binding(ctx.getBindings(ScriptContext.ENGINE_SCOPE)) {

        @Override
        public Object getVariable(String name) {
            synchronized (ctx) {
                int scope = ctx.getAttributesScope(name);
                if (scope != -1) {
                    return ctx.getAttribute(name, scope);
                }
                // Redirect script output to context writer, if out var is not already provided
                if ("out".equals(name)) {
                    Writer writer = ctx.getWriter();
                    if (writer != null) {
                        return (writer instanceof PrintWriter) ? (PrintWriter) writer : new PrintWriter(writer, true);
                    }
                }
                // Provide access to engine context, if context var is not already provided
                if ("context".equals(name)) {
                    return ctx;
                }
            }
            throw new MissingPropertyException(name, getClass());
        }

        @Override
        public void setVariable(String name, Object value) {
            synchronized (ctx) {
                int scope = ctx.getAttributesScope(name);
                if (scope == -1) {
                    scope = ScriptContext.ENGINE_SCOPE;
                }
                ctx.setAttribute(name, value, scope);
            }
        }
    };
    try {
        // then simply return that class
        if (!Script.class.isAssignableFrom(scriptClass)) {
            return scriptClass;
        } else {
            // it's a script
            Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
            // save all current closures into global closures map
            Method[] methods = scriptClass.getMethods();
            for (Method m : methods) {
                String name = m.getName();
                globalClosures.put(name, new MethodClosure(scriptObject, name));
            }
            MetaClass oldMetaClass = scriptObject.getMetaClass();
            /*
                * We override the MetaClass of this script object so that we can
                * forward calls to global closures (of previous or future "eval" calls)
                * This gives the illusion of working on the same "global" scope.
                */
            scriptObject.setMetaClass(new DelegatingMetaClass(oldMetaClass) {

                @Override
                public Object invokeMethod(Object object, String name, Object args) {
                    if (args == null) {
                        return invokeMethod(object, name, MetaClassHelper.EMPTY_ARRAY);
                    }
                    if (args instanceof Tuple) {
                        return invokeMethod(object, name, ((Tuple) args).toArray());
                    }
                    if (args instanceof Object[]) {
                        return invokeMethod(object, name, (Object[]) args);
                    } else {
                        return invokeMethod(object, name, new Object[] { args });
                    }
                }

                @Override
                public Object invokeMethod(Object object, String name, Object[] args) {
                    try {
                        return super.invokeMethod(object, name, args);
                    } catch (MissingMethodException mme) {
                        return callGlobal(name, args, ctx);
                    }
                }

                @Override
                public Object invokeStaticMethod(Object object, String name, Object[] args) {
                    try {
                        return super.invokeStaticMethod(object, name, args);
                    } catch (MissingMethodException mme) {
                        return callGlobal(name, args, ctx);
                    }
                }
            });
            return scriptObject.run();
        }
    } catch (Exception e) {
        throw new ScriptException(e);
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) CompiledScript(javax.script.CompiledScript) MissingPropertyException(groovy.lang.MissingPropertyException) Method(java.lang.reflect.Method) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) MissingPropertyException(groovy.lang.MissingPropertyException) ScriptException(javax.script.ScriptException) MissingMethodException(groovy.lang.MissingMethodException) IOException(java.io.IOException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) ScriptException(javax.script.ScriptException) MissingMethodException(groovy.lang.MissingMethodException) MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) Tuple(groovy.lang.Tuple) PrintWriter(java.io.PrintWriter)

Example 4 with MissingPropertyException

use of groovy.lang.MissingPropertyException in project gradle by gradle.

the class AbstractDynamicObject method getMissingProperty.

public MissingPropertyException getMissingProperty(String name) {
    Class<?> publicType = getPublicType();
    boolean includeDisplayName = hasUsefulDisplayName();
    if (publicType != null && includeDisplayName) {
        return new MissingPropertyException(String.format("Could not get unknown property '%s' for %s of type %s.", name, getDisplayName(), publicType.getName()), name, publicType);
    } else if (publicType != null) {
        return new MissingPropertyException(String.format("Could not get unknown property '%s' for object of type %s.", name, publicType.getName()), name, publicType);
    } else {
        // Use the display name anyway
        return new MissingPropertyException(String.format("Could not get unknown property '%s' for %s.", name, getDisplayName()), name, null);
    }
}
Also used : MissingPropertyException(groovy.lang.MissingPropertyException)

Example 5 with MissingPropertyException

use of groovy.lang.MissingPropertyException in project gradle by gradle.

the class AbstractDynamicObject method setMissingProperty.

public MissingPropertyException setMissingProperty(String name) {
    Class<?> publicType = getPublicType();
    boolean includeDisplayName = hasUsefulDisplayName();
    if (publicType != null && includeDisplayName) {
        return new MissingPropertyException(String.format("Could not set unknown property '%s' for %s of type %s.", name, getDisplayName(), publicType.getName()), name, publicType);
    } else if (publicType != null) {
        return new MissingPropertyException(String.format("Could not set unknown property '%s' for object of type %s.", name, publicType.getName()), name, publicType);
    } else {
        // Use the display name anyway
        return new MissingPropertyException(String.format("Could not set unknown property '%s' for %s.", name, getDisplayName()), name, null);
    }
}
Also used : MissingPropertyException(groovy.lang.MissingPropertyException)

Aggregations

MissingPropertyException (groovy.lang.MissingPropertyException)16 GString (groovy.lang.GString)4 MetaClass (groovy.lang.MetaClass)4 MissingMethodException (groovy.lang.MissingMethodException)4 Script (groovy.lang.Script)4 Tuple (groovy.lang.Tuple)4 IOException (java.io.IOException)4 Binding (groovy.lang.Binding)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)3 PrintWriter (java.io.PrintWriter)3 Method (java.lang.reflect.Method)3 CompiledScript (javax.script.CompiledScript)3 ScriptException (javax.script.ScriptException)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)3 MetaProperty (groovy.lang.MetaProperty)2 Range (groovy.lang.Range)2 Writer (java.io.Writer)2 SQLException (java.sql.SQLException)2 Iterator (java.util.Iterator)2