1 package com.github.javaparser.resolution.declarations; 2 3 import com.github.javaparser.ast.Node; 4 5 import java.util.Optional; 6 7 /** 8 * A declaration that can be potentially associated with an AST node. 9 * @param <N> type of AST Node that can be associated 10 */ 11 public interface AssociableToAST<N extends Node> { 12 13 /** 14 * If the declaration is associated to an AST node return it, otherwise it return empty. 15 * Declaration based on source code have an AST node associated while others don't. Example 16 * of other declarations are declarations coming from reflection or JARs. 17 * 18 * You may wonder how this method is different from the various getWrappedNode. 19 * The difference is that toAst is present in all Resolved* declarations (such as 20 * ResolvedAnnotationDeclaration), while getWrappedNode is present 21 * only on the subclasses of the Resolved* declarations that derive from JP AST nodes (such as 22 * JavaParserClassDeclaration). Therefore one 23 * which has a Resolved* declaration need to do a downcast before being able to use getWrappedNode. 24 * 25 * Now, this means that toAst could potentially replace getWrappedNode (but not the other way around!). 26 * However toAst return an Optional, which is less convenient than getting the direct node. Also, 27 * toAst sometimes have to return a more generic node. This is the case for subclasses of 28 * ResolvedClassDeclaration. In those cases toAst return a Node. Why? Because both anonymous 29 * class declarations and standard class declarations are subclasses of that. In one case the 30 * underlying AST node is an ObjectCreationExpr, while in the other case it is ClassOrInterfaceDeclaration. 31 * In these cases getWrappedNode is particularly nice because it returns the right type of AST node, 32 * not just a Node. 33 */ toAst()34 default Optional<N> toAst() { 35 throw new UnsupportedOperationException(); 36 } 37 } 38