Coverage Report - org.apache.johnzon.mapper.reflection.JohnzonParameterizedType
 
Classes in this File Line Coverage Branch Coverage Complexity
JohnzonParameterizedType
45%
18/40
17%
5/28
3
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. 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,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.johnzon.mapper.reflection;
 20  
 
 21  
 import java.lang.reflect.ParameterizedType;
 22  
 import java.lang.reflect.Type;
 23  
 import java.util.Arrays;
 24  
 
 25  
 public class JohnzonParameterizedType implements ParameterizedType {
 26  
     private final Type rawType;
 27  
     private final Type[] types;
 28  
 
 29  4
     public JohnzonParameterizedType(final Type raw, final Type... types) {
 30  4
         if (raw == null) {
 31  1
             final ParameterizedType userFinalType = findUserParameterizedType();
 32  1
             this.rawType = userFinalType.getRawType();
 33  1
             this.types = userFinalType.getActualTypeArguments();
 34  1
         } else {
 35  3
             this.rawType = raw;
 36  3
             this.types = types;
 37  
         }
 38  4
     }
 39  
 
 40  
     private ParameterizedType findUserParameterizedType() {
 41  1
         final Type genericSuperclass = getClass().getGenericSuperclass();
 42  1
         if (!ParameterizedType.class.isInstance(genericSuperclass)) {
 43  0
             throw new IllegalArgumentException("raw can be null only for children classes");
 44  
         }
 45  1
         final ParameterizedType pt = ParameterizedType.class.cast(genericSuperclass); // our type, then unwrap it
 46  
 
 47  1
         final Type userType = pt.getActualTypeArguments()[0];
 48  1
         if (!ParameterizedType.class.isInstance(userType)) {
 49  0
             throw new IllegalArgumentException("You need to pass a parameterized type to Johnzon*Types");
 50  
         }
 51  
 
 52  1
         return ParameterizedType.class.cast(userType);
 53  
     }
 54  
 
 55  
     @Override
 56  
     public Type[] getActualTypeArguments() {
 57  4
         return types.clone();
 58  
     }
 59  
 
 60  
     @Override
 61  
     public Type getOwnerType() {
 62  0
         return null;
 63  
     }
 64  
 
 65  
     @Override
 66  
     public Type getRawType() {
 67  4
         return rawType;
 68  
     }
 69  
 
 70  
     @Override
 71  
     public int hashCode() {
 72  11
         return Arrays.hashCode(types) ^ (rawType == null ? 0 : rawType.hashCode());
 73  
     }
 74  
 
 75  
     @Override
 76  
     public boolean equals(final Object obj) {
 77  0
         if (this == obj) {
 78  0
             return true;
 79  0
         } else if (obj instanceof ParameterizedType) {
 80  0
             final ParameterizedType that = (ParameterizedType) obj;
 81  0
             final Type thatRawType = that.getRawType();
 82  0
             return that.getOwnerType() == null
 83  
                     && (rawType == null ? thatRawType == null : rawType.equals(thatRawType))
 84  
                     && Arrays.equals(types, that.getActualTypeArguments());
 85  
         } else {
 86  0
             return false;
 87  
         }
 88  
     }
 89  
 
 90  
     @Override
 91  
     public String toString() {
 92  0
         final StringBuilder buffer = new StringBuilder();
 93  0
         buffer.append(((Class<?>) rawType).getSimpleName());
 94  0
         final Type[] actualTypes = getActualTypeArguments();
 95  0
         if (actualTypes.length > 0) {
 96  0
             buffer.append("<");
 97  0
             int length = actualTypes.length;
 98  0
             for (int i = 0; i < length; i++) {
 99  0
                 buffer.append(actualTypes[i].toString());
 100  0
                 if (i != actualTypes.length - 1) {
 101  0
                     buffer.append(",");
 102  
                 }
 103  
             }
 104  
 
 105  0
             buffer.append(">");
 106  
         }
 107  0
         return buffer.toString();
 108  
     }
 109  
 }