| 1 | /*
|
| 2 | * JavaClassFieldModel.java
|
| 3 | *
|
| 4 | * Copyright (c) 2004-2007 GradSoft Ukraine
|
| 5 | * All Rights Reserved
|
| 6 | */
|
| 7 |
|
| 8 |
|
| 9 | package ua.gradsoft.javachecker.models;
|
| 10 |
|
| 11 | import java.lang.annotation.Annotation;
|
| 12 | import java.lang.annotation.ElementType;
|
| 13 | import java.lang.reflect.Field;
|
| 14 | import java.lang.reflect.Type;
|
| 15 | import java.util.List;
|
| 16 | import java.util.Map;
|
| 17 | import ua.gradsoft.javachecker.util.Function;
|
| 18 | import ua.gradsoft.javachecker.util.FunctionMap;
|
| 19 | import ua.gradsoft.javachecker.util.ImmutableMappedCollection;
|
| 20 | import ua.gradsoft.javachecker.util.ImmutableMappedList;
|
| 21 | import ua.gradsoft.javachecker.util.IntegerOrderList;
|
| 22 | import ua.gradsoft.termware.Term;
|
| 23 | import ua.gradsoft.termware.TermWareException;
|
| 24 |
|
| 25 | /**
|
| 26 | *Model for field of class
|
| 27 | * @author Ruslan Shevchenko
|
| 28 | */
|
| 29 | public class JavaClassFieldModel extends JavaMemberVariableModel {
|
| 30 |
|
| 31 | /** Creates a new instance of JavaClassFieldModel */
|
| 32 | public JavaClassFieldModel(Field field,JavaClassTypeModel classTypeModel) {
|
| 33 | field_=field;
|
| 34 | classTypeModel_=classTypeModel;
|
| 35 | }
|
| 36 |
|
| 37 |
|
| 38 | public String getName() {
|
| 39 | return field_.getName();
|
| 40 | }
|
| 41 |
|
| 42 | public JavaModifiersModel getModifiers() {
|
| 43 | int jmodifiers=field_.getModifiers();
|
| 44 | return new JavaClassModifiersModel(getAnnotationsList(),JavaClassTypeModel.translateModifiers(jmodifiers));
|
| 45 | }
|
| 46 |
|
| 47 | public JavaTypeModel getOwnerType() {
|
| 48 | return classTypeModel_;
|
| 49 | }
|
| 50 |
|
| 51 | public JavaTypeModel getType() throws TermWareException {
|
| 52 | Type fieldType = field_.getGenericType();
|
| 53 | return JavaClassTypeModel.createTypeModel(fieldType);
|
| 54 | }
|
| 55 |
|
| 56 | public Map<String,JavaAnnotationInstanceModel> getAnnotationsMap() {
|
| 57 | return new FunctionMap<String,JavaAnnotationInstanceModel>(
|
| 58 | new ImmutableMappedCollection<Integer,String>(
|
| 59 | new IntegerOrderList(field_.getDeclaredAnnotations().length),
|
| 60 | new Function<Integer,String>(){
|
| 61 | public String function(Integer x) {
|
| 62 | return field_.getDeclaredAnnotations()[x].annotationType().getName();
|
| 63 | }
|
| 64 | }
|
| 65 | ),
|
| 66 | new Function<String,JavaAnnotationInstanceModel>(){
|
| 67 | public JavaAnnotationInstanceModel function(String value) throws TermWareException
|
| 68 | {
|
| 69 | Annotation annotation = field_.getAnnotation(JavaClassTypeModel.forName(value));
|
| 70 | if (annotation==null) {
|
| 71 | return null;
|
| 72 | }
|
| 73 | return new JavaClassAnnotationInstanceModel(ElementType.FIELD, annotation ,this);
|
| 74 | }
|
| 75 |
|
| 76 | }
|
| 77 | );
|
| 78 | }
|
| 79 |
|
| 80 | public List<JavaAnnotationInstanceModel> getAnnotationsList()
|
| 81 | {
|
| 82 | return new ImmutableMappedList<Integer,JavaAnnotationInstanceModel>(
|
| 83 | new IntegerOrderList(field_.getDeclaredAnnotations().length),
|
| 84 | new Function<Integer,JavaAnnotationInstanceModel>(){
|
| 85 | public JavaAnnotationInstanceModel function(Integer x) {
|
| 86 | return new JavaClassAnnotationInstanceModel(ElementType.FIELD, field_.getDeclaredAnnotations()[x],this);
|
| 87 | }
|
| 88 | }
|
| 89 | );
|
| 90 | }
|
| 91 |
|
| 92 | public boolean isSupportInitializerExpression()
|
| 93 | { return false; }
|
| 94 |
|
| 95 | public JavaExpressionModel getInitializerExpression()
|
| 96 | { return null; }
|
| 97 |
|
| 98 | public boolean isConstant()
|
| 99 | {
|
| 100 | return this.getModifiers().isFinal() && this.getModifiers().isStatic();
|
| 101 | }
|
| 102 |
|
| 103 | /**
|
| 104 | * ClassField(this)
|
| 105 | */
|
| 106 | public Term getModelTerm() throws TermWareException {
|
| 107 | Term tctx = TermUtils.createJTerm(this);
|
| 108 | return TermUtils.createTerm("ClassField",tctx);
|
| 109 | }
|
| 110 |
|
| 111 |
|
| 112 | private JavaClassTypeModel classTypeModel_;
|
| 113 | private Field field_;
|
| 114 | }
|