EMMA Coverage Report (generated Wed Aug 10 12:30:04 EDT 2011)
[all classes][org.jclouds.json.internal]

COVERAGE SUMMARY FOR SOURCE FILE [EnumTypeAdapterThatReturnsFromValue.java]

nameclass, %method, %block, %line, %
EnumTypeAdapterThatReturnsFromValue.java100% (2/2)86%  (6/7)85%  (62/73)78%  (14/18)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class EnumTypeAdapterThatReturnsFromValue100% (1/1)80%  (4/5)79%  (41/52)67%  (8/12)
serialize (Enum, Type, JsonSerializationContext): JsonElement 0%   (0/1)0%   (0/6)0%   (0/1)
deserialize (JsonElement, Type, JsonDeserializationContext): Enum 100% (1/1)84%  (26/31)62%  (5/8)
<static initializer> 100% (1/1)100% (9/9)100% (1/1)
EnumTypeAdapterThatReturnsFromValue (): void 100% (1/1)100% (3/3)100% (1/1)
toString (): String 100% (1/1)100% (3/3)100% (1/1)
     
class EnumTypeAdapterThatReturnsFromValue$1100% (1/1)100% (2/2)100% (21/21)100% (6/6)
EnumTypeAdapterThatReturnsFromValue$1 (): void 100% (1/1)100% (3/3)100% (1/1)
apply (Class): Method 100% (1/1)100% (18/18)100% (5/5)

1/**
2 *
3 * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4 *
5 * ====================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ====================================================================
18 */
19package org.jclouds.json.internal;
20 
21import java.lang.reflect.Method;
22import java.lang.reflect.Type;
23import java.util.Map;
24 
25import com.google.common.base.Function;
26import com.google.common.collect.MapMaker;
27import com.google.gson.JsonDeserializationContext;
28import com.google.gson.JsonDeserializer;
29import com.google.gson.JsonElement;
30import com.google.gson.JsonParseException;
31import com.google.gson.JsonPrimitive;
32import com.google.gson.JsonSerializationContext;
33import com.google.gson.JsonSerializer;
34 
35/**
36 * @author Adrian Cole
37 */
38@SuppressWarnings("unchecked")
39public class EnumTypeAdapterThatReturnsFromValue<T extends Enum<T>> implements JsonSerializer<T>, JsonDeserializer<T> {
40   public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
41      return new JsonPrimitive(src.name());
42   }
43 
44   @SuppressWarnings("cast")
45   public T deserialize(JsonElement json, Type classOfT, JsonDeserializationContext context) throws JsonParseException {
46      try {
47         return (T) Enum.valueOf((Class<T>) classOfT, json.getAsString());
48      } catch (IllegalArgumentException e) {
49         Method converter = classToConvert.get(classOfT);
50         if (converter != null)
51            try {
52               return (T) converter.invoke(null, json.getAsString());
53            } catch (Exception e1) {
54               throw e;
55            }
56         else
57            throw e;
58      }
59   }
60 
61   private final static Map<Class<?>, Method> classToConvert = new MapMaker()
62         .makeComputingMap(new Function<Class<?>, Method>() {
63 
64            @Override
65            public Method apply(Class<?> from) {
66               try {
67                  Method method = from.getMethod("fromValue", String.class);
68                  method.setAccessible(true);
69                  return method;
70               } catch (Exception e) {
71                  return null;
72               }
73            }
74 
75         });
76 
77   @Override
78   public String toString() {
79      return EnumTypeAdapterThatReturnsFromValue.class.getSimpleName();
80   }
81}

[all classes][org.jclouds.json.internal]
EMMA 2.0.5312 (C) Vladimir Roubtsov