本文整理汇总了Java中groovy.lang.DelegatesTo.Target方法的典型用法代码示例。如果您正苦于以下问题:Java DelegatesTo.Target方法的具体用法?Java DelegatesTo.Target怎么用?Java DelegatesTo.Target使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类groovy.lang.DelegatesTo
的用法示例。
在下文中一共展示了DelegatesTo.Target方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: with
import groovy.lang.DelegatesTo; //导入方法依赖的package包/类
/**
* Allows the closure to be called for the object reference self.
* <p/>
* Any method invoked inside the closure will first be invoked on the
* self reference. For example, the following method calls to the append()
* method are invoked on the StringBuilder instance and then, because
* 'returning' is true, the self instance is returned:
* <pre class="groovyTestCase">
* def b = new StringBuilder().with(true) {
* append('foo')
* append('bar')
* }
* assert b.toString() == 'foobar'
* </pre>
* The returning parameter is commonly set to true when using with to simplify object
* creation, such as this example:
* <pre>
* def p = new Person().with(true) {
* firstName = 'John'
* lastName = 'Doe'
* }
* </pre>
* Alternatively, 'tap' is an alias for 'with(true)', so that method can be used instead.
*
* The other main use case for with is when returning a value calculated using self as shown here:
* <pre>
* def fullName = person.with(false){ "$firstName $lastName" }
* </pre>
* Alternatively, 'with' is an alias for 'with(false)', so the boolean parameter can be ommitted instead.
*
* @param self the object to have a closure act upon
* @param returning if true, return the self object; otherwise, the result of calling the closure
* @param closure the closure to call on the object
* @return the self object or the result of calling the closure depending on 'returning'
* @see #with(Object, Closure)
* @see #tap(Object, Closure)
* @since 2.5.0
*/
public static <T,U extends T, V extends T> T with(
@DelegatesTo.Target("self") U self,
boolean returning,
@DelegatesTo(value=DelegatesTo.Target.class,
target="self",
strategy=Closure.DELEGATE_FIRST)
@ClosureParams(FirstParam.class)
Closure<T> closure) {
@SuppressWarnings("unchecked")
final Closure<V> clonedClosure = (Closure<V>) closure.clone();
clonedClosure.setResolveStrategy(Closure.DELEGATE_FIRST);
clonedClosure.setDelegate(self);
V result = clonedClosure.call(self);
return returning ? self : result;
}
示例2: identity
import groovy.lang.DelegatesTo; //导入方法依赖的package包/类
/**
* Allows the closure to be called for the object reference self.
* Synonym for 'with()'.
*
* @param self the object to have a closure act upon
* @param closure the closure to call on the object
* @return result of calling the closure
* @see #with(Object, Closure)
* @since 1.0
*/
public static <T,U> T identity(
@DelegatesTo.Target("self") U self,
@DelegatesTo(value=DelegatesTo.Target.class,
target="self",
strategy=Closure.DELEGATE_FIRST)
@ClosureParams(FirstParam.class)
Closure<T> closure) {
return DefaultGroovyMethods.with(self, closure);
}
示例3: tap
import groovy.lang.DelegatesTo; //导入方法依赖的package包/类
/**
* Allows the closure to be called for the object reference self (similar
* to <code>with</code> and always returns self.
* <p>
* Any method invoked inside the closure will first be invoked on the
* self reference. For instance, the following method calls to the append()
* method are invoked on the StringBuilder instance:
* <pre>
* def b = new StringBuilder().tap {
* append('foo')
* append('bar')
* }
* assert b.toString() == 'foobar'
* </pre>
* This is commonly used to simplify object creation, such as this example:
* <pre>
* def p = new Person().tap {
* firstName = 'John'
* lastName = 'Doe'
* }
* </pre>
*
* @param self the object to have a closure act upon
* @param closure the closure to call on the object
* @return self
* @see #with(Object, boolean, Closure)
* @see #with(Object, Closure)
* @since 2.5.0
*/
@SuppressWarnings("unchecked")
public static <T,U> U tap(
@DelegatesTo.Target("self") U self,
@DelegatesTo(value=DelegatesTo.Target.class,
target="self",
strategy=Closure.DELEGATE_FIRST)
@ClosureParams(FirstParam.class)
Closure<T> closure) {
return (U) with(self, true, (Closure<Object>)closure);
}
示例4: of
import groovy.lang.DelegatesTo; //导入方法依赖的package包/类
/**
* A shortcut for directly creating an object using {@code AutoBuilder}.
*
* <p> Interaction with the {@link BuilderDSL} is done inside the passed-in {@code
* Closure} by setting properties as if the closure directly acted upon the
* to-be-created object. Properties are specified by their names as strings, and
* nested properties are supported using a dot '{@code .}' as a separator. Setting a
* property inside the closure is equivalent to calling the {@link
* BuilderDSL#with(String, Object)} method.
*
* <p> For example:
* <pre>{@code
* class Person {
* String name;
* Address address;
* }
*
* // Groovy code:
* def person = Person.of {
* name = "Granger, Hermione"
* address.city = LONDON
* }
* }</pre>
*
* @param self an object on which this extension method is invoked
* @param instanceData the closure that sets the property values on the built object
* @param <T> the type of the object to be built
*
* @return an instance of {@code T}, or {@code null} if configured so, equivalent to
* calling the {@link BuilderDSL#build()} method
*/
@Nullable
public static <T> T of(
@DelegatesTo.Target Class<T> self,
@DelegatesTo(strategy = DELEGATE_FIRST, genericTypeIndex = 0) Closure<?> instanceData) {
return TableDSL.parseSingle(AutoBuilder.instanceOf(self), instanceData);
}
示例5: fromTable
import groovy.lang.DelegatesTo; //导入方法依赖的package包/类
/**
* A shortcut for the {@link #fromTable(BuilderDSL, Closure)} method.
*
* <p> Can be called on the class object directly without the need to create a {@code
* BuilderDSL}. This method is helpful in <i>IDE</i>s that offer proper
* auto-completion features for the properties in the passed-in closure (and that do
* not have such auto-completion when using the other {@code fromTable} method).
*
* @param self an object on which this extension method is invoked
* @param tableData closure with the definition of the tabular data
* @param <T> the type of objects to be built
*
* @return a list with one instance of {@code T} created using the {@link
* BuilderDSL#build()} method for each row in the table; never {@code null}, but may
* be {@code empty} and may contain {@code null} elements if the builder was
* configured to resolve some elements as {@code null}
*
* @see #fromTable(BuilderDSL, Closure)
*/
public static <T> List<T> fromTable(
@DelegatesTo.Target Class<T> self,
@DelegatesTo(strategy = DELEGATE_FIRST, genericTypeIndex = 0) Closure<?> tableData) {
return fromTable(AutoBuilder.instanceOf(self), tableData);
}