org.apache.brooklyn.util.groovy.GroovyJavaMethods Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brooklyn-utils-groovy Show documentation
Show all versions of brooklyn-utils-groovy Show documentation
Groovy extensions and utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.brooklyn.util.groovy;
import java.util.concurrent.Callable;
import org.apache.brooklyn.util.concurrent.CallableFromRunnable;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;
import org.codehaus.groovy.runtime.callsite.CallSite;
import org.codehaus.groovy.runtime.callsite.CallSiteArray;
import org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import groovy.lang.Closure;
import groovy.lang.GString;
/**
* Handy methods available in groovy packaged so they can be consumed from java,
* and other conversion/conveniences; but see JavaGroovyEquivalents for faster alternatives.
*
* @deprecated since 0.11.0; explicit groovy utilities/support will be deleted.
*/
@Deprecated
public class GroovyJavaMethods {
private static final CallSiteArray CALL_SITE_ARRAY = new CallSiteArray(GroovyJavaMethods.class, new String[] {"metaClass", "invokeMethod"});
//TODO use named subclasses, would that be more efficient?
// TODO xFromY methods not in correct class: they are not "handy method available in groovy"?
public static Closure closureFromRunnable(final Runnable job) {
return new FromRunnableClosure(GroovyJavaMethods.class, job);
}
public static Closure closureFromCallable(final Callable job) {
return new FromCallableClosure(GroovyJavaMethods.class, job);
}
public static Closure closureFromFunction(final Function,T> job) {
return new FromFunctionClosure(GroovyJavaMethods.class, job);
}
@SuppressWarnings("unchecked")
public static Callable callableFromClosure(final Closure job) {
try {
return (Callable)ScriptBytecodeAdapter.asType(job, Callable.class);
} catch (Throwable e) {
throw Exceptions.propagate(e);
}
}
@SuppressWarnings("unchecked")
public static Callable callableFromRunnable(final Runnable job) {
try {
if (safeGroovyIsCase(job, Callable.class)) {
return (Callable)ScriptBytecodeAdapter.asType(job, Callable.class);
} else {
return CallableFromRunnable.newInstance(job, null);
}
} catch (Throwable e) {
throw Exceptions.propagate(e);
}
}
/**
* Alternative implementation of {@link ScriptBytecodeAdapter#isCase(Object, Object)}
* Stripped down to work only for caseExpression of type java.lang.Class
.
* It behaves the same way only for cases when caseExpression java.lang.Class
does not implement isCase
method.
* It goes directly to {@link DefaultGroovyMethods#isCase(Object, Object)} method instead of using Groovy dynamic invocation.
* This saves extra operations and avoids the locks used in Groovy dynamic invocation. See BROOKLYN-424.
*/
public static boolean safeGroovyIsCase(Object switchValue, Class caseExpression) {
if (caseExpression == null) {
return switchValue == null;
}
return DefaultGroovyMethods.isCase(caseExpression, switchValue);
}
public static Predicate predicateFromClosure(final Closure job) {
return new Predicate() {
@Override
public boolean apply(Object input) {
return job.call(input);
}
};
}
public static Function functionFromClosure(final Closure job) {
return new Function() {
@Override
public T apply(F input) {
return job.call(input);
}
};
}
@SuppressWarnings("unchecked")
public static Predicate castToPredicate(Object o) {
try {
if (safeGroovyIsCase(o, Closure.class)) {
return predicateFromClosure((Closure)o);
} else {
return (Predicate) o;
}
} catch (Throwable e) {
throw Exceptions.propagate(e);
}
}
@SuppressWarnings("unchecked")
public static Closure castToClosure(Object o) {
try {
if (ScriptBytecodeAdapter.compareEqual(o, null)) {
return (Closure)ScriptBytecodeAdapter.castToType(o, Closure.class);
} else if (safeGroovyIsCase(o, Closure.class)) {
return (Closure)ScriptBytecodeAdapter.castToType(o, Closure.class);
} else if (o instanceof Runnable) {
return closureFromRunnable((Runnable)ScriptBytecodeAdapter.createPojoWrapper(ScriptBytecodeAdapter.castToType(o, Runnable.class), Runnable.class));
} else if (o instanceof Callable) {
return closureFromCallable((Callable)ScriptBytecodeAdapter.createPojoWrapper(ScriptBytecodeAdapter.castToType(o, Callable.class), Callable.class));
} else if (o instanceof Function) {
return closureFromFunction((Function