Line 0
Link Here
|
|
|
1 |
/* ==================================================================== |
2 |
Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
contributor license agreements. See the NOTICE file distributed with |
4 |
this work for additional information regarding copyright ownership. |
5 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
(the "License"); you may not use this file except in compliance with |
7 |
the License. You may obtain a copy of the License at |
8 |
|
9 |
http://www.apache.org/licenses/LICENSE-2.0 |
10 |
|
11 |
Unless required by applicable law or agreed to in writing, software |
12 |
distributed under the License is distributed on an "AS IS" BASIS, |
13 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
See the License for the specific language governing permissions and |
15 |
limitations under the License. |
16 |
==================================================================== */ |
17 |
|
18 |
package org.apache.poi; |
19 |
|
20 |
import java.lang.reflect.AnnotatedElement; |
21 |
import java.lang.reflect.Constructor; |
22 |
import java.lang.reflect.Field; |
23 |
import java.lang.reflect.Method; |
24 |
import java.lang.reflect.Member; |
25 |
import java.lang.Deprecated; |
26 |
import java.lang.annotation.Annotation; |
27 |
|
28 |
//import org.apache.maven.artifact.versioning.DefaultArtifactVersion; |
29 |
|
30 |
import org.apache.poi.util.Removal; |
31 |
|
32 |
/** |
33 |
* Find classes, methods, and field in the POI project that have been deprecated and marked for removal |
34 |
* Looks for {@literal @}{@link java.lang.Deprecated} and {@literal @}{@link org.apache.poi.util.Removal}. |
35 |
* |
36 |
* Unfortunately, a feature that has been {@literal @}deprecated in the JavaDocs cannot be accessed by |
37 |
* reflection because JavaDocs are not compiled into the final classes. |
38 |
* Some features may be JavaDoc-deprecated and marked for {@literal @}Removal, but not {@literal @}Deprecated. |
39 |
* |
40 |
* |
41 |
* This class could be used to fail a build if a deprecated feature lingers well after its intended removal |
42 |
* date. |
43 |
* |
44 |
* It may not be possible to remove a deprecated feature if other features that depend on it were not also |
45 |
* marked for removal. For example, if deprecation and removal annotations were added to an interface but |
46 |
* not to all implementing classes, we may have to wait another 2 releases to deprecate the forgotten |
47 |
* features before removing the feature. |
48 |
* |
49 |
* For this reason, this unit test should be run in for-info-only mode, and a separate job that is not part |
50 |
* of the main JDK6-8 builds would be responsible for notifying when features marked for removal are past due. |
51 |
* |
52 |
* Based on http://www.vogella.com/tutorials/JavaAnnotations/article.html |
53 |
*/ |
54 |
public class TestDeprecatedFeatures { |
55 |
|
56 |
private static String checkDeprecated(final String message, final AnnotatedElement e) { |
57 |
final Annotation deprecated = e.getAnnotation(Deprecated.class); |
58 |
final Removal removal = (Removal) e.getAnnotation(Removal.class); |
59 |
|
60 |
if (deprecated == null && removal == null) { |
61 |
return null; |
62 |
} |
63 |
|
64 |
// compare the current version from build.xml (given by |
65 |
final String currentVersion = System.getProperty("version.id"); //set by build script (build.xml, build.gradle) |
66 |
if (currentVersion != null && removal != null) { |
67 |
final String removalVersion = removal.version(); |
68 |
if (removalVersion != null) { |
69 |
// Compare the version numbers with some version-aware string parser |
70 |
// org.apache.maven.artifact.versioning.ComparableVersion is ASL 2.0-licensed |
71 |
// org.apache.maven.artifact.versioning.DefaultArtifactVersion |
72 |
//assertThat(new DefaultArtifactVersion(currentVersion), |
73 |
// greaterThan(new DefaultArtifactVersion(removalVersion))); |
74 |
} |
75 |
} |
76 |
final String fullName = e.toString(); |
77 |
final String preamble = message + " [" + fullName + "] "; |
78 |
|
79 |
if (deprecated != null && removal != null) { |
80 |
return preamble + "is deprecated and scheduled for removal in POI " + removal.version(); |
81 |
} |
82 |
else if (deprecated != null) { |
83 |
return preamble + "is deprecated but no scheduled removal version is specified"; |
84 |
} |
85 |
else if (removal != null) { |
86 |
return preamble + "is scheduled for removal in POI " + removal.version(); |
87 |
} |
88 |
return null; |
89 |
} |
90 |
private static void check(final String message, final AnnotatedElement e) { |
91 |
String s = checkDeprecated(message, e); |
92 |
if (s != null) { |
93 |
System.out.println(s); |
94 |
} |
95 |
} |
96 |
|
97 |
public static void main(final String[] args) { |
98 |
Annotation deprecated, removal; |
99 |
|
100 |
for (final Class klass : ClassFinder.find("org.apache.poi")) { |
101 |
// check if the class is deprecated |
102 |
check("Class", klass); |
103 |
// inner classes and enums will be checked later by the ClassFinder. |
104 |
|
105 |
// check if the class contains any deprecated contructors |
106 |
for (final Constructor constructor : klass.getDeclaredConstructors()) { |
107 |
check("Constructor", constructor); |
108 |
} |
109 |
|
110 |
// check if the class contains any deprecated methods |
111 |
for (final Method method : klass.getDeclaredMethods()) { |
112 |
check("Method", method); |
113 |
} |
114 |
|
115 |
// check if the class contains any deprecated fields |
116 |
// https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html |
117 |
for (final Field field : klass.getDeclaredFields()) { |
118 |
check("Field", field); |
119 |
} |
120 |
|
121 |
} |
122 |
} |
123 |
|
124 |
} |
125 |
native |