Coverage Report - org.simpleframework.xml.convert.ScannerBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ScannerBuilder
100%
7/7
100%
2/2
2.4
ScannerBuilder$Entry
100%
16/16
90%
9/10
2.4
 
 1  
 /*
 2  
  * ScannerBuilder.java January 2010
 3  
  *
 4  
  * Copyright (C) 2010, 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.convert;
 20  
 
 21  
 import java.lang.annotation.Annotation;
 22  
 
 23  
 import org.simpleframework.xml.util.ConcurrentCache;
 24  
 
 25  
 /**
 26  
  * The <code>ScannerBuilder</code> is used to build and cache each
 27  
  * scanner requested. Building and caching scanners ensures that 
 28  
  * annotations can be acquired from a class quickly as a scan only 
 29  
  * needs to be performed once. Each scanner built scans the class 
 30  
  * provided as well as all the classes in the hierarchy.
 31  
  * 
 32  
  * @author Niall Gallagher
 33  
  * 
 34  
  * @see org.simpleframework.xml.convert.ConverterScanner
 35  
  */
 36  
 class ScannerBuilder extends ConcurrentCache<Scanner> {
 37  
 
 38  
    /**
 39  
     * Constructor for the <code>ScannerBuilder</code> object. This
 40  
     * will create a builder for annotation scanners. Each of the
 41  
     * scanners build will be cached internally to ensure that any
 42  
     * further requests for the scanner are quicker.
 43  
     */
 44  
    public ScannerBuilder() {
 45  13
       super();
 46  13
    }
 47  
    
 48  
    /**
 49  
     * This is used to build <code>Scanner</code> objects that are
 50  
     * used to scan the provided class for annotations. Each scanner
 51  
     * instance is cached once created to ensure it does not need to
 52  
     * be built twice, which improves the performance.
 53  
     * 
 54  
     * @param type this is the type to build a scanner object for
 55  
     * 
 56  
     * @return this will return a scanner instance for the given type
 57  
     */
 58  
    public Scanner build(Class<?> type) {
 59  107
       Scanner scanner = get(type);
 60  
       
 61  107
       if(scanner == null) {
 62  29
          scanner = new Entry(type);
 63  29
          put(type, scanner);
 64  
       }
 65  107
       return scanner;
 66  
    }
 67  
     
 68  
    /**
 69  
     * The <code>Entry</code> object represents a scanner that is
 70  
     * used to scan a specified type for annotations. All annotations
 71  
     * scanned from the type are cached so that they do not need to
 72  
     * be looked up twice. This ensures scanning is much quicker.
 73  
     * 
 74  
     * @author Niall Gallagher
 75  
     */
 76  
    private static class Entry extends ConcurrentCache<Annotation> implements Scanner {
 77  
       
 78  
       /**
 79  
        * This class is the subject for all annotation scans performed.
 80  
        */
 81  
       private final Class root;
 82  
       
 83  
       /**
 84  
        * Constructor for the <code>Entry</code> object is used to 
 85  
        * create a scanner that will scan the specified type. All
 86  
        * annotations that are scanned are cached to ensure that they
 87  
        * do not need to be looked up twice. This ensures that scans
 88  
        * are quicker including ones that result in null.
 89  
        * 
 90  
        * @param root this is the root class that is to be scanned
 91  
        */
 92  29
       public Entry(Class root) {
 93  29
          this.root = root;
 94  29
       }
 95  
       
 96  
       /**
 97  
        * This method will scan a class for the specified annotation. 
 98  
        * If the annotation is found on the class, or on one of the 
 99  
        * super types then it is returned. All scans will be cached 
 100  
        * to ensure scanning is only performed once.
 101  
        * 
 102  
        * @param type this is the annotation type to be scanned for
 103  
        * 
 104  
        * @return this will return the annotation if it is found
 105  
        */
 106  
       public <T extends Annotation> T scan(Class<T> type) {
 107  116
          if(!contains(type)) {
 108  68
             T value = find(type);
 109  
             
 110  68
             if(type != null && value != null) {
 111  20
                put(type, value);
 112  
             }
 113  
          }
 114  116
          return (T)get(type);
 115  
       }
 116  
       
 117  
       /**
 118  
        * This method will scan a class for the specified annotation. 
 119  
        * If the annotation is found on the class, or on one of the 
 120  
        * super types then it is returned. All scans will be cached 
 121  
        * to ensure scanning is only performed once.
 122  
        * 
 123  
        * @param label this is the annotation type to be scanned for
 124  
        * 
 125  
        * @return this will return the annotation if it is found
 126  
        */
 127  
       private <T extends Annotation> T find(Class<T> label) {
 128  68
          Class<?> type = root;
 129  
          
 130  183
          while(type != null) {
 131  135
             T value = type.getAnnotation(label);
 132  
             
 133  135
             if(value != null) {
 134  20
                return value;
 135  
             }
 136  115
             type = type.getSuperclass();
 137  115
          }
 138  48
          return null;
 139  
       }
 140  
    }
 141  
 }