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.

IfPseudoToken.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 Common Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/cpl-v10.html
  8. *
  9. * Contributors:
  10. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.internal.compiler.ast;
  13. import java.lang.reflect.Modifier;
  14. import org.aspectj.ajdt.internal.compiler.lookup.EclipseWorld;
  15. import org.aspectj.weaver.*;
  16. import org.aspectj.weaver.ResolvedMember;
  17. import org.aspectj.weaver.patterns.*;
  18. import org.eclipse.jdt.internal.compiler.CompilationResult;
  19. import org.eclipse.jdt.internal.compiler.ast.*;
  20. import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
  21. import org.eclipse.jdt.internal.compiler.parser.*;
  22. /**
  23. * (formals*): ... if(expr) ...
  24. *
  25. * generates the following:
  26. * public static final boolean ajc$if_N(formals*, [thisJoinPoints as needed]) {
  27. * return expr;
  28. * }
  29. *
  30. * Here's the complicated bit, it deals with cflow:
  31. * (a): ... this(a) && cflow(if (a == foo)) is an error.
  32. * The way we capture this is:
  33. * We generate the ajc$if method with an (a) parameter, we let eclipse do the proper
  34. * name binding. We then, as a post pass (that we need to do anyway) look for the
  35. * used parameters. If a is used, we signal an error because a was not one of the
  36. * cflow variables.
  37. * XXX we'll do this part after we do cflow
  38. *
  39. * The IfPointcut pcd then generates itself always as a dynamic test, it has to
  40. * get the right parameters through any named pointcut references...
  41. */
  42. public class IfPseudoToken extends PseudoToken {
  43. public Expression expr;
  44. public MethodDeclaration testMethod;
  45. private IfPointcut pointcut;
  46. public IfPseudoToken(
  47. Parser parser,
  48. Expression expr) {
  49. super(parser, "if", false);
  50. this.expr = expr;
  51. }
  52. public Pointcut maybeGetParsedPointcut() {
  53. pointcut = new IfPointcut(new ResolvedMember(Member.METHOD, TypeX.OBJECT, 0, "if_", "()V"), 0);
  54. return pointcut;
  55. }
  56. /**
  57. * enclosingDec is either AdviceDeclaration or PointcutDeclaration
  58. */
  59. public void postParse(TypeDeclaration typeDec, MethodDeclaration enclosingDec) {
  60. // typeDec.scope.problemReporter().signalError(sourceStart, sourceEnd,
  61. // "if pcd is not implemented in 1.1alpha1");
  62. //XXX need to implement correctly
  63. if (pointcut == null) return;
  64. testMethod = makeMethod(enclosingDec.compilationResult, enclosingDec);
  65. AstUtil.addMethodDeclaration(typeDec, testMethod);
  66. }
  67. //XXX static state bad
  68. private static int counter = 0;
  69. //XXX todo: make sure that errors in Arguments only get displayed once
  70. private MethodDeclaration makeMethod(CompilationResult result, MethodDeclaration enclosingDec) {
  71. MethodDeclaration ret = new IfMethodDeclaration(result, pointcut);
  72. ret.modifiers = AccStatic | AccFinal | AccPublic;
  73. ret.returnType = AstUtil.makeTypeReference(TypeBinding.BooleanBinding);
  74. ret.selector = ("ajc$if_" + counter++).toCharArray();
  75. ret.arguments = makeArguments(enclosingDec);
  76. ret.statements = new Statement[] {
  77. new ReturnStatement(expr, expr.sourceStart, expr.sourceEnd)
  78. };
  79. return ret;
  80. }
  81. private Argument[] makeArguments(MethodDeclaration enclosingDec) {
  82. Argument[] baseArguments = enclosingDec.arguments;
  83. int len = baseArguments.length;
  84. if (enclosingDec instanceof AdviceDeclaration) {
  85. len = ((AdviceDeclaration)enclosingDec).baseArgumentCount;
  86. }
  87. Argument[] ret = new Argument[len];
  88. for (int i=0; i < len; i ++) {
  89. Argument a = baseArguments[i];
  90. ret[i] = new Argument(a.name, AstUtil.makeLongPos(a.sourceStart, a.sourceEnd),
  91. a.type, Modifier.FINAL);
  92. }
  93. ret = AdviceDeclaration.addTjpArguments(ret);
  94. return ret;
  95. }
  96. }