当前位置: 首页>>代码示例>>Java>>正文


Java DelegatesTo.Target方法代码示例

本文整理汇总了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;
}
 
开发者ID:apache,项目名称:groovy,代码行数:54,代码来源:DefaultGroovyMethods.java

示例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);
}
 
开发者ID:apache,项目名称:groovy,代码行数:20,代码来源:DefaultGroovyMethods.java

示例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);
}
 
开发者ID:apache,项目名称:groovy,代码行数:40,代码来源:DefaultGroovyMethods.java

示例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);
}
 
开发者ID:jakubkolar,项目名称:autobuilder,代码行数:38,代码来源:BuilderDSLGroovyMethods.java

示例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);
}
 
开发者ID:jakubkolar,项目名称:autobuilder,代码行数:25,代码来源:BuilderDSLGroovyMethods.java


注:本文中的groovy.lang.DelegatesTo.Target方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。