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.body.Parameter; 20 import com.github.javaparser.ast.type.UnknownType; 21 import com.github.javaparser.resolution.declarations.ResolvedParameterDeclaration; 22 import com.github.javaparser.resolution.types.ResolvedArrayType; 23 import com.github.javaparser.resolution.types.ResolvedType; 24 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; 25 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory; 26 import com.github.javaparser.symbolsolver.javaparsermodel.contexts.LambdaExprContext; 27 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; 28 import com.github.javaparser.symbolsolver.model.resolution.Value; 29 30 import java.util.Optional; 31 32 /** 33 * @author Federico Tomassetti 34 */ 35 public class JavaParserParameterDeclaration implements ResolvedParameterDeclaration { 36 37 private Parameter wrappedNode; 38 private TypeSolver typeSolver; 39 40 public JavaParserParameterDeclaration(Parameter wrappedNode, TypeSolver typeSolver) { 41 this.wrappedNode = wrappedNode; 42 this.typeSolver = typeSolver; 43 } 44 45 @Override 46 public String getName() { 47 return wrappedNode.getName().getId(); 48 } 49 50 @Override 51 public boolean isField() { 52 return false; 53 } 54 55 @Override 56 public boolean isParameter() { 57 return true; 58 } 59 60 @Override 61 public boolean isVariadic() { 62 return wrappedNode.isVarArgs(); 63 } 64 65 @Override 66 public boolean isType() { 67 throw new UnsupportedOperationException(); 68 } 69 70 @Override 71 public ResolvedType getType() { 72 if (wrappedNode.getType() instanceof UnknownType && JavaParserFactory.getContext(wrappedNode, typeSolver) instanceof LambdaExprContext) { 73 Optional<Value> value = JavaParserFactory.getContext(wrappedNode, typeSolver).solveSymbolAsValue(wrappedNode.getNameAsString(), typeSolver); 74 if (value.isPresent()) { 75 return value.get().getType(); 76 } 77 } 78 ResolvedType res = JavaParserFacade.get(typeSolver).convert(wrappedNode.getType(), wrappedNode); 79 if (isVariadic()) { 80 res = new ResolvedArrayType(res); 81 } 82 return res; 83 } 84 85 @Override 86 public ResolvedParameterDeclaration asParameter() { 87 return this; 88 } 89 90 /** 91 * Returns the JavaParser node associated with this JavaParserParameterDeclaration. 92 * 93 * @return A visitable JavaParser node wrapped by this object. 94 */ 95 public Parameter getWrappedNode() { 96 return wrappedNode; 97 } 98 99 100 } 101