Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DeclareAnnotationDeclaration.java 4.3KB

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