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 11KB

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