JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/resolution/declarations/AssociableToAST.java
1/*
2 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2020 The JavaParser Team.
4 *
5 * This file is part of JavaParser.
6 *
7 * JavaParser can be used either under the terms of
8 * a) the GNU Lesser General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 * b) the terms of the Apache License
12 *
13 * You should have received a copy of both licenses in LICENCE.LGPL and
14 * LICENCE.APACHE. Please refer to those files for details.
15 *
16 * JavaParser is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 */
21
22package com.github.javaparser.resolution.declarations;
23
24import com.github.javaparser.ast.Node;
25
26import java.util.Optional;
27
28/**
29 * A declaration that can be potentially associated with an AST node.
30 * @param <N> type of AST Node that can be associated
31 */
32public interface AssociableToAST<N extends Node> {
33
34    /**
35     * If the declaration is associated to an AST node return it, otherwise it return empty.
36     * Declaration based on source code have an AST node associated while others don't. Example
37     * of other declarations are declarations coming from reflection or JARs.
38     *
39     * You may wonder how this method is different from the various getWrappedNode.
40     * The difference is that toAst is present in all Resolved* declarations (such as
41     * ResolvedAnnotationDeclaration), while getWrappedNode is present
42     * only on the subclasses of the Resolved* declarations that derive from JP AST nodes (such as
43     * JavaParserClassDeclaration). Therefore one
44     * which has a Resolved* declaration need to do a downcast before being able to use getWrappedNode.
45     *
46     * Now, this means that toAst could potentially replace getWrappedNode (but not the other way around!).
47     * However toAst return an Optional, which is less convenient than getting the direct node. Also,
48     * toAst sometimes have to return a more generic node. This is the case for subclasses of
49     * ResolvedClassDeclaration. In those cases toAst return a Node. Why? Because both anonymous
50     * class declarations and standard class declarations are subclasses of that. In one case the
51     * underlying AST node is an ObjectCreationExpr, while in the other case it is ClassOrInterfaceDeclaration.
52     * In these cases getWrappedNode is particularly nice because it returns the right type of AST node,
53     * not just a Node.
54     */
55    default Optional<NtoAst() {
56        throw new UnsupportedOperationException();
57    }
58}
59
MembersX
AssociableToAST:toAst
Members
X