| 1 | /******************************************************************************* |
|---|---|
| 2 | * Copyright (c) 2000, 2009 IBM Corporation and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * IBM Corporation - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | package org.eclipse.jdt.core.dom; |
| 15 | |
| 16 | import org.eclipse.jdt.internal.compiler.ASTVisitor; |
| 17 | import org.eclipse.jdt.internal.compiler.ast.ConstructorDeclaration; |
| 18 | import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; |
| 19 | import org.eclipse.jdt.internal.compiler.ast.Initializer; |
| 20 | import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; |
| 21 | import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; |
| 22 | import org.eclipse.jdt.internal.compiler.lookup.ClassScope; |
| 23 | import org.eclipse.jdt.internal.compiler.lookup.CompilationUnitScope; |
| 24 | import org.eclipse.jdt.internal.compiler.lookup.MethodScope; |
| 25 | |
| 26 | class NodeSearcher extends ASTVisitor { |
| 27 | public org.eclipse.jdt.internal.compiler.ast.ASTNode found; |
| 28 | public TypeDeclaration enclosingType; |
| 29 | public int position; |
| 30 | |
| 31 | NodeSearcher(int position) { |
| 32 | this.position = position; |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public boolean visit( |
| 37 | ConstructorDeclaration constructorDeclaration, |
| 38 | ClassScope scope) { |
| 39 | |
| 40 | if (constructorDeclaration.declarationSourceStart <= this.position |
| 41 | && this.position <= constructorDeclaration.declarationSourceEnd) { |
| 42 | this.found = constructorDeclaration; |
| 43 | return false; |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public boolean visit( |
| 50 | FieldDeclaration fieldDeclaration, |
| 51 | MethodScope scope) { |
| 52 | if (fieldDeclaration.declarationSourceStart <= this.position |
| 53 | && this.position <= fieldDeclaration.declarationSourceEnd) { |
| 54 | this.found = fieldDeclaration; |
| 55 | return false; |
| 56 | } |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public boolean visit(Initializer initializer, MethodScope scope) { |
| 62 | if (initializer.declarationSourceStart <= this.position |
| 63 | && this.position <= initializer.declarationSourceEnd) { |
| 64 | this.found = initializer; |
| 65 | return false; |
| 66 | } |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean visit( |
| 72 | TypeDeclaration memberTypeDeclaration, |
| 73 | ClassScope scope) { |
| 74 | if (memberTypeDeclaration.declarationSourceStart <= this.position |
| 75 | && this.position <= memberTypeDeclaration.declarationSourceEnd) { |
| 76 | this.enclosingType = memberTypeDeclaration; |
| 77 | return true; |
| 78 | |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean visit( |
| 85 | MethodDeclaration methodDeclaration, |
| 86 | ClassScope scope) { |
| 87 | |
| 88 | if (methodDeclaration.declarationSourceStart <= this.position |
| 89 | && this.position <= methodDeclaration.declarationSourceEnd) { |
| 90 | this.found = methodDeclaration; |
| 91 | return false; |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean visit( |
| 98 | TypeDeclaration typeDeclaration, |
| 99 | CompilationUnitScope scope) { |
| 100 | if (typeDeclaration.declarationSourceStart <= this.position |
| 101 | && this.position <= typeDeclaration.declarationSourceEnd) { |
| 102 | this.enclosingType = typeDeclaration; |
| 103 | return true; |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | } |
| 109 |
Members