    001package cpw.mods.fml.common.discovery.asm;
    002
    003import java.util.ArrayList;
    004import java.util.Map;
    005
    006import org.objectweb.asm.Type;
    007
    008import com.google.common.base.Objects;
    009import com.google.common.collect.Lists;
    010import com.google.common.collect.Maps;
    011
    012import cpw.mods.fml.common.discovery.asm.ASMModParser.AnnotationType;
    013
    014public class ModAnnotation
    015{
    016    public class EnumHolder
    017    {
    018
    019        private String desc;
    020        private String value;
    021
    022        public EnumHolder(String desc, String value)
    023        {
    024            this.desc = desc;
    025            this.value = value;
    026        }
    027
    028    }
    029    AnnotationType type;
    030    Type asmType;
    031    String member;
    032    Map<String,Object> values = Maps.newHashMap();
    033    private ArrayList<Object> arrayList;
    034    private Object array;
    035    private String arrayName;
    036    private ModAnnotation parent;
    037    public ModAnnotation(AnnotationType type, Type asmType, String member)
    038    {
    039        this.type = type;
    040        this.asmType = asmType;
    041        this.member = member;
    042    }
    043
    044    public ModAnnotation(AnnotationType type, Type asmType, ModAnnotation parent)
    045    {
    046        this.type = type;
    047        this.asmType = asmType;
    048        this.parent = parent;
    049    }
    050    @Override
    051    public String toString()
    052    {
    053        return Objects.toStringHelper("Annotation")
    054                .add("type",type)
    055                .add("name",asmType.getClassName())
    056                .add("member",member)
    057                .add("values", values)
    058                .toString();
    059    }
    060    public AnnotationType getType()
    061    {
    062        return type;
    063    }
    064    public Type getASMType()
    065    {
    066        return asmType;
    067    }
    068    public String getMember()
    069    {
    070        return member;
    071    }
    072    public Map<String, Object> getValues()
    073    {
    074        return values;
    075    }
    076    public void addArray(String name)
    077    {
    078        this.arrayList = Lists.newArrayList();
    079        this.arrayName = name;
    080    }
    081    public void addProperty(String key, Object value)
    082    {
    083        if (this.arrayList != null)
    084        {
    085            arrayList.add(value);
    086        }
    087        else
    088        {
    089            values.put(key, value);
    090        }
    091    }
    092
    093    public void addEnumProperty(String key, String enumName, String value)
    094    {
    095        values.put(key, new EnumHolder(enumName, value));
    096    }
    097
    098    public void endArray()
    099    {
    100        values.put(arrayName, arrayList);
    101        arrayList = null;
    102    }
    103    public ModAnnotation addChildAnnotation(String name, String desc)
    104    {
    105        return new ModAnnotation(AnnotationType.SUBTYPE, Type.getType(desc), this);
    106    }
    107}