Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2013 John Ericksen 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.parceler;
 
 
Static utility class used to wrap an `@Parcel` annotated class with the generated `Parcelable` wrapper.

Author(s):
John Ericksen
 
 public final class Parcels {
 
     public static final String PARCELS_NAME = "Parcels";
     public static final String PARCELS_REPOSITORY_NAME = "Parceler$$Parcels";
     public static final String PARCELS_PACKAGE = "org.parceler";
     public static final String IMPL_EXT = "Parcelable";
 
     private static final ParcelCodeRepository REPOSITORY = new ParcelCodeRepository();
 
     private Parcels(){
         // private utility class constructor
     }

    
Testing method for replacing the Parceler$Parcels class with one referenced in the given classloader.

Parameters:
classLoader ClassLoader to use when loading repository.
 
     protected static void update(ClassLoader classLoader){
         .loadRepository(classLoader);
     }

    
Wraps the input `@Parcel` annotated class with a `Parcelable` wrapper.

Parameters:
input Parcel
Returns:
Parcelable wrapper
Throws:
ParcelerRuntimeException if there was an error looking up the wrapped Parceler$Parcels class.
 
     @SuppressWarnings("unchecked")
     public static <T> Parcelable wrap(T input) {
         ParcelableFactory parcelableFactory = .get(input.getClass());
 
         return parcelableFactory.buildParcelable(input);
     }

    
Unwraps the input wrapped `@Parcel` `Parcelable`

Parameters:
input Parcelable implementing ParcelWrapper
<T> type of unwrapped `@Parcel`
Returns:
Unwrapped `@Parcel`
Throws:
java.lang.ClassCastException if the input Parcelable does not implement ParcelWrapper with the correct parameter type.
 
     @SuppressWarnings("unchecked")
     public static <T> T unwrap(Parcelable input) {
         ParcelWrapper<T> wrapper = (ParcelWrapper<T>) input;
         return wrapper.getParcel();
     }

    
Factory class for building a `Parcelable` from the given input.
 
     public interface ParcelableFactory<T> {
 
         String BUILD_PARCELABLE = "buildParcelable";

        
Build the corresponding `Parcelable` class.

Parameters:
input input to wrap with a Parcelable
Returns:
Parcelable instance
 
         Parcelable buildParcelable(T input);
     }
 
     private static final class ParcelableFactoryReflectionProxy<T> implements Parcels.ParcelableFactory<T> {
 
         private final Constructor<? extends Parcelableconstructor;
 
        public ParcelableFactoryReflectionProxy(Class<T> parcelClassClass<? extends ParcelableparcelWrapperClass) {
            try {
                this. = parcelWrapperClass.getConstructor(parcelClass);
            } catch (NoSuchMethodException e) {
                throw new ParcelerRuntimeException("Unable to create ParcelFactory Type"e);
            }
        }
        @Override
        public Parcelable buildParcelable(T input) {
            try {
                return .newInstance(input);
            } catch (InstantiationException e) {
                throw new ParcelerRuntimeException("Unable to create ParcelFactory Type"e);
            } catch (IllegalAccessException e) {
                throw new ParcelerRuntimeException("Unable to create ParcelFactory Type"e);
            } catch (InvocationTargetException e) {
                throw new ParcelerRuntimeException("Unable to create ParcelFactory Type"e);
            }
        }
    }
    private static final class ParcelCodeRepository {
        public ParcelCodeRepository() {
            loadRepository(getClass().getClassLoader());
        }
        public ParcelableFactory get(Class clazz){
            ParcelableFactory result = .get(clazz);
            if (result == null) {
                ParcelableFactory value = findClass(clazz);
                if(value == null){
                    throw new ParcelerRuntimeException("Unable to create ParcelableFactory");
                }
                result = .putIfAbsent(clazzvalue);
                if (result == null) {
                    result = value;
                }
            }
            return result;
        }
        @SuppressWarnings("unchecked")
        public ParcelableFactory findClass(Class clazz){
            try {
                Class parcelWrapperClass = Class.forName(clazz.getName() + "$$" + );
                return new ParcelableFactoryReflectionProxy(clazzparcelWrapperClass);
            } catch (ClassNotFoundException e) {
                return null;
            }
        }

        
Update the repository class from the given classloader. If the given repository class cannot be instantiated then this method will throw a ParcelerRuntimeException.

Parameters:
classLoader
Throws:
ParcelerRuntimeException
        @SuppressWarnings("unchecked")
        public void loadRepository(ClassLoader classLoader){
            try{
                Class repositoryClass = classLoader.loadClass( + "." + );
                Repository<ParcelableFactoryinstance = (Repository<ParcelableFactory>) repositoryClass.newInstance();
                .putAll(instance.get());
            } catch (ClassNotFoundException e) {
                //nothing
            } catch (InstantiationException e) {
                throw new ParcelerRuntimeException("Unable to instantiate generated Repository"e);
            } catch (IllegalAccessException e) {
                throw new ParcelerRuntimeException("Unable to access generated Repository"e);
            }
        }
    }
New to GrepCode? Check out our FAQ X