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.

DeclareAnnotationDeclaration.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* *******************************************************************
  2. * Copyright (c) 2005 IBM Corporation.
  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. * Adrian Colyer initial implementation
  11. * Andy Clement wired up to back end
  12. * ******************************************************************/
  13. package org.aspectj.ajdt.internal.compiler.ast;
  14. import org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult;
  15. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Annotation;
  16. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.MarkerAnnotation;
  17. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.flow.FlowContext;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.flow.FlowInfo;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ClassScope;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TagBits;
  22. import org.aspectj.weaver.patterns.DeclareAnnotation;
  23. public class DeclareAnnotationDeclaration extends DeclareDeclaration {
  24. private Annotation annotation;
  25. private boolean isRemover = false;
  26. public DeclareAnnotationDeclaration(CompilationResult result, DeclareAnnotation symbolicDeclare, Annotation annotation) {
  27. super(result, symbolicDeclare);
  28. this.annotation = annotation;
  29. addAnnotation(annotation);
  30. if (symbolicDeclare == null) {
  31. return; // there is an error that will already be getting reported (e.g. incorrect pattern on decaf/decac)
  32. }
  33. this.isRemover = symbolicDeclare.isRemover();
  34. symbolicDeclare.setAnnotationString(annotation.toString());
  35. symbolicDeclare.setAnnotationLocation(annotation.sourceStart, annotation.sourceEnd);
  36. }
  37. @Override
  38. public void analyseCode(ClassScope classScope, FlowContext flowContext, FlowInfo flowInfo) {
  39. super.analyseCode(classScope, flowContext, flowInfo);
  40. if (isRemover) {
  41. if (((DeclareAnnotation) declareDecl).getKind() != DeclareAnnotation.AT_FIELD) {
  42. classScope.problemReporter().signalError(this.sourceStart(), this.sourceEnd,
  43. "Annotation removal only supported for declare @field (compiler limitation)");
  44. }
  45. else if (isRemover && !(annotation instanceof MarkerAnnotation)) {
  46. classScope.problemReporter().signalError(this.sourceStart(), this.sourceEnd,
  47. "Annotation removal does not allow values to be specified for the annotation (compiler limitation)");
  48. }
  49. }
  50. long bits = annotation.resolvedType.getAnnotationTagBits();
  51. if ((bits & TagBits.AnnotationTarget) != 0) {
  52. // The annotation is stored against a method. For declare @type we need to
  53. // confirm the annotation targets the right types. Earlier checking will
  54. // have not found this problem because an annotation for target METHOD will
  55. // not be reported on as we *do* store it against a method in this case
  56. DeclareAnnotation.Kind k = ((DeclareAnnotation) declareDecl).getKind();
  57. if (k.equals(DeclareAnnotation.AT_TYPE)) {
  58. if ((bits & TagBits.AnnotationForMethod) != 0) {
  59. classScope.problemReporter().disallowedTargetForAnnotation(annotation);
  60. }
  61. }
  62. if (k.equals(DeclareAnnotation.AT_FIELD)) {
  63. if ((bits & TagBits.AnnotationForMethod) != 0) {
  64. classScope.problemReporter().disallowedTargetForAnnotation(annotation);
  65. }
  66. }
  67. }
  68. }
  69. public Annotation getDeclaredAnnotation() {
  70. return annotation;
  71. }
  72. protected boolean shouldDelegateCodeGeneration() {
  73. return true; // declare annotation needs a method to be written out.
  74. }
  75. protected boolean shouldBeSynthetic() {
  76. return false;
  77. }
  78. private void addAnnotation(Annotation ann) {
  79. if (this.annotations == null) {
  80. this.annotations = new Annotation[1];
  81. }
  82. else {
  83. Annotation[] old = this.annotations;
  84. this.annotations = new Annotation[old.length + 1];
  85. System.arraycopy(old, 0, this.annotations, 1, old.length);
  86. }
  87. this.annotations[0] = ann;
  88. }
  89. public void postParse(TypeDeclaration typeDec) {
  90. super.postParse(typeDec);
  91. if (declareDecl != null) {
  92. ((DeclareAnnotation) declareDecl).setAnnotationMethod(new String(selector));
  93. }
  94. }
  95. public boolean isRemover() {
  96. return isRemover;
  97. }
  98. }