Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 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.felix.scrplugin.description; |
| 20 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
| 23 | |
| 24 | /** |
| 25 | * A <code>ClassDescription</code> is describing the found annotations |
| 26 | * of a class. |
| 27 | * Its basically used as a wrapper to hold all the various annotations |
| 28 | * found in a class. This description only contains the annotations |
| 29 | * found in the current class, but not in any superclass. |
| 30 | * The annotations are available as descriptions. |
| 31 | * @see ComponentDescription |
| 32 | * @see ServiceDescription |
| 33 | * @see ReferenceDescription |
| 34 | * @see PropertyDescription |
| 35 | */ |
| 36 | public class ClassDescription { |
| 37 | |
| 38 | /** All descriptions. */ |
| 39 | final List<AbstractDescription> descriptions = new ArrayList<AbstractDescription>(); |
| 40 | |
| 41 | /** The corresponding class. */ |
| 42 | private final Class<?> describedClass; |
| 43 | |
| 44 | /** The source (file) of the class or any other location information. */ |
| 45 | private final String source; |
| 46 | |
| 47 | /** |
| 48 | * Create a new class description |
| 49 | */ |
| 50 | public ClassDescription(final Class<?> describedClass, final String source) { |
| 51 | this.describedClass = describedClass; |
| 52 | this.source = source; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the associated class. |
| 57 | * @return The associated class. |
| 58 | */ |
| 59 | public Class<?> getDescribedClass() { |
| 60 | return this.describedClass; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the location information like the source file etc. |
| 65 | * @return The location information. |
| 66 | */ |
| 67 | public String getSource() { |
| 68 | return this.source; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Add a new description |
| 73 | */ |
| 74 | public void add(final AbstractDescription desc) { |
| 75 | this.descriptions.add(desc); |
Carsten Ziegeler | 9a496fe | 2012-06-27 06:20:28 +0000 | [diff] [blame] | 76 | desc.setSource(this.source); |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get all descriptions of that type. |
| 81 | * @param descType The description class. |
| 82 | * @return A list of found descriptions or the empty array. |
| 83 | */ |
| 84 | @SuppressWarnings("unchecked") |
| 85 | public <T extends AbstractDescription> List<T> getDescriptions(final Class<T> descType) { |
| 86 | final List<T> result = new ArrayList<T>(); |
| 87 | for(final AbstractDescription desc : descriptions) { |
| 88 | if ( descType.isAssignableFrom(desc.getClass()) ) { |
| 89 | result.add((T) desc); |
| 90 | } |
| 91 | } |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the first description of that type |
| 97 | * @param descType The description class |
| 98 | * @return THe first description or <code>null</code> |
| 99 | */ |
| 100 | public <T extends AbstractDescription> T getDescription(final Class<T> descType) { |
| 101 | final List<T> result = this.getDescriptions(descType); |
| 102 | if ( result.size() > 0 ) { |
| 103 | return result.get(0); |
| 104 | } |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public String toString() { |
| 110 | return "ClassDescription [descriptions=" + descriptions |
| 111 | + ", describedClass=" + describedClass + ", source=" + source |
| 112 | + "]"; |
| 113 | } |
Carsten Ziegeler | 547de0c | 2012-06-28 11:22:40 +0000 | [diff] [blame] | 114 | |
| 115 | public ClassDescription clone() { |
| 116 | final ClassDescription cd = new ClassDescription(this.describedClass, this.source); |
| 117 | for(final AbstractDescription ad : this.descriptions) { |
| 118 | cd.add(ad.clone()); |
| 119 | } |
| 120 | return cd; |
| 121 | } |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 122 | } |