Home | History | Annotate | Download | only in contexts
      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.javaparsermodel.contexts;
     18 
     19 import com.github.javaparser.ast.body.EnumConstantDeclaration;
     20 import com.github.javaparser.ast.body.EnumDeclaration;
     21 import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
     22 import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
     23 import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
     24 import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
     25 import com.github.javaparser.resolution.types.ResolvedType;
     26 import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumConstantDeclaration;
     27 import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumDeclaration;
     28 import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
     29 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
     30 
     31 import java.util.List;
     32 
     33 /**
     34  * @author Federico Tomassetti
     35  */
     36 public class EnumDeclarationContext extends AbstractJavaParserContext<EnumDeclaration> {
     37 
     38     private JavaParserTypeDeclarationAdapter javaParserTypeDeclarationAdapter;
     39 
     40     public EnumDeclarationContext(EnumDeclaration wrappedNode, TypeSolver typeSolver) {
     41         super(wrappedNode, typeSolver);
     42         this.javaParserTypeDeclarationAdapter = new JavaParserTypeDeclarationAdapter(wrappedNode, typeSolver,
     43                 getDeclaration(), this);
     44     }
     45 
     46     @Override
     47     public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
     48         if (typeSolver == null) throw new IllegalArgumentException();
     49 
     50         // among constants
     51         for (EnumConstantDeclaration constant : wrappedNode.getEntries()) {
     52             if (constant.getName().getId().equals(name)) {
     53                 return SymbolReference.solved(new JavaParserEnumConstantDeclaration(constant, typeSolver));
     54             }
     55         }
     56 
     57         if (this.getDeclaration().hasField(name)) {
     58             return SymbolReference.solved(this.getDeclaration().getField(name));
     59         }
     60 
     61         // then to parent
     62         return getParent().solveSymbol(name, typeSolver);
     63     }
     64 
     65     @Override
     66     public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
     67         return javaParserTypeDeclarationAdapter.solveType(name, typeSolver);
     68     }
     69 
     70     @Override
     71     public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly, TypeSolver typeSolver) {
     72         return javaParserTypeDeclarationAdapter.solveMethod(name, argumentsTypes, staticOnly, typeSolver);
     73     }
     74 
     75     ///
     76     /// Private methods
     77     ///
     78 
     79     private ResolvedReferenceTypeDeclaration getDeclaration() {
     80         return new JavaParserEnumDeclaration(this.wrappedNode, typeSolver);
     81     }
     82 }
     83