• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.declarations.ResolvedDeclaration;
20 
21 import java.util.Optional;
22 
23 /**
24  * A reference to a symbol. It can solved or not solved. If solved the corresponding
25  * declaration will be provided.
26  *
27  * @author Federico Tomassetti
28  */
29 public class SymbolReference<S extends ResolvedDeclaration> {
30 
31     private Optional<? extends S> correspondingDeclaration;
32 
SymbolReference(Optional<? extends S> correspondingDeclaration)33     private SymbolReference(Optional<? extends S> correspondingDeclaration) {
34         this.correspondingDeclaration = correspondingDeclaration;
35     }
36 
37     /**
38      * Create a solve reference to the given symbol.
39      */
solved(S2 symbolDeclaration)40     public static <S extends ResolvedDeclaration, S2 extends S> SymbolReference<S> solved(S2 symbolDeclaration) {
41         return new SymbolReference<S>(Optional.of(symbolDeclaration));
42     }
43 
44     /**
45      * Create an unsolved reference specifying the type of the value expected.
46      */
unsolved(Class<S2> clazz)47     public static <S extends ResolvedDeclaration, S2 extends S> SymbolReference<S> unsolved(Class<S2> clazz) {
48         return new SymbolReference<>(Optional.empty());
49     }
50 
51     @Override
toString()52     public String toString() {
53         return "SymbolReference{" + correspondingDeclaration + "}";
54     }
55 
56     /**
57      * The corresponding declaration. If not solve this throws UnsupportedOperationException.
58      */
getCorrespondingDeclaration()59     public S getCorrespondingDeclaration() {
60         if (!isSolved()) {
61             throw new UnsupportedOperationException("CorrespondingDeclaration not available for unsolved symbol.");
62         }
63         return correspondingDeclaration.get();
64     }
65 
66     /**
67      * Is the reference solved?
68      */
isSolved()69     public boolean isSolved() {
70         return correspondingDeclaration.isPresent();
71     }
72 
adapt(SymbolReference<? extends O> ref, Class<O> clazz)73     public static <O extends ResolvedDeclaration> SymbolReference<O> adapt(SymbolReference<? extends O> ref, Class<O> clazz) {
74         if (ref.isSolved()) {
75             return SymbolReference.solved(ref.getCorrespondingDeclaration());
76         } else {
77             return SymbolReference.unsolved(clazz);
78         }
79     }
80 }
81