You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Lint.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.text.MessageFormat;
  18. import java.util.Collection;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import java.util.Properties;
  22. import org.aspectj.bridge.IMessage;
  23. import org.aspectj.bridge.ISourceLocation;
  24. import org.aspectj.bridge.MessageUtil;
  25. import org.aspectj.weaver.tools.Trace;
  26. import org.aspectj.weaver.tools.TraceFactory;
  27. public class Lint {
  28. Map<String, Lint.Kind> kinds = new HashMap<>();
  29. /* private */World world;
  30. public final Kind invalidAbsoluteTypeName = new Kind("invalidAbsoluteTypeName", "no match for this type name: {0}");
  31. public final Kind invalidWildcardTypeName = new Kind("invalidWildcardTypeName", "no match for this type pattern: {0}");
  32. public final Kind unresolvableMember = new Kind("unresolvableMember", "can not resolve this member: {0}");
  33. public final Kind typeNotExposedToWeaver = new Kind("typeNotExposedToWeaver",
  34. "this affected type is not exposed to the weaver: {0}");
  35. public final Kind shadowNotInStructure = new Kind("shadowNotInStructure",
  36. "the shadow for this join point is not exposed in the structure model: {0}");
  37. public final Kind unmatchedSuperTypeInCall = new Kind("unmatchedSuperTypeInCall",
  38. "does not match because declaring type is {0}, if match desired use target({1})");
  39. public final Kind unmatchedTargetKind = new Kind("unmatchedTargetKind", "does not match because annotation {0} has @Target{1}");
  40. public final Kind canNotImplementLazyTjp = new Kind("canNotImplementLazyTjp",
  41. "can not implement lazyTjp on this joinpoint {0} because around advice is used");
  42. public final Kind multipleAdviceStoppingLazyTjp = new Kind("multipleAdviceStoppingLazyTjp",
  43. "can not implement lazyTjp at joinpoint {0} because of advice conflicts, see secondary locations to find conflicting advice");
  44. public final Kind needsSerialVersionUIDField = new Kind("needsSerialVersionUIDField",
  45. "serialVersionUID of type {0} needs to be set because of {1}");
  46. public final Kind serialVersionUIDBroken = new Kind("brokeSerialVersionCompatibility",
  47. "serialVersionUID of type {0} is broken because of added field {1}");
  48. public final Kind noInterfaceCtorJoinpoint = new Kind("noInterfaceCtorJoinpoint",
  49. "no interface constructor-execution join point - use {0}+ for implementing classes");
  50. public final Kind noJoinpointsForBridgeMethods = new Kind(
  51. "noJoinpointsForBridgeMethods",
  52. "pointcut did not match on the method call to a bridge method. Bridge methods are generated by the compiler and have no join points");
  53. public final Kind enumAsTargetForDecpIgnored = new Kind("enumAsTargetForDecpIgnored",
  54. "enum type {0} matches a declare parents type pattern but is being ignored");
  55. public final Kind annotationAsTargetForDecpIgnored = new Kind("annotationAsTargetForDecpIgnored",
  56. "annotation type {0} matches a declare parents type pattern but is being ignored");
  57. public final Kind cantMatchArrayTypeOnVarargs = new Kind("cantMatchArrayTypeOnVarargs",
  58. "an array type as the last parameter in a signature does not match on the varargs declared method: {0}");
  59. public final Kind adviceDidNotMatch = new Kind("adviceDidNotMatch", "advice defined in {0} has not been applied");
  60. public final Kind invalidTargetForAnnotation = new Kind("invalidTargetForAnnotation",
  61. "{0} is not a valid target for annotation {1}, this annotation can only be applied to {2}");
  62. public final Kind elementAlreadyAnnotated = new Kind("elementAlreadyAnnotated",
  63. "{0} - already has an annotation of type {1}, cannot add a second instance");
  64. public final Kind runtimeExceptionNotSoftened = new Kind("runtimeExceptionNotSoftened",
  65. "{0} will not be softened as it is already a RuntimeException");
  66. public final Kind uncheckedArgument = new Kind("uncheckedArgument",
  67. "unchecked match of {0} with {1} when argument is an instance of {2} at join point {3}");
  68. public final Kind uncheckedAdviceConversion = new Kind("uncheckedAdviceConversion",
  69. "unchecked conversion when advice applied at shadow {0}, expected {1} but advice uses {2}");
  70. public final Kind noGuardForLazyTjp = new Kind("noGuardForLazyTjp",
  71. "can not build thisJoinPoint lazily for this advice since it has no suitable guard");
  72. public final Kind noExplicitConstructorCall = new Kind("noExplicitConstructorCall",
  73. "inter-type constructor does not contain explicit constructor call: field initializers in the target type will not be executed");
  74. public final Kind aspectExcludedByConfiguration = new Kind("aspectExcludedByConfiguration",
  75. "aspect {0} exluded for class loader {1}");
  76. public final Kind unorderedAdviceAtShadow = new Kind("unorderedAdviceAtShadow",
  77. "at this shadow {0} no precedence is specified between advice applying from aspect {1} and aspect {2}");
  78. public final Kind swallowedExceptionInCatchBlock = new Kind("swallowedExceptionInCatchBlock",
  79. "exception swallowed in catch block");
  80. public final Kind calculatingSerialVersionUID = new Kind("calculatingSerialVersionUID",
  81. "calculated SerialVersionUID for type {0} to be {1}");
  82. public final Kind nonReweavableTypeEncountered = new Kind("nonReweavableTypeEncountered",
  83. "class {0} is already woven and has not been built in reweavable mode");
  84. // there are a lot of messages in the cant find type family - I'm defining an umbrella lint warning that
  85. // allows a user to control their severity (for e.g. ltw or binary weaving)
  86. public final Kind cantFindType = new Kind("cantFindType", "{0}");
  87. public final Kind cantFindTypeAffectingJoinPointMatch = new Kind("cantFindTypeAffectingJPMatch", "{0}");
  88. public final Kind advisingSynchronizedMethods = new Kind("advisingSynchronizedMethods",
  89. "advice matching the synchronized method shadow ''{0}'' will be executed outside the lock rather than inside (compiler limitation)");
  90. public final Kind mustWeaveXmlDefinedAspects = new Kind(
  91. "mustWeaveXmlDefinedAspects",
  92. "XML Defined aspects must be woven in cases where cflow pointcuts are involved. Currently the include/exclude patterns exclude ''{0}''");
  93. public final Kind cannotAdviseJoinpointInInterfaceWithAroundAdvice = new Kind(
  94. "cannotAdviseJoinpointInInterfaceWithAroundAdvice",
  95. "The joinpoint ''{0}'' cannot be advised and is being skipped as the compiler implementation will lead to creation of methods with bodies in an interface (compiler limitation)");
  96. /**
  97. * Indicates an aspect could not be found when attempting reweaving.
  98. */
  99. public final Kind missingAspectForReweaving = new Kind("missingAspectForReweaving",
  100. "aspect {0} cannot be found when reweaving {1}");
  101. private static Trace trace = TraceFactory.getTraceFactory().getTrace(Lint.class);
  102. public Lint(World world) {
  103. if (trace.isTraceEnabled()) {
  104. trace.enter("<init>", this, world);
  105. }
  106. this.world = world;
  107. if (trace.isTraceEnabled()) {
  108. trace.exit("<init>");
  109. }
  110. }
  111. public void setAll(String messageKind) {
  112. if (trace.isTraceEnabled()) {
  113. trace.enter("setAll", this, messageKind);
  114. }
  115. setAll(getMessageKind(messageKind));
  116. if (trace.isTraceEnabled()) {
  117. trace.exit("setAll");
  118. }
  119. }
  120. private void setAll(IMessage.Kind messageKind) {
  121. for (Kind kind : kinds.values()) {
  122. kind.setKind(messageKind);
  123. }
  124. }
  125. public void setFromMap(Map<String,String> lintOptionsMap) {
  126. for (String key: lintOptionsMap.keySet()) {
  127. String value = lintOptionsMap.get(key);
  128. Kind kind = kinds.get(key);
  129. if (kind == null) {
  130. MessageUtil.error(world.getMessageHandler(), WeaverMessages.format(WeaverMessages.XLINT_KEY_ERROR, key));
  131. } else {
  132. kind.setKind(getMessageKind(value));
  133. }
  134. }
  135. }
  136. public void setFromProperties(File file) {
  137. if (trace.isTraceEnabled()) {
  138. trace.enter("setFromProperties", this, file);
  139. }
  140. InputStream s = null;
  141. try {
  142. s = new FileInputStream(file);
  143. setFromProperties(s);
  144. } catch (IOException ioe) {
  145. MessageUtil.error(world.getMessageHandler(),
  146. WeaverMessages.format(WeaverMessages.XLINT_LOAD_ERROR, file.getPath(), ioe.getMessage()));
  147. } finally {
  148. if (s != null) {
  149. try {
  150. s.close();
  151. } catch (IOException e) {
  152. // ignore
  153. }
  154. }
  155. }
  156. if (trace.isTraceEnabled()) {
  157. trace.exit("setFromProperties");
  158. }
  159. }
  160. public void loadDefaultProperties() {
  161. InputStream s = getClass().getResourceAsStream("XlintDefault.properties");
  162. if (s == null) {
  163. MessageUtil.warn(world.getMessageHandler(), WeaverMessages.format(WeaverMessages.XLINTDEFAULT_LOAD_ERROR));
  164. return;
  165. }
  166. try {
  167. setFromProperties(s);
  168. } catch (IOException ioe) {
  169. MessageUtil.error(world.getMessageHandler(),
  170. WeaverMessages.format(WeaverMessages.XLINTDEFAULT_LOAD_PROBLEM, ioe.getMessage()));
  171. } finally {
  172. try {
  173. s.close();
  174. } catch (IOException ioe) {
  175. // ignore
  176. }
  177. }
  178. }
  179. private void setFromProperties(InputStream s) throws IOException {
  180. Properties p = new Properties();
  181. p.load(s);
  182. setFromProperties(p);
  183. }
  184. public void setFromProperties(Properties properties) {
  185. for (Map.Entry<Object, Object> entry : properties.entrySet()) {
  186. Kind kind = kinds.get(entry.getKey());
  187. if (kind == null) {
  188. MessageUtil.error(world.getMessageHandler(), WeaverMessages.format(WeaverMessages.XLINT_KEY_ERROR, entry.getKey()));
  189. } else {
  190. kind.setKind(getMessageKind((String) entry.getValue()));
  191. }
  192. }
  193. }
  194. public Collection<Kind> allKinds() {
  195. return kinds.values();
  196. }
  197. public Kind getLintKind(String name) {
  198. return kinds.get(name);
  199. }
  200. // temporarily suppress the given lint messages
  201. public void suppressKinds(Collection<Kind> lintKind) {
  202. if (lintKind.isEmpty()) {
  203. return;
  204. }
  205. for (Kind k : lintKind) {
  206. k.setSuppressed(true);
  207. }
  208. }
  209. // remove any suppression of lint warnings in place
  210. public void clearAllSuppressions() {
  211. for (Kind k : kinds.values()) {
  212. k.setSuppressed(false);
  213. }
  214. }
  215. public void clearSuppressions(Collection<Lint.Kind> lintKinds) {
  216. for (Kind k : lintKinds) {
  217. k.setSuppressed(false);
  218. }
  219. }
  220. private IMessage.Kind getMessageKind(String v) {
  221. if (v.equals("ignore")) {
  222. return null;
  223. } else if (v.equals("warning")) {
  224. return IMessage.WARNING;
  225. } else if (v.equals("error")) {
  226. return IMessage.ERROR;
  227. }
  228. MessageUtil.error(world.getMessageHandler(), WeaverMessages.format(WeaverMessages.XLINT_VALUE_ERROR, v));
  229. return null;
  230. }
  231. public Kind fromKey(String lintkey) {
  232. return kinds.get(lintkey);
  233. }
  234. public class Kind {
  235. private final String name;
  236. private final String message;
  237. private IMessage.Kind kind = IMessage.WARNING;
  238. private boolean isSupressed = false; // by SuppressAjWarnings
  239. public Kind(String name, String message) {
  240. this.name = name;
  241. this.message = message;
  242. kinds.put(this.name, this);
  243. }
  244. public void setSuppressed(boolean shouldBeSuppressed) {
  245. this.isSupressed = shouldBeSuppressed;
  246. }
  247. public boolean isEnabled() {
  248. return (kind != null) && !isSupressed();
  249. }
  250. private boolean isSupressed() {
  251. // can't suppress errors!
  252. return isSupressed && (kind != IMessage.ERROR);
  253. }
  254. public String getName() {
  255. return name;
  256. }
  257. public IMessage.Kind getKind() {
  258. return kind;
  259. }
  260. public void setKind(IMessage.Kind kind) {
  261. this.kind = kind;
  262. }
  263. public void signal(String info, ISourceLocation location) {
  264. if (kind == null) {
  265. return;
  266. }
  267. String text = MessageFormat.format(message, new Object[] { info });
  268. text += " [Xlint:" + name + "]";
  269. world.getMessageHandler().handleMessage(new LintMessage(text, kind, location, null, getLintKind(name)));
  270. }
  271. public void signal(String[] infos, ISourceLocation location, ISourceLocation[] extraLocations) {
  272. if (kind == null) {
  273. return;
  274. }
  275. String text = MessageFormat.format(message, (Object[]) infos);
  276. text += " [Xlint:" + name + "]";
  277. world.getMessageHandler().handleMessage(new LintMessage(text, kind, location, extraLocations, getLintKind(name)));
  278. }
  279. }
  280. }