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.

DeclareTypeErrorOrWarning.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* *******************************************************************
  2. * Copyright (c) 2010 Contributors
  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. * Andy Clement initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.patterns;
  13. import java.io.IOException;
  14. import java.util.Map;
  15. import org.aspectj.weaver.CompressingDataOutputStream;
  16. import org.aspectj.weaver.ISourceContext;
  17. import org.aspectj.weaver.VersionedDataInputStream;
  18. import org.aspectj.weaver.World;
  19. /**
  20. * For a declare error/warning that specified a type pattern rather than a pointcut.
  21. *
  22. * @author Andy Clement
  23. * @since 1.6.9
  24. */
  25. public class DeclareTypeErrorOrWarning extends Declare {
  26. private boolean isError;
  27. private TypePattern typePattern;
  28. private String message;
  29. public DeclareTypeErrorOrWarning(boolean isError, TypePattern typePattern, String message) {
  30. this.isError = isError;
  31. this.typePattern = typePattern;
  32. this.message = message;
  33. }
  34. /**
  35. * returns "declare warning: <typepattern>: <message>" or "declare error: <typepattern>: <message>"
  36. */
  37. public String toString() {
  38. StringBuilder buf = new StringBuilder();
  39. buf.append("declare ");
  40. if (isError) {
  41. buf.append("error: ");
  42. } else {
  43. buf.append("warning: ");
  44. }
  45. buf.append(typePattern);
  46. buf.append(": ");
  47. buf.append("\"");
  48. buf.append(message);
  49. buf.append("\";");
  50. return buf.toString();
  51. }
  52. public boolean equals(Object other) {
  53. if (!(other instanceof DeclareTypeErrorOrWarning)) {
  54. return false;
  55. }
  56. DeclareTypeErrorOrWarning o = (DeclareTypeErrorOrWarning) other;
  57. return (o.isError == isError) && o.typePattern.equals(typePattern) && o.message.equals(message);
  58. }
  59. public int hashCode() {
  60. int result = isError ? 19 : 23;
  61. result = 37 * result + typePattern.hashCode();
  62. result = 37 * result + message.hashCode();
  63. return result;
  64. }
  65. public Object accept(PatternNodeVisitor visitor, Object data) {
  66. return visitor.visit(this, data);
  67. }
  68. public Object traverse(PatternNodeVisitor visitor, Object data) {
  69. Object ret = accept(visitor, data);
  70. if (this.typePattern != null)
  71. this.typePattern.traverse(visitor, ret);
  72. return ret;
  73. }
  74. public void write(CompressingDataOutputStream s) throws IOException {
  75. s.writeByte(Declare.TYPE_ERROR_OR_WARNING);
  76. s.writeBoolean(isError);
  77. typePattern.write(s);
  78. s.writeUTF(message);
  79. writeLocation(s);
  80. }
  81. public static Declare read(VersionedDataInputStream s, ISourceContext context) throws IOException {
  82. Declare ret = new DeclareTypeErrorOrWarning(s.readBoolean(), TypePattern.read(s, context), s.readUTF());
  83. ret.readLocation(context, s);
  84. return ret;
  85. }
  86. public boolean isError() {
  87. return isError;
  88. }
  89. public String getMessage() {
  90. return message;
  91. }
  92. public TypePattern getTypePattern() {
  93. return typePattern;
  94. }
  95. public void resolve(IScope scope) {
  96. typePattern.resolve(scope.getWorld());
  97. }
  98. public Declare parameterizeWith(Map typeVariableBindingMap, World w) {
  99. Declare ret = new DeclareTypeErrorOrWarning(isError, typePattern.parameterizeWith(typeVariableBindingMap, w), message);
  100. ret.copyLocationFrom(this);
  101. return ret;
  102. }
  103. public boolean isAdviceLike() {
  104. return false;
  105. }
  106. public String getNameSuffix() {
  107. return "teow";
  108. }
  109. /**
  110. * returns "declare type warning" or "declare type error"
  111. */
  112. public String getName() {
  113. StringBuilder buf = new StringBuilder();
  114. buf.append("declare type ");
  115. if (isError) {
  116. buf.append("error");
  117. } else {
  118. buf.append("warning");
  119. }
  120. return buf.toString();
  121. }
  122. }