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

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