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

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