View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.util;
35  
36  import static java.beans.Introspector.decapitalize;
37  
38  import info.magnolia.annotation.deprecation.MgnlDeprecated;
39  import info.magnolia.jcr.node2bean.PropertyTypeDescriptor;
40  import info.magnolia.jcr.node2bean.TypeMapping;
41  
42  import java.lang.reflect.AnnotatedElement;
43  import java.lang.reflect.Method;
44  import java.util.Collection;
45  import java.util.List;
46  import java.util.Optional;
47  
48  import org.apache.commons.lang3.ClassUtils;
49  import org.apache.commons.lang3.StringUtils;
50  
51  import com.google.common.collect.ImmutableSet;
52  import com.google.common.collect.Lists;
53  
54  /**
55   * Provides convenience methods for processing {@link Deprecated} and {@link MgnlDeprecated} annotations.
56   */
57  public final class DeprecationUtil {
58  
59      private DeprecationUtil() {
60      }
61  
62      /**
63       * Checks whether the given parameter {@code element} is Deprecated or not.
64       */
65      public static boolean isDeprecated(AnnotatedElement element) {
66          return element.isAnnotationPresent(Deprecated.class);
67      }
68  
69      /**
70       * Searches for {@link Deprecated} super types of the given {@code clazz}.
71       *
72       * <p>
73       *      Returns the first encountered Deprecated Interface or Class (in favour of Interface).
74       *      Returns {@link Optional#EMPTY}, if nothing found.
75       * </p>
76       */
77      public static Optional<Class<?>> findFirstEncounteredDeprecatedSuperType(Class<?> clazz) {
78          Optional<Class<?>> deprecatedFirstSuperInterface = ClassUtils.getAllInterfaces(clazz).stream()
79                  .filter(DeprecationUtil::isDeprecated)
80                  .findFirst();
81  
82          if (deprecatedFirstSuperInterface.isPresent()) {
83              return deprecatedFirstSuperInterface;
84          }
85  
86          return ClassUtils.getAllSuperclasses(clazz).stream()
87                  .filter(DeprecationUtil::isDeprecated)
88                  .findFirst();
89      }
90  
91      /**
92       * Returns {@link Deprecated} methods from {@code parentClass}.
93       *
94       * <p>
95       *     Internally fetches class hierarchy of the given {@code parentClass} and iterates over them to find
96       *     all deprecated methods. This is done because those methods could be deprecated in the super class/interface,
97       *     however, not in the direct {@code parentClass}.
98       * </p>
99       */
100     public static Collection<Method> getDeprecatedReadMethods(TypeMapping mapping, Class<?> parentClass, String sourcePropertyName) {
101         List<Method> deprecatedMethods = Lists.newArrayList();
102 
103         Collection<Class<?>> classes = DeprecationUtil.getInheritanceTree(parentClass);
104         for (Class<?> clazz : classes) {
105 
106             PropertyTypeDescriptor propertyTypeDescriptor = mapping.getPropertyTypeDescriptor(clazz, sourcePropertyName);
107             if (propertyTypeDescriptor.getType() != null) {
108 
109                 // check if getter method itself is deprecated
110                 Method readMethod = propertyTypeDescriptor.getReadMethod();
111                 if (readMethod != null && DeprecationUtil.isDeprecated(readMethod)) {
112                     deprecatedMethods.add(readMethod);
113                 }
114             }
115         }
116 
117         return deprecatedMethods;
118     }
119 
120     /**
121      * Searches for all super types of the given {@code clazz}.
122      */
123     private static Collection<Class<?>> getInheritanceTree(Class<?> clazz) {
124         List<Class<?>> allSuperInterfaces = ClassUtils.getAllInterfaces(clazz);
125         List<Class<?>> allSuperclasses = ClassUtils.getAllSuperclasses(clazz);
126 
127         return ImmutableSet.<Class<?>>builder()
128                 .add(clazz)
129                 .addAll(allSuperInterfaces)
130                 .addAll(allSuperclasses)
131                 .build();
132     }
133 
134     /**
135      * Returns human readable deprecation information for given {@code deprecatedClass}.
136      */
137     public static String getDeprecationMessage(Class<?> deprecatedClass) {
138         StringBuilder deprecationWarning = new StringBuilder();
139         deprecationWarning.append(String.format("Configuration was evaluated to a deprecated type: [%s]",
140                 deprecatedClass.getCanonicalName().replaceAll("\\$\\$.*$", "")));
141 
142         appendDeprecationInformation(deprecatedClass, deprecationWarning);
143         return deprecationWarning.toString();
144     }
145 
146     /**
147      * Returns human readable deprecation information for given {@code actualClass} and {@code deprecatedParentClass}.
148      */
149     public static String getDeprecationMessage(Class<?> actualClass, Class<?> deprecatedParentClass) {
150         StringBuilder deprecationWarning = new StringBuilder();
151         deprecationWarning.append(String.format("Configuration was evaluated to a deprecated type: [%s] due to deprecated parent type: [%s]",
152                 actualClass.getCanonicalName().replaceAll("\\$\\$.*$", ""), deprecatedParentClass.getCanonicalName().replaceAll("\\$\\$.*$", "")));
153 
154         appendDeprecationInformation(deprecatedParentClass, deprecationWarning);
155         return deprecationWarning.toString();
156     }
157 
158     /**
159      * Returns human readable deprecation information for given {@code deprecatedMethod}.
160      */
161     public static String getDeprecationMessage(Method deprecatedMethod) {
162         StringBuilder deprecationWarning = new StringBuilder();
163         deprecationWarning.append(String.format("Configuration relies on a property: [%s] which is deprecated",
164                 decapitalize(deprecatedMethod.getName().replaceAll("^(get|is)", ""))));
165 
166         appendDeprecationInformation(deprecatedMethod, deprecationWarning);
167         return deprecationWarning.toString();
168     }
169 
170     /**
171      * Returns human readable deprecation information.
172      */
173     public static String getDeprecationMessage(String deprecatedType, String deprecatedTypeName ,String since, String description) {
174         StringBuilder deprecationMessage = new StringBuilder("Deprecated " + deprecatedType + " definition '" + deprecatedTypeName + "' is used.");
175         deprecationMessage.append("\n").append("Deprecated since: ").append(since);
176 
177         if (StringUtils.isNotBlank(description)) {
178             deprecationMessage.append("\n").append("Description: ").append(description);
179         }
180 
181         return deprecationMessage.toString();
182     }
183 
184     /**
185      * If {@link MgnlDeprecated} annotation is present, parses and appends its information to {@code deprecationWarning}.
186      */
187     private static void appendDeprecationInformation(AnnotatedElement deprecatedClass, StringBuilder deprecationWarning) {
188         if (deprecatedClass.isAnnotationPresent(MgnlDeprecated.class)) {
189             MgnlDeprecated deprecationAnnotation = deprecatedClass.getAnnotation(MgnlDeprecated.class);
190             deprecationWarning.append("\n").append("Deprecated since: ").append(deprecationAnnotation.since());
191             if (StringUtils.isNotBlank(deprecationAnnotation.description())) {
192                 deprecationWarning.append("\n").append("Description: ").append(deprecationAnnotation.description());
193             }
194         }
195     }
196 }