Coverage Report - com.lexicalscope.fluentreflection.bean.MapBean
 
Classes in this File Line Coverage Branch Coverage Complexity
MapBean
66%
2/3
N/A
1.111
MapBean$1
6%
1/15
N/A
1.111
MapBean$1$1
0%
0/4
N/A
1.111
MapBean$1$2
0%
0/6
0%
0/4
1.111
MapBean$1$3
0%
0/4
N/A
1.111
MapBean$1$4
0%
0/4
N/A
1.111
MapBean$1$5
0%
0/4
N/A
1.111
MapBean$1$6
0%
0/4
N/A
1.111
MapBean$1$7
0%
0/4
N/A
1.111
 
 1  0
 package com.lexicalscope.fluentreflection.bean;
 2  
 
 3  
 import static com.lexicalscope.fluentreflection.FluentReflection.type;
 4  
 import static com.lexicalscope.fluentreflection.ReflectionMatchers.*;
 5  
 import static com.lexicalscope.fluentreflection.dynamicproxy.FluentProxy.dynamicProxy;
 6  
 
 7  
 import java.lang.reflect.Proxy;
 8  
 import java.util.Map;
 9  
 
 10  
 import com.lexicalscope.fluentreflection.FluentClass;
 11  
 import com.lexicalscope.fluentreflection.dynamicproxy.Implementing;
 12  
 import com.lexicalscope.fluentreflection.dynamicproxy.MethodBody;
 13  
 
 14  
 /*
 15  
  * Copyright 2011 Tim Wood
 16  
  *
 17  
  * Licensed under the Apache License, Version 2.0 (the "License");
 18  
  * you may not use this file except in compliance with the License.
 19  
  * You may obtain a copy of the License at
 20  
  *
 21  
  * http://www.apache.org/licenses/LICENSE-2.0
 22  
  *
 23  
  * Unless required by applicable law or agreed to in writing, software
 24  
  * distributed under the License is distributed on an "AS IS" BASIS,
 25  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 26  
  * See the License for the specific language governing permissions and
 27  
  * limitations under the License.
 28  
  */
 29  
 
 30  0
 public class MapBean {
 31  
     public static <T> T bean(final Class<T> klass, final Map<String, Object> map) {
 32  96
         return bean(type(klass), map);
 33  
     }
 34  
 
 35  
     public static <T> T bean(final FluentClass<T> klass, final Map<String, Object> map) {
 36  96
         return dynamicProxy(new Implementing<T>(klass) {
 37  0
             private final Object identity = new Object();
 38  
             {
 39  0
                 whenProxying(isHashCodeMethod())
 40  0
                         .execute(new MethodBody() {
 41  
                             @Override public void body() throws Throwable {
 42  0
                                 returnValue(identity.hashCode());
 43  0
                             }
 44  
                         });
 45  
 
 46  0
                 whenProxying(isEqualsMethod())
 47  0
                         .execute(new MethodBody() {
 48  
                             @Override public void body() throws Throwable {
 49  0
                                 final Object that = args()[0];
 50  0
                                 returnValue(Proxy.isProxyClass(that.getClass())
 51  0
                                         && that == proxy());
 52  0
                             }
 53  
                         });
 54  
 
 55  0
                 whenProxying(isToStringMethod()).execute(new MethodBody() {
 56  
                     @Override public void body() throws Throwable {
 57  0
                         returnValue(klass.name() + " " + map.toString());
 58  0
                     }
 59  
                 });
 60  
 
 61  0
                 whenProxying(
 62  0
                         isGetter().and(
 63  0
                                 hasType(boolean.class).or(
 64  0
                                         hasType(Boolean.class)))).execute(new MethodBody() {
 65  
                     @Override public void body() {
 66  0
                         returnValue(map.containsKey(method().property()));
 67  0
                     }
 68  
                 });
 69  
 
 70  0
                 whenProxying(isExistence()).execute(new MethodBody() {
 71  
                     @Override public void body() {
 72  0
                         returnValue(map.containsKey(method().property()));
 73  0
                     }
 74  
                 });
 75  
 
 76  0
                 whenProxying(isQuery()).execute(new MethodBody() {
 77  
                     @Override public void body() {
 78  0
                         returnValue(map.get(method().property()));
 79  0
                     }
 80  
                 });
 81  
 
 82  0
                 whenProxying(isMutator()).execute(new MethodBody() {
 83  
                     @Override public void body() {
 84  0
                         map.put(method().property(), args()[0]);
 85  0
                     }
 86  
                 });
 87  
             }
 88  
         });
 89  
     }
 90  
 }