您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

InterSuperFixerVisitor.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 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. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.internal.compiler.ast;
  13. import org.aspectj.ajdt.internal.compiler.lookup.EclipseFactory;
  14. import org.aspectj.ajdt.internal.compiler.lookup.EclipseSourceLocation;
  15. import org.aspectj.bridge.IMessage;
  16. import org.aspectj.bridge.ISourceLocation;
  17. import org.aspectj.org.eclipse.jdt.internal.compiler.ASTVisitor;
  18. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.Expression;
  19. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.FieldReference;
  20. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.MessageSend;
  21. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.SuperReference;
  22. import org.aspectj.org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
  23. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.BlockScope;
  24. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
  25. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.Scope;
  26. import org.aspectj.org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
  27. /**
  28. * Walks the body of inter-type declarations and replaces SuperReference with InterSuperReference
  29. *
  30. * @author Jim Hugunin
  31. */
  32. public class InterSuperFixerVisitor extends ASTVisitor {
  33. InterTypeDeclaration dec;
  34. ReferenceBinding onType;
  35. TypeBinding superType;
  36. private int depthCounter = 0; // Keeps track of whether we are inside any nested local type declarations
  37. EclipseFactory world;
  38. public InterSuperFixerVisitor(InterTypeDeclaration dec, EclipseFactory world, Scope scope) {
  39. this.dec = dec;
  40. this.onType = dec.onTypeBinding;
  41. this.world = world;
  42. // AMC with the java 5 compiler the superclass() of an interface is object,
  43. // not a parent interface (if one exists)
  44. if (onType.isInterface() && onType.superInterfaces().length == 1) {
  45. superType=onType.superInterfaces()[0];
  46. } else if (onType.superclass() != null) {
  47. superType = onType.superclass();
  48. } else if (onType.superInterfaces() == null || onType.superInterfaces().length == 0) {
  49. superType = scope.getJavaLangObject();
  50. } else if (onType.superInterfaces().length == 1) {
  51. superType = onType.superInterfaces()[0];
  52. } else {
  53. superType = null;
  54. }
  55. }
  56. public void endVisit(FieldReference ref, BlockScope scope) {
  57. ref.receiver = fixReceiver(ref.receiver, scope);
  58. }
  59. public void endVisit(MessageSend send, BlockScope scope) {
  60. send.receiver = fixReceiver(send.receiver, scope);
  61. }
  62. public boolean visit(TypeDeclaration localTypeDeclaration, BlockScope scope) {
  63. depthCounter++;
  64. return super.visit(localTypeDeclaration, scope);
  65. }
  66. public void endVisit(TypeDeclaration localTypeDeclaration,BlockScope scope) {
  67. depthCounter--;
  68. }
  69. private Expression fixReceiver(Expression expression, BlockScope scope) {
  70. if (depthCounter!=0) return expression; // Don't mess with super calls down in nested local type declarations (pr90143)
  71. if (expression instanceof SuperReference) {
  72. SuperReference superRef = (SuperReference) expression;
  73. if (superType == null) {
  74. ISourceLocation location =
  75. new EclipseSourceLocation(scope.problemReporter().referenceContext.compilationResult(),
  76. expression.sourceStart, expression.sourceEnd);
  77. world.showMessage(IMessage.ERROR, "multiple supertypes for this interface", location, null);
  78. dec.ignoreFurtherInvestigation = true;
  79. }
  80. //FIXME ??? note error
  81. expression = new InterSuperReference(superRef, superType);
  82. }
  83. return expression;
  84. }
  85. }