Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 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.callbackparams.support;
 
 import java.util.Map;

Serves as a factory for creating instances of its wrapped class. It does not really do much except hiding the checked exceptions that would otherwise have to be taken care of.

Author(s):
Henrik Kaipe
 
 public class ClassWrapper {
 
     private Class wrappedClass;
    
    
Serves as a cache for single-argument constructors.
 
     private Map constructors = new HashMap();
 
     public ClassWrapper(Class wrappedClass) {
         this. = wrappedClass;
     }
 
     public Class getWrappedClass() {
         return ;
     }
    
    
Returns a new instance, which is created with the default constructor of the wrapped class.
 
     public Object newInstance() {
         try {
             return .newInstance();
         } catch (Exception x) {
             throw new Error(x);
         }
     }
    
    
Returns a new instance of the wrapped class. It is created with the first constructor found that accepts the specified constructor argument.
 
     public Object newInstance(Object singleConstructorArgument) {
         final Constructor constr;
         if (null == singleConstructorArgument) {
             constr = chooseNullConstructor();
             if (0 == constr.getParameterTypes().length) {
                 return newInstance();
             }
         } else {
             constr = chooseConstructor(singleConstructorArgument.getClass());
         }
         try {
             return constr.newInstance(new Object[] {singleConstructorArgument});
         } catch (Exception x) {
             throw new Error(x);
         }
     }
 
     public Object newInstance(final Object[] multiargConstructorArguments) {
         final Constructor[] constructors = getWrappedClass().getConstructors();
         for (int i = 0 ; i < constructors.length ; ++i) {
             if (multiargConstructorArguments.length
                     != constructors[i].getParameterTypes().length) {
                 continue;
             }
             try {
                 return constructors[i]
                         .newInstance(multiargConstructorArguments);
             } catch (InstantiationException couldTheClassBeAbstract) {
                 throw new Error(couldTheClassBeAbstract);
             } catch (IllegalAccessException shouldNeverHappen) {
                 throw new Error(shouldNeverHappen);
             } catch (IllegalArgumentException unsuiteableConstructor) {
                 continue;
             } catch (InvocationTargetException ex) {
                 throwUnchecked(ex);
            }
        }
        throw new Error("Could not find any suiteable constructor for arguments"
                + Arrays.asList(multiargConstructorArguments));
    }
    private Constructor chooseNullConstructor() {
        Constructor constr = (Constructorthis..get(null);
        if (null != constr) {
            return constr;
            
        } else {
            try {
                constr = getWrappedClass().getConstructor(new Class[]{});
            } catch (NoSuchMethodException x) {
                final Constructor[] allConstructors =
                        getWrappedClass().getConstructors();
                for (int i = 0 ; i < allConstructors.length ; ++i) {
                    final Class[] paramTypes =
                            allConstructors[i].getParameterTypes();
                    if (1 == paramTypes.length
                            && false == paramTypes[0].isPrimitive()) {
                        constr = allConstructors[0];
                        break;
                    }
                }
            }
            if (null == constr) {
                throw new NoSuchMethodError("Cannot find any constructor that"
                        + " accepts as null as argument");
            } else {
                this..put(nullconstr);
                return constr;
            }
        }
        
    }
    
    private Constructor chooseConstructor(Class argumentClass) {
        Constructor constr = (Constructorthis..get(argumentClass);
        if (null != constr) {
            return constr;
            
        } else {
            constr = lookupExactOrPrimitiveConstructor(argumentClass);
            if (null == constr) {
                final Constructor[] allConstructors =
                        getWrappedClass().getConstructors();
                for (int i = 0 ; i < allConstructors.length ; ++i) {
                    final Class[] paramTypes =
                            allConstructors[i].getParameterTypes();
                    if (1 == paramTypes.length
                            && paramTypes[0].isAssignableFrom(argumentClass)) {
                        constr = allConstructors[i];
                        break;
                    }
                }
            }
            if (null == constr) {
                throw new NoSuchMethodError("Cannot find any constructor that"
                        + " accepts a single " + argumentClass + " argument.");
            } else {
                this..put(argumentClassconstr);
                return constr;
            }
        }
    }
    private Constructor lookupExactOrPrimitiveConstructor(Class argumentClass) {
        Class[] paramClassArray = {argumentClass};
        try {
            return getWrappedClass().getConstructor(paramClassArray);
        } catch (NoSuchMethodException x) {
            if (Integer.class == argumentClass) {
                paramClassArray[0] = int.class;
            } else if (Boolean.class == argumentClass) {
                paramClassArray[0] = boolean.class;
            } else if (Character.class == argumentClass) {
                paramClassArray[0] = char.class;
            } else if (Byte.class == argumentClass) {
                paramClassArray[0] = byte.class;
            } else if (Long.class == argumentClass) {
                paramClassArray[0] = long.class;
            } else if (Double.class == argumentClass) {
                paramClassArray[0] = double.class;
            } else if (Float.class == argumentClass) {
                paramClassArray[0] = float.class;
            } else if (Short.class == argumentClass) {
                paramClassArray[0] = short.class;
            } else {
                return null;
            }
            try {
                return getWrappedClass().getConstructor(paramClassArray);
            } catch (NoSuchMethodException ex) {
                return null;
            }
        }
    }
    private void throwUnchecked(InvocationTargetException x)
    throws RuntimeExceptionError {
        Throwable t = x.getTargetException();
        if (t instanceof RuntimeException) {
            throw (RuntimeException)t;
        } else if (t instanceof Error) {
            throw (Error)t;
        } else {
            throw new Error(t);
        }
    }
New to GrepCode? Check out our FAQ X