/cpp/src/main/java/com/google/test/metric/cpp/dom/VariableDeclarationFinder.java
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 18package com.google.test.metric.cpp.dom; 19 20import java.util.Stack; 21 22class VariableDeclarationFinder extends Visitor { 23 private final Variable variable; 24 private final Stack<Node> parents = new Stack<Node>(); 25 private VariableDeclaration result; 26 27 public VariableDeclarationFinder(Variable v, Node context) { 28 this.variable = v; 29 parents.push(context.getParent()); 30 } 31 32 public VariableDeclaration getResult() { 33 return result; 34 } 35 36 @Override 37 public void visit(VariableDeclaration localVariableDeclaration) { 38 Variable var = new Variable(localVariableDeclaration.getName(), 39 localVariableDeclaration); 40 Node parent = parents.peek(); 41 if (localVariableDeclaration.getParent() == parent && 42 var.equals(variable)) { 43 result = localVariableDeclaration; 44 } 45 } 46 47 @Override 48 public void beginVisit(Namespace namespace) { 49 parents.push(namespace); 50 } 51 52 @Override 53 public void endVisit(Namespace namespace) { 54 if (parents.peek() == namespace) { 55 parents.pop(); 56 } 57 } 58}