Search in sources :

Example 31 with GroovyRuntimeException

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

the class AbstractDynamicObject method getWriteOnlyProperty.

protected GroovyRuntimeException getWriteOnlyProperty(String name) {
    Class<?> publicType = getPublicType();
    boolean includeDisplayName = hasUsefulDisplayName();
    if (publicType != null && includeDisplayName) {
        return new GroovyRuntimeException(String.format("Cannot get the value of write-only property '%s' for %s of type %s.", name, getDisplayName(), publicType.getName()));
    } else if (publicType != null) {
        return new GroovyRuntimeException(String.format("Cannot get the value of write-only property '%s' for object of type %s.", name, publicType.getName()));
    } else {
        // Use the display name anyway
        return new GroovyRuntimeException(String.format("Cannot get the value of write-only property '%s' for %s.", name, getDisplayName()));
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException)

Example 32 with GroovyRuntimeException

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

the class AbstractDynamicObject method setReadOnlyProperty.

protected GroovyRuntimeException setReadOnlyProperty(String name) {
    Class<?> publicType = getPublicType();
    boolean includeDisplayName = hasUsefulDisplayName();
    if (publicType != null && includeDisplayName) {
        return new GroovyRuntimeException(String.format("Cannot set the value of read-only property '%s' for %s of type %s.", name, getDisplayName(), publicType.getName()));
    } else if (publicType != null) {
        return new GroovyRuntimeException(String.format("Cannot set the value of read-only property '%s' for object of type %s.", name, publicType.getName()));
    } else {
        // Use the display name anyway
        return new GroovyRuntimeException(String.format("Cannot set the value of read-only property '%s' for %s.", name, getDisplayName()));
    }
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException)

Example 33 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

the class MixinInMetaClass method mixinClassesToMetaClass.

public static void mixinClassesToMetaClass(MetaClass self, List<Class> categoryClasses) {
    final Class selfClass = self.getTheClass();
    if (self instanceof HandleMetaClass) {
        self = (MetaClass) ((HandleMetaClass) self).replaceDelegate();
    }
    if (!(self instanceof ExpandoMetaClass)) {
        if (self instanceof DelegatingMetaClass && ((DelegatingMetaClass) self).getAdaptee() instanceof ExpandoMetaClass) {
            self = ((DelegatingMetaClass) self).getAdaptee();
        } else {
            throw new GroovyRuntimeException("Can't mixin methods to meta class: " + self);
        }
    }
    ExpandoMetaClass mc = (ExpandoMetaClass) self;
    List<MetaMethod> arr = new ArrayList<MetaMethod>();
    for (Class categoryClass : categoryClasses) {
        final CachedClass cachedCategoryClass = ReflectionCache.getCachedClass(categoryClass);
        final MixinInMetaClass mixin = new MixinInMetaClass(mc, cachedCategoryClass);
        final MetaClass metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(categoryClass);
        final List<MetaProperty> propList = metaClass.getProperties();
        for (MetaProperty prop : propList) if (self.getMetaProperty(prop.getName()) == null) {
            mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
        }
        for (MetaProperty prop : cachedCategoryClass.getFields()) if (self.getMetaProperty(prop.getName()) == null) {
            mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
        }
        for (MetaMethod method : metaClass.getMethods()) {
            final int mod = method.getModifiers();
            if (!Modifier.isPublic(mod))
                continue;
            if (method instanceof CachedMethod && ((CachedMethod) method).getCachedMethod().isSynthetic())
                continue;
            if (Modifier.isStatic(mod)) {
                if (method instanceof CachedMethod)
                    staticMethod(self, arr, (CachedMethod) method);
            } else if (method.getDeclaringClass().getTheClass() != Object.class || method.getName().equals("toString")) {
                //                    if (self.pickMethod(method.getName(), method.getNativeParameterTypes()) == null) {
                final MixinInstanceMetaMethod metaMethod = new MixinInstanceMetaMethod(method, mixin);
                arr.add(metaMethod);
            //                    }
            }
        }
    }
    for (Object res : arr) {
        final MetaMethod metaMethod = (MetaMethod) res;
        if (metaMethod.getDeclaringClass().isAssignableFrom(selfClass))
            mc.registerInstanceMethod(metaMethod);
        else {
            mc.registerSubclassInstanceMethod(metaMethod);
        }
    }
}
Also used : MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) MetaMethod(groovy.lang.MetaMethod) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ArrayList(java.util.ArrayList) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) MixinInstanceMetaProperty(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixinInstanceMetaProperty(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty) MetaProperty(groovy.lang.MetaProperty) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)

Example 34 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

the class ProcessGroovyMethods method pipeTo.

/**
     * Allows one Process to asynchronously pipe data to another Process.
     *
     * @param left  a Process instance
     * @param right a Process to pipe output to
     * @return the second Process to allow chaining
     * @throws java.io.IOException if an IOException occurs.
     * @since 1.5.2
     */
public static Process pipeTo(final Process left, final Process right) throws IOException {
    new Thread(new Runnable() {

        public void run() {
            InputStream in = new BufferedInputStream(getIn(left));
            OutputStream out = new BufferedOutputStream(getOut(right));
            byte[] buf = new byte[8192];
            int next;
            try {
                while ((next = in.read(buf)) != -1) {
                    out.write(buf, 0, next);
                }
            } catch (IOException e) {
                throw new GroovyRuntimeException("exception while reading process stream", e);
            } finally {
                closeWithWarning(out);
            }
        }
    }).start();
    return right;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 35 with GroovyRuntimeException

use of groovy.lang.GroovyRuntimeException in project groovy-core by groovy.

the class ProxyGeneratorAdapter method proxy.

@SuppressWarnings("unchecked")
public GroovyObject proxy(Map<Object, Object> map, Object... constructorArgs) {
    if (constructorArgs == null && cachedNoArgConstructor != null) {
        // if there isn't any argument, we can make invocation faster using the cached constructor
        try {
            return (GroovyObject) cachedNoArgConstructor.newInstance(map);
        } catch (InstantiationException e) {
            throw new GroovyRuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new GroovyRuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new GroovyRuntimeException(e);
        }
    }
    if (constructorArgs == null)
        constructorArgs = EMPTY_ARGS;
    Object[] values = new Object[constructorArgs.length + 1];
    System.arraycopy(constructorArgs, 0, values, 0, constructorArgs.length);
    values[values.length - 1] = map;
    return DefaultGroovyMethods.<GroovyObject>newInstance(cachedClass, values);
}
Also used : GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyObject(groovy.lang.GroovyObject) InvocationTargetException(java.lang.reflect.InvocationTargetException) GroovyObject(groovy.lang.GroovyObject)

Aggregations

GroovyRuntimeException (groovy.lang.GroovyRuntimeException)59 IOException (java.io.IOException)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)9 GroovyObject (groovy.lang.GroovyObject)8 ArrayList (java.util.ArrayList)6 Closure (groovy.lang.Closure)5 InputStream (java.io.InputStream)5 List (java.util.List)5 GroovyShell (groovy.lang.GroovyShell)4 MetaClass (groovy.lang.MetaClass)4 Map (java.util.Map)4 ClassNode (org.codehaus.groovy.ast.ClassNode)4 MethodNode (org.codehaus.groovy.ast.MethodNode)4 MetaMethod (groovy.lang.MetaMethod)3 Script (groovy.lang.Script)3 PrintWriter (java.io.PrintWriter)3 StringWriter (java.io.StringWriter)3 URL (java.net.URL)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)2 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)2