Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2003-2007 the original author or authors.
   *
   * Licensed 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.codehaus.groovy.runtime.metaclass;
 
 
 
This MetaBeanProperty will create a pseudo property whose value is bound to an object using weak references. The values will go out of scope and be garabage collected when the the object is collected In fact, this class should be called ExpandoProperty

Author(s):
Graeme Rocher
Alex Tkachman
Since:
1.1
 
     private static final CachedClass[] ZERO_ARGUMENT_LIST = new CachedClass[0];
 
     private final ManagedConcurrentMap instance2Prop;
 
     private Class declaringClass;
     private ThreadBoundGetter getter;
     private ThreadBoundSetter setter;
     private Object initialValue;
     private Closure initialValueCreator;
     
     private final static ReferenceBundle softBundle = ReferenceBundle.getSoftBundle();

    
Retrieves the initial value of the ThreadBound property

Returns:
The initial value
 
     public synchronized Object getInitialValue() {
         return getInitialValue(null);
     }
 
     public synchronized Object getInitialValue(Object object) {
         if ( != null) {
             return .call(object);
         }
         return ;
 
     }

    
Closure responsible for creating the initial value of thread-managed bean properties

Parameters:
callable The closure responsible for creating the initial value
 
     public void setInitialValueCreator(Closure callable) {
         this. = callable;
     }

    
Constructs a new ThreadManagedBeanProperty for the given arguments

Parameters:
declaringClass The class that declares the property
name The name of the property
type The type of the property
iv The properties initial value
 
     public ThreadManagedMetaBeanProperty(Class declaringClassString nameClass typeObject iv) {
         super(nametypenullnull);
         this. = type;
         this. = declaringClass;
 
         this. = new ThreadBoundGetter(name);
         this. = new ThreadBoundSetter(name);
          = iv;
 
          = getInstance2PropName(name);
    }

    
Constructs a new ThreadManagedBeanProperty for the given arguments

Parameters:
declaringClass The class that declares the property
name The name of the property
type The type of the property
initialValueCreator The closure responsible for creating the initial value
    public ThreadManagedMetaBeanProperty(Class declaringClassString nameClass typeClosure initialValueCreator) {
        super(nametypenullnull);
        this. = type;
        this. = declaringClass;
        this. = new ThreadBoundGetter(name);
        this. = new ThreadBoundSetter(name);
        this. = initialValueCreator;
         = getInstance2PropName(name);
    }
    private static ManagedConcurrentMap getInstance2PropName(String name) {
        ManagedConcurrentMap res = .get(name);
        if (res == null) {
            res = new ManagedConcurrentMap();
            ManagedConcurrentMap ores = .putIfAbsent(nameres);
            if (ores != null)
              return ores;
        }
        return res;
    }
    /* (non-Javadoc)
      * @see groovy.lang.MetaBeanProperty#getGetter()
      */
    public MetaMethod getGetter() {
        return this.;
    }
    /* (non-Javadoc)
      * @see groovy.lang.MetaBeanProperty#getSetter()
      */
    public MetaMethod getSetter() {
        return this.;
    }


    
Accesses the ThreadBound state of the property as a getter

Author(s):
Graeme Rocher
    class ThreadBoundGetter extends MetaMethod {
        private final String name;
        public ThreadBoundGetter(String name) {
            setParametersTypes(new CachedClass[0]);
            this. = getGetterName(name);
        }
        public int getModifiers() {
            return .;
        }
        public String getName() {
            return ;
        }
        public Class getReturnType() {
            return ;
        }
        public CachedClass getDeclaringClass() {
            return ReflectionCache.getCachedClass();
        }
        /* (non-Javadoc)
           * @see groovy.lang.MetaMethod#invoke(java.lang.Object, java.lang.Object[])
           */
        public Object invoke(Object objectObject[] arguments) {
            return ((ManagedConcurrentMap.EntryWithValue).getOrPut(objectgetInitialValue())).getValue();
        }
    }

    
Sets the ThreadBound state of the property like a setter

Author(s):
Graeme Rocher
    private class ThreadBoundSetter extends MetaMethod {
        private final String name;
        public ThreadBoundSetter(String name) {
            setParametersTypes (new CachedClass [] {ReflectionCache.getCachedClass()} );
            this. = getSetterName(name);
        }
        public int getModifiers() {
            return .;
        }
        /* (non-Javadoc)
		 * @see groovy.lang.MetaMethod#getName()
		 */
        public String getName() {
            return ;
        }
        public Class getReturnType() {
            return ;
        }
        public CachedClass getDeclaringClass() {
            return ReflectionCache.getCachedClass();
        }
        /* (non-Javadoc)
           * @see groovy.lang.MetaMethod#invoke(java.lang.Object, java.lang.Object[])
           */
        public Object invoke(Object objectObject[] arguments) {
            .put(objectarguments[0]);
            return null;
        }
    }
New to GrepCode? Check out our FAQ X