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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  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.FlowInfo;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.flow.InitializationFlowContext;
  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. public void analyseCode(ClassScope classScope, InitializationFlowContext initializationContext, FlowInfo flowInfo) {
  38. super.analyseCode(classScope, initializationContext, flowInfo);
  39. if (isRemover) {
  40. if (((DeclareAnnotation) declareDecl).getKind() != DeclareAnnotation.AT_FIELD) {
  41. classScope.problemReporter().signalError(this.sourceStart(), this.sourceEnd,
  42. "Annotation removal only supported for declare @field (compiler limitation)");
  43. }
  44. else if (isRemover && !(annotation instanceof MarkerAnnotation)) {
  45. classScope.problemReporter().signalError(this.sourceStart(), this.sourceEnd,
  46. "Annotation removal does not allow values to be specified for the annotation (compiler limitation)");
  47. }
  48. }
  49. long bits = annotation.resolvedType.getAnnotationTagBits();
  50. if ((bits & TagBits.AnnotationTarget) != 0) {
  51. // The annotation is stored against a method. For declare @type we need to
  52. // confirm the annotation targets the right types. Earlier checking will
  53. // have not found this problem because an annotation for target METHOD will
  54. // not be reported on as we *do* store it against a method in this case
  55. DeclareAnnotation.Kind k = ((DeclareAnnotation) declareDecl).getKind();
  56. if (k.equals(DeclareAnnotation.AT_TYPE)) {
  57. if ((bits & TagBits.AnnotationForMethod) != 0) {
  58. classScope.problemReporter().disallowedTargetForAnnotation(annotation);
  59. }
  60. }
  61. if (k.equals(DeclareAnnotation.AT_FIELD)) {
  62. if ((bits & TagBits.AnnotationForMethod) != 0) {
  63. classScope.problemReporter().disallowedTargetForAnnotation(annotation);
  64. }
  65. }
  66. }
  67. }
  68. public Annotation getDeclaredAnnotation() {
  69. return annotation;
  70. }
  71. protected boolean shouldDelegateCodeGeneration() {
  72. return true; // declare annotation needs a method to be written out.
  73. }
  74. protected boolean shouldBeSynthetic() {
  75. return false;
  76. }
  77. private void addAnnotation(Annotation ann) {
  78. if (this.annotations == null) {
  79. this.annotations = new Annotation[1];
  80. }
  81. else {
  82. Annotation[] old = this.annotations;
  83. this.annotations = new Annotation[old.length + 1];
  84. System.arraycopy(old, 0, this.annotations, 1, old.length);
  85. }
  86. this.annotations[0] = ann;
  87. }
  88. public void postParse(TypeDeclaration typeDec) {
  89. super.postParse(typeDec);
  90. if (declareDecl != null) {
  91. ((DeclareAnnotation) declareDecl).setAnnotationMethod(new String(selector));
  92. }
  93. }
  94. public boolean isRemover() {
  95. return isRemover;
  96. }
  97. }