1 /*
2  * Copyright 2016 Federico Tomassetti
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.github.javaparser.symbolsolver.model.resolution;
18 
19 import com.github.javaparser.resolution.UnsolvedSymbolException;
20 import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
21 import com.github.javaparser.resolution.types.ResolvedType;
22 
23 /**
24  * An element able to find TypeDeclaration from their name.
25  * TypeSolvers are organized in hierarchies.
26  *
27  * @author Federico Tomassetti
28  */
29 public interface TypeSolver {
30 
31     /**
32      * Get the root of the hierarchy of type solver.
33      */
getRoot()34     default TypeSolver getRoot() {
35         if (getParent() == null) {
36             return this;
37         } else {
38             return getParent().getRoot();
39         }
40     }
41 
42     /**
43      * Parent of the this TypeSolver. This can return null.
44      */
getParent()45     TypeSolver getParent();
46 
47     /**
48      * Set the parent of this TypeSolver.
49      */
setParent(TypeSolver parent)50     void setParent(TypeSolver parent);
51 
52     /**
53      * Try to solve the type with the given name. It always return a SymbolReference which can be solved
54      * or unsolved.
55      */
tryToSolveType(String name)56     SymbolReference<ResolvedReferenceTypeDeclaration> tryToSolveType(String name);
57 
58     /**
59      * Solve the given type. Either the type is found and returned or an UnsolvedSymbolException is thrown.
60      */
solveType(String name)61     default ResolvedReferenceTypeDeclaration solveType(String name) throws UnsolvedSymbolException {
62         SymbolReference<ResolvedReferenceTypeDeclaration> ref = tryToSolveType(name);
63         if (ref.isSolved()) {
64             return ref.getCorrespondingDeclaration();
65         } else {
66             throw new UnsolvedSymbolException(name, this.toString());
67         }
68     }
69 
70 }
71