/cpp/src/main/java/com/google/test/metric/cpp/dom/VariableDeclarationFinder.java

http://testability-explorer.googlecode.com/ · Java · 58 lines · 34 code · 8 blank · 16 comment · 4 complexity · eed864625966eb99e0b9310da254dae7 MD5 · raw file

  1. /*
  2. * Copyright 2008 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. // Copyright 2009 Google Inc. All Rights Reserved.
  17. package com.google.test.metric.cpp.dom;
  18. import java.util.Stack;
  19. class VariableDeclarationFinder extends Visitor {
  20. private final Variable variable;
  21. private final Stack<Node> parents = new Stack<Node>();
  22. private VariableDeclaration result;
  23. public VariableDeclarationFinder(Variable v, Node context) {
  24. this.variable = v;
  25. parents.push(context.getParent());
  26. }
  27. public VariableDeclaration getResult() {
  28. return result;
  29. }
  30. @Override
  31. public void visit(VariableDeclaration localVariableDeclaration) {
  32. Variable var = new Variable(localVariableDeclaration.getName(),
  33. localVariableDeclaration);
  34. Node parent = parents.peek();
  35. if (localVariableDeclaration.getParent() == parent &&
  36. var.equals(variable)) {
  37. result = localVariableDeclaration;
  38. }
  39. }
  40. @Override
  41. public void beginVisit(Namespace namespace) {
  42. parents.push(namespace);
  43. }
  44. @Override
  45. public void endVisit(Namespace namespace) {
  46. if (parents.peek() == namespace) {
  47. parents.pop();
  48. }
  49. }
  50. }