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.Node;
     21 import com.github.javaparser.ast.expr.ObjectCreationExpr;
     22 import com.github.javaparser.resolution.MethodUsage;
     23 import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
     24 import com.github.javaparser.resolution.declarations.ResolvedParameterDeclaration;
     25 import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
     26 import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration;
     27 import com.github.javaparser.resolution.types.ResolvedType;
     28 import com.github.javaparser.symbolsolver.core.resolution.Context;
     29 import com.github.javaparser.symbolsolver.declarations.common.MethodDeclarationCommonLogic;
     30 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
     31 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory;
     32 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
     33 
     34 import java.util.List;
     35 import java.util.stream.Collectors;
     36 
     37 import static com.github.javaparser.symbolsolver.javaparser.Navigator.requireParentNode;
     38 
     39 /**
     40  * @author Federico Tomassetti
     41  */
     42 public class JavaParserMethodDeclaration implements ResolvedMethodDeclaration {
     43 
     44     private com.github.javaparser.ast.body.MethodDeclaration wrappedNode;
     45     private TypeSolver typeSolver;
     46 
     47     public JavaParserMethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration wrappedNode, TypeSolver typeSolver) {
     48         this.wrappedNode = wrappedNode;
     49         this.typeSolver = typeSolver;
     50     }
     51 
     52     @Override
     53     public String toString() {
     54         return "JavaParserMethodDeclaration{" +
     55                 "wrappedNode=" + wrappedNode +
     56                 ", typeSolver=" + typeSolver +
     57                 '}';
     58     }
     59 
     60     @Override
     61     public ResolvedReferenceTypeDeclaration declaringType() {
     62         if (requireParentNode(wrappedNode) instanceof ObjectCreationExpr) {
     63             ObjectCreationExpr parentNode = (ObjectCreationExpr) requireParentNode(wrappedNode);
     64             return new JavaParserAnonymousClassDeclaration(parentNode, typeSolver);
     65         }
     66         return JavaParserFactory.toTypeDeclaration(requireParentNode(wrappedNode), typeSolver);
     67     }
     68 
     69     @Override
     70     public ResolvedType getReturnType() {
     71         return JavaParserFacade.get(typeSolver).convert(wrappedNode.getType(), getContext());
     72     }
     73 
     74     @Override
     75     public int getNumberOfParams() {
     76         return wrappedNode.getParameters().size();
     77     }
     78 
     79     @Override
     80     public ResolvedParameterDeclaration getParam(int i) {
     81         if (i < 0 || i >= getNumberOfParams()) {
     82             throw new IllegalArgumentException(String.format("No param with index %d. Number of params: %d", i, getNumberOfParams()));
     83         }
     84         return new JavaParserParameterDeclaration(wrappedNode.getParameters().get(i), typeSolver);
     85     }
     86 
     87     public MethodUsage getUsage(Node node) {
     88         throw new UnsupportedOperationException();
     89     }
     90 
     91     public MethodUsage resolveTypeVariables(Context context, List<ResolvedType> parameterTypes) {
     92         return new MethodDeclarationCommonLogic(this, typeSolver).resolveTypeVariables(context, parameterTypes);
     93     }
     94 
     95     private Context getContext() {
     96         return JavaParserFactory.getContext(wrappedNode, typeSolver);
     97     }
     98 
     99     @Override
    100     public boolean isAbstract() {
    101         return !wrappedNode.getBody().isPresent();
    102     }
    103 
    104     @Override
    105     public String getName() {
    106         return wrappedNode.getName().getId();
    107     }
    108 
    109     @Override
    110     public boolean isField() {
    111         throw new UnsupportedOperationException();
    112     }
    113 
    114     @Override
    115     public boolean isParameter() {
    116         throw new UnsupportedOperationException();
    117     }
    118 
    119     @Override
    120     public boolean isType() {
    121         throw new UnsupportedOperationException();
    122     }
    123 
    124     @Override
    125     public List<ResolvedTypeParameterDeclaration> getTypeParameters() {
    126         return this.wrappedNode.getTypeParameters().stream().map((astTp) -> new JavaParserTypeParameter(astTp, typeSolver)).collect(Collectors.toList());
    127     }
    128 
    129     @Override
    130     public boolean isDefaultMethod() {
    131         return wrappedNode.isDefault();
    132     }
    133 
    134     @Override
    135     public boolean isStatic() {
    136         return wrappedNode.isStatic();
    137     }
    138 
    139     /**
    140      * Returns the JavaParser node associated with this JavaParserMethodDeclaration.
    141      *
    142      * @return A visitable JavaParser node wrapped by this object.
    143      */
    144     public com.github.javaparser.ast.body.MethodDeclaration getWrappedNode() {
    145         return wrappedNode;
    146     }
    147 
    148     @Override
    149     public AccessSpecifier accessSpecifier() {
    150         return Helper.toAccessLevel(wrappedNode.getModifiers());
    151     }
    152 
    153     @Override
    154     public int getNumberOfSpecifiedExceptions() {
    155         return wrappedNode.getThrownExceptions().size();
    156     }
    157 
    158     @Override
    159     public ResolvedType getSpecifiedException(int index) {
    160         if (index < 0 || index >= getNumberOfSpecifiedExceptions()) {
    161             throw new IllegalArgumentException(String.format("No exception with index %d. Number of exceptions: %d",
    162                     index, getNumberOfSpecifiedExceptions()));
    163         }
    164         return JavaParserFacade.get(typeSolver).convert(wrappedNode.getThrownExceptions()
    165                 .get(index), wrappedNode);
    166     }
    167 }
    168