Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004-2010 Oracle Corporation. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at Contributors: Kohsuke Kawaguchi /
package org.kohsuke.stapler;
Allows "tear-off" objects to be linked to the parent object.

This mechanism is used to avoid static linking optional packages, so that stapler can work even when the optional dependencies are missing.

Kohsuke Kawaguchi
public abstract class TearOffSupport {
    private volatile Map<Class,ObjecttearOffs;
    public final <T> T getTearOff(Class<T> t) {
        Map<Class,Objectm = ;
        if(m==null)     return null;
        return (T)m.get(t);
    public final <T> T loadTearOff(Class<T> t) {
        T o = getTearOff(t);
        if(o==null) {
            try {
                o = t.getConstructor(getClass()).newInstance(this);
            } catch (InstantiationException e) {
                throw new InstantiationError(e.getMessage());
            } catch (IllegalAccessException e) {
                throw new IllegalAccessError(e.getMessage());
            } catch (InvocationTargetException e) {
                Throwable ex = e.getTargetException();
                if(ex instanceof RuntimeException)
                    throw (RuntimeException)ex;
                if(ex instanceof Error)
                    throw (Error)ex;
                throw new Error(e);
            } catch (NoSuchMethodException e) {
                throw new NoSuchMethodError(e.getMessage());
        return o;
    public synchronized <T> void setTearOff(Class<T> type, T instance) {
        Map<Class,Objectm = ;
        Map<Class,Objectr = m!=null ? new HashMap<ClassObject>() : new HashMap<Class,Object>();
         = r;
New to GrepCode? Check out our FAQ X