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.reflectionmodel; 18 19 import com.github.javaparser.ast.AccessSpecifier; 20 import com.github.javaparser.resolution.declarations.ResolvedFieldDeclaration; 21 import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration; 22 import com.github.javaparser.resolution.types.ResolvedType; 23 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; 24 25 import java.lang.reflect.Field; 26 import java.lang.reflect.Modifier; 27 28 /** 29 * @author Federico Tomassetti 30 */ 31 public class ReflectionFieldDeclaration implements ResolvedFieldDeclaration { 32 33 private Field field; 34 private TypeSolver typeSolver; 35 private ResolvedType type; 36 37 public ReflectionFieldDeclaration(Field field, TypeSolver typeSolver) { 38 this.field = field; 39 this.typeSolver = typeSolver; 40 this.type = calcType(); 41 } 42 43 private ReflectionFieldDeclaration(Field field, TypeSolver typeSolver, ResolvedType type) { 44 this.field = field; 45 this.typeSolver = typeSolver; 46 this.type = type; 47 } 48 49 @Override 50 public ResolvedType getType() { 51 return type; 52 } 53 54 private ResolvedType calcType() { 55 // TODO consider interfaces, enums, primitive types, arrays 56 return ReflectionFactory.typeUsageFor(field.getGenericType(), typeSolver); 57 } 58 59 @Override 60 public String getName() { 61 return field.getName(); 62 } 63 64 @Override 65 public boolean isStatic() { 66 return Modifier.isStatic(field.getModifiers()); 67 } 68 69 @Override 70 public boolean isField() { 71 return true; 72 } 73 74 @Override 75 public ResolvedTypeDeclaration declaringType() { 76 return ReflectionFactory.typeDeclarationFor(field.getDeclaringClass(), typeSolver); 77 } 78 79 public ResolvedFieldDeclaration replaceType(ResolvedType fieldType) { 80 return new ReflectionFieldDeclaration(field, typeSolver, fieldType); 81 } 82 83 @Override 84 public boolean isParameter() { 85 return false; 86 } 87 88 @Override 89 public boolean isType() { 90 return false; 91 } 92 93 @Override 94 public AccessSpecifier accessSpecifier() { 95 return ReflectionFactory.modifiersToAccessLevel(field.getModifiers()); 96 } 97 } 98