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.resolution.declarations.ResolvedClassDeclaration; 21 import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration; 22 import com.github.javaparser.resolution.declarations.ResolvedParameterDeclaration; 23 import com.github.javaparser.resolution.declarations.ResolvedTypeParameterDeclaration; 24 import com.github.javaparser.resolution.types.ResolvedType; 25 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; 26 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; 27 28 import java.util.List; 29 import java.util.stream.Collectors; 30 31 /** 32 * @author Federico Tomassetti 33 */ 34 public class JavaParserConstructorDeclaration implements ResolvedConstructorDeclaration { 35 36 private ResolvedClassDeclaration classDeclaration; 37 private com.github.javaparser.ast.body.ConstructorDeclaration wrappedNode; 38 private TypeSolver typeSolver; 39 40 JavaParserConstructorDeclaration(ResolvedClassDeclaration classDeclaration, com.github.javaparser.ast.body.ConstructorDeclaration wrappedNode, 41 TypeSolver typeSolver) { 42 this.classDeclaration = classDeclaration; 43 this.wrappedNode = wrappedNode; 44 this.typeSolver = typeSolver; 45 } 46 47 @Override 48 public ResolvedClassDeclaration declaringType() { 49 return classDeclaration; 50 } 51 52 @Override 53 public int getNumberOfParams() { 54 return this.wrappedNode.getParameters().size(); 55 } 56 57 @Override 58 public ResolvedParameterDeclaration getParam(int i) { 59 if (i < 0 || i >= getNumberOfParams()) { 60 throw new IllegalArgumentException(String.format("No param with index %d. Number of params: %d", i, getNumberOfParams())); 61 } 62 return new JavaParserParameterDeclaration(wrappedNode.getParameters().get(i), typeSolver); 63 } 64 65 @Override 66 public String getName() { 67 return this.classDeclaration.getName(); 68 } 69 70 /** 71 * Returns the JavaParser node associated with this JavaParserConstructorDeclaration. 72 * 73 * @return A visitable JavaParser node wrapped by this object. 74 */ 75 public com.github.javaparser.ast.body.ConstructorDeclaration getWrappedNode() { 76 return wrappedNode; 77 } 78 79 @Override 80 public AccessSpecifier accessSpecifier() { 81 return Helper.toAccessLevel(wrappedNode.getModifiers()); 82 } 83 84 @Override 85 public List<ResolvedTypeParameterDeclaration> getTypeParameters() { 86 return this.wrappedNode.getTypeParameters().stream().map((astTp) -> new JavaParserTypeParameter(astTp, typeSolver)).collect(Collectors.toList()); 87 } 88 89 @Override 90 public int getNumberOfSpecifiedExceptions() { 91 return wrappedNode.getThrownExceptions().size(); 92 } 93 94 @Override 95 public ResolvedType getSpecifiedException(int index) { 96 if (index < 0 || index >= getNumberOfSpecifiedExceptions()) { 97 throw new IllegalArgumentException(String.format("No exception with index %d. Number of exceptions: %d", 98 index, getNumberOfSpecifiedExceptions())); 99 } 100 return JavaParserFacade.get(typeSolver) 101 .convert(wrappedNode.getThrownExceptions().get(index), wrappedNode); 102 } 103 } 104