Coverage Report - org.simpleframework.xml.core.DetailExtractor
 
Classes in this File Line Coverage Branch Coverage Complexity
DetailExtractor
100%
31/31
57%
8/14
2.167
 
 1  
 /*
 2  
  * DetailExtractor.java July 2006
 3  
  *
 4  
  * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
 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 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.core;
 20  
 
 21  
 import org.simpleframework.xml.util.Cache;
 22  
 import org.simpleframework.xml.util.ConcurrentCache;
 23  
 
 24  
 /**
 25  
  * The <code>DetailExtractor</code> object is used to extract details
 26  
  * for a specific class. All details extracted are cached so that 
 27  
  * they can be reused when requested several times. This provides an
 28  
  * increase in performance when there are large class hierarchies
 29  
  * as annotations does not need to be scanned a second time.
 30  
  * 
 31  
  * @author Niall Gallagher
 32  
  * 
 33  
  * @see org.simpleframework.xml.core.Detail
 34  
  */
 35  
 class DetailExtractor {
 36  
    
 37  
    /**
 38  
     * This is the cache of methods for specific classes scanned.
 39  
     */
 40  
    private final Cache<ContactList> methods;
 41  
    
 42  
    /**
 43  
     * This is the cache of fields for specific classes scanned.
 44  
     */
 45  
    private final Cache<ContactList> fields;
 46  
    
 47  
    /**
 48  
     * This contains a cache of the details scanned for classes.
 49  
     */
 50  
    private final Cache<Detail> details;
 51  
    
 52  
    /**
 53  
     * This contains various support functions for the details.
 54  
     */
 55  
    private final Support support;
 56  
    
 57  
    /**
 58  
     * Constructor for the <code>DetailExtractor</code> object. This
 59  
     * is used to extract various details for a class, such as the
 60  
     * method and field details as well as the annotations used on
 61  
     * the class. The primary purpose for this is to create cachable
 62  
     * values that reduce the amount of reflection required.
 63  
     * 
 64  
     * @param support this contains various support functions
 65  
     */
 66  950
    public DetailExtractor(Support support) {
 67  950
       this.methods = new ConcurrentCache<ContactList>();
 68  950
       this.fields = new ConcurrentCache<ContactList>();
 69  950
       this.details = new ConcurrentCache<Detail>();
 70  950
       this.support = support;
 71  950
    }
 72  
    
 73  
    /**
 74  
     * This is used to get a <code>Detail</code> object describing a
 75  
     * class and its annotations. Any detail retrieved from this will 
 76  
     * be cached to increase the performance of future accesses.
 77  
     * 
 78  
     * @param type this is the type to acquire the detail for
 79  
     * 
 80  
     * @return an object describing the type and its annotations
 81  
     */
 82  
    public Detail getDetail(Class type) {
 83  13552
       Detail detail = details.fetch(type);
 84  
       
 85  13552
       if(detail == null) {
 86  4272
          detail = new DetailScanner(type);
 87  4272
          details.cache(type,  detail);
 88  
       }
 89  13552
       return detail;
 90  
    }
 91  
    
 92  
    /**
 93  
     * This is used to acquire a list of <code>Contact</code> objects
 94  
     * that represent the annotated fields in a type. The entire
 95  
     * class hierarchy is scanned for annotated fields. Caching of
 96  
     * the contact list is done to increase performance.
 97  
     * 
 98  
     * @param type this is the type to scan for annotated fields
 99  
     * 
 100  
     * @return this returns a list of the annotated fields
 101  
     */
 102  
    public ContactList getFields(Class type) throws Exception {
 103  3388
       ContactList list = fields.fetch(type);
 104  
       
 105  3388
       if(list == null) {
 106  3388
          Detail detail = getDetail(type);
 107  
          
 108  3388
          if(detail != null) {
 109  3388
             list = getFields(detail);
 110  
          }
 111  
       }
 112  3388
       return list;
 113  
    }
 114  
    
 115  
    /**
 116  
     * This is used to acquire a list of <code>Contact</code> objects
 117  
     * that represent the annotated fields in a type. The entire
 118  
     * class hierarchy is scanned for annotated fields. Caching of
 119  
     * the contact list is done to increase performance.
 120  
     * 
 121  
     * @param detail this is the detail to scan for annotated fields
 122  
     * 
 123  
     * @return this returns a list of the annotated fields
 124  
     */
 125  
    private ContactList getFields(Detail detail) throws Exception {
 126  3388
       ContactList list = new FieldScanner(detail, support);
 127  
       
 128  3388
       if(detail != null) {
 129  3388
          fields.cache(detail, list);
 130  
       }
 131  3388
       return list;
 132  
    }
 133  
    
 134  
    /**
 135  
     * This is used to acquire a list of <code>Contact</code> objects
 136  
     * that represent the annotated methods in a type. The entire
 137  
     * class hierarchy is scanned for annotated methods. Caching of
 138  
     * the contact list is done to increase performance.
 139  
     * 
 140  
     * @param type this is the type to scan for annotated methods
 141  
     * 
 142  
     * @return this returns a list of the annotated methods
 143  
     */
 144  
    public ContactList getMethods(Class type) throws Exception {
 145  3393
       ContactList list = methods.fetch(type);
 146  
       
 147  3393
       if(list == null) {
 148  3393
          Detail detail = getDetail(type);
 149  
          
 150  3393
          if(detail != null) {
 151  3393
             list = getMethods(detail);
 152  
          }
 153  
       }
 154  3389
       return list;
 155  
    }
 156  
    
 157  
    /**
 158  
     * This is used to acquire a list of <code>Contact</code> objects
 159  
     * that represent the annotated methods in a type. The entire
 160  
     * class hierarchy is scanned for annotated methods. Caching of
 161  
     * the contact list is done to increase performance.
 162  
     * 
 163  
     * @param detail this is the type to scan for annotated methods
 164  
     * 
 165  
     * @return this returns a list of the annotated methods
 166  
     */
 167  
    private ContactList getMethods(Detail detail) throws Exception {
 168  3393
       ContactList list = new MethodScanner(detail, support);
 169  
       
 170  3389
       if(detail != null) {
 171  3389
          methods.cache(detail, list);
 172  
       }
 173  3389
       return list;
 174  
    }
 175  
 }