Home | History | Annotate | Download | only in declarations
      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.declarations;
     18 
     19 import com.github.javaparser.ast.AccessSpecifier;
     20 import com.github.javaparser.ast.Modifier;
     21 import com.github.javaparser.ast.body.EnumConstantDeclaration;
     22 import com.github.javaparser.ast.body.VariableDeclarator;
     23 import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration;
     24 import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
     25 import com.github.javaparser.resolution.types.ResolvedType;
     26 import com.github.javaparser.symbolsolver.javaparser.Navigator;
     27 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
     28 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
     29 import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl;
     30 
     31 import java.util.Optional;
     32 
     33 import static com.github.javaparser.symbolsolver.javaparser.Navigator.getParentNode;
     34 import static com.github.javaparser.symbolsolver.javaparser.Navigator.requireParentNode;
     35 
     36 /**
     37  * @author Federico Tomassetti
     38  */
     39 public class JavaParserFieldDeclaration implements ResolvedFieldDeclaration {
     40 
     41     private VariableDeclarator variableDeclarator;
     42     private com.github.javaparser.ast.body.FieldDeclaration wrappedNode;
     43     private EnumConstantDeclaration enumConstantDeclaration;
     44     private TypeSolver typeSolver;
     45 
     46     public JavaParserFieldDeclaration(VariableDeclarator variableDeclarator, TypeSolver typeSolver) {
     47         if (typeSolver == null) {
     48             throw new IllegalArgumentException("typeSolver should not be null");
     49         }
     50         this.variableDeclarator = variableDeclarator;
     51         this.typeSolver = typeSolver;
     52         if (!(requireParentNode(variableDeclarator) instanceof com.github.javaparser.ast.body.FieldDeclaration)) {
     53             throw new IllegalStateException(requireParentNode(variableDeclarator).getClass().getCanonicalName());
     54         }
     55         this.wrappedNode = (com.github.javaparser.ast.body.FieldDeclaration) requireParentNode(variableDeclarator);
     56     }
     57 
     58     public JavaParserFieldDeclaration(EnumConstantDeclaration enumConstantDeclaration, TypeSolver typeSolver) {
     59         if (typeSolver == null) {
     60             throw new IllegalArgumentException("typeSolver should not be null");
     61         }
     62         this.enumConstantDeclaration = enumConstantDeclaration;
     63         this.typeSolver = typeSolver;
     64     }
     65 
     66     @Override
     67     public ResolvedType getType() {
     68         if (enumConstantDeclaration != null) {
     69             com.github.javaparser.ast.body.EnumDeclaration enumDeclaration = (com.github.javaparser.ast.body.EnumDeclaration) requireParentNode(enumConstantDeclaration);
     70             return new ReferenceTypeImpl(new JavaParserEnumDeclaration(enumDeclaration, typeSolver), typeSolver);
     71         }
     72         return JavaParserFacade.get(typeSolver).convert(variableDeclarator.getType(), wrappedNode);
     73     }
     74 
     75     @Override
     76     public String getName() {
     77         if (enumConstantDeclaration != null) {
     78             return enumConstantDeclaration.getName().getId();
     79         } else {
     80             return variableDeclarator.getName().getId();
     81         }
     82     }
     83 
     84     @Override
     85     public boolean isStatic() {
     86         return wrappedNode.getModifiers().contains(Modifier.STATIC);
     87     }
     88 
     89     @Override
     90     public boolean isField() {
     91         return true;
     92     }
     93 
     94     /**
     95      * Returns the JavaParser node associated with this JavaParserFieldDeclaration.
     96      *
     97      * @return A visitable JavaParser node wrapped by this object.
     98      */
     99     public com.github.javaparser.ast.body.FieldDeclaration getWrappedNode() {
    100         return wrappedNode;
    101     }
    102 
    103     @Override
    104     public String toString() {
    105         return "JPField{" + getName() + "}";
    106     }
    107 
    108     @Override
    109     public AccessSpecifier accessSpecifier() {
    110         return Helper.toAccessLevel(wrappedNode.getModifiers());
    111     }
    112 
    113     @Override
    114     public ResolvedTypeDeclaration declaringType() {
    115         Optional<com.github.javaparser.ast.body.TypeDeclaration> typeDeclaration = wrappedNode.findParent(com.github.javaparser.ast.body.TypeDeclaration.class);
    116         if (typeDeclaration.isPresent()) {
    117             return JavaParserFacade.get(typeSolver).getTypeDeclaration(typeDeclaration.get());
    118         }
    119         throw new IllegalStateException();
    120     }
    121 }
    122