View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.spi.core.util;
17  
18  import java.lang.reflect.Method;
19  
20  import de.smartics.properties.api.core.annotations.PropertyMetaDataMethod;
21  import de.smartics.properties.api.core.annotations.PropertySet;
22  
23  /**
24   * Utilities to deal with property meta data.
25   */
26  public final class PropertyUtils
27  {
28    // ********************************* Fields *********************************
29  
30    // --- constants ------------------------------------------------------------
31  
32    // --- members --------------------------------------------------------------
33  
34    // ****************************** Initializer *******************************
35  
36    // ****************************** Constructors ******************************
37  
38    private PropertyUtils()
39    {
40    }
41  
42    // ****************************** Inner Classes *****************************
43  
44    // ********************************* Methods ********************************
45  
46    // --- init -----------------------------------------------------------------
47  
48    // --- get&set --------------------------------------------------------------
49  
50    // --- business -------------------------------------------------------------
51  
52    /**
53     * Checks of the given type is a property set types.
54     * <p>
55     * Property set types are interfaces marked with a {@link PropertySet}
56     * annotation.
57     * </p>
58     *
59     * @param propertySetType the type to check.
60     * @throws NullPointerException if {@code propertySetType} is
61     *           <code>null</code>.
62     * @throws IllegalArgumentException of {@code propertySetType} is not a
63     *           property set type.
64     */
65    public static void checkPropertySetType(final Class<?> propertySetType)
66      throws NullPointerException, IllegalArgumentException
67    {
68      if (propertySetType.getAnnotation(PropertySet.class) == null
69          || !propertySetType.isInterface())
70      {
71        throw new IllegalArgumentException("Type '" + propertySetType.getName()
72                                           + "' is not annotated with '"
73                                           + PropertySet.class.getName() + "'.");
74      }
75    }
76  
77    /**
78     * Checks if the given {@code method} is a property accessor. The property
79     * accessor is not required to provide a <code>get</code> prefix.
80     *
81     * @param method the method to check for being a property accessor.
82     * @return <code>true</code> if it is a property descriptor,
83     *         <code>false</code> otherwise.
84     * @throws NullPointerException if {@code method} is <code>null</code>.
85     */
86    public static boolean isPropertyMethod(final Method method)
87      throws NullPointerException
88    {
89      final PropertyMetaDataMethod annotation =
90          method.getAnnotation(PropertyMetaDataMethod.class);
91      if (annotation == null)
92      {
93        final String name = method.getName();
94        if (!name.endsWith(PropertyMetaDataMethod.PROPERTY_DESCRIPTOR)
95            && !name.endsWith(PropertyMetaDataMethod.PROPERTY_KEY))
96        {
97          return true;
98        }
99      }
100     return false;
101   }
102 
103   // --- object basics --------------------------------------------------------
104 
105 }