001/*
002 * Forge Mod Loader
003 * Copyright (c) 2012-2013 cpw.
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser Public License v2.1
006 * which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
008 * 
009 * Contributors:
010 *     cpw - implementation
011 */
012
013package cpw.mods.fml.common.discovery.asm;
014
015import java.util.ArrayList;
016import java.util.Map;
017
018import org.objectweb.asm.Type;
019
020import com.google.common.base.Objects;
021import com.google.common.collect.Lists;
022import com.google.common.collect.Maps;
023
024import cpw.mods.fml.common.discovery.asm.ASMModParser.AnnotationType;
025
026public class ModAnnotation
027{
028    public class EnumHolder
029    {
030
031        private String desc;
032        private String value;
033
034        public EnumHolder(String desc, String value)
035        {
036            this.desc = desc;
037            this.value = value;
038        }
039
040    }
041    AnnotationType type;
042    Type asmType;
043    String member;
044    Map<String,Object> values = Maps.newHashMap();
045    private ArrayList<Object> arrayList;
046    private Object array;
047    private String arrayName;
048    private ModAnnotation parent;
049    public ModAnnotation(AnnotationType type, Type asmType, String member)
050    {
051        this.type = type;
052        this.asmType = asmType;
053        this.member = member;
054    }
055    
056    public ModAnnotation(AnnotationType type, Type asmType, ModAnnotation parent)
057    {
058        this.type = type;
059        this.asmType = asmType;
060        this.parent = parent;
061    }
062    @Override
063    public String toString()
064    {
065        return Objects.toStringHelper("Annotation")
066                .add("type",type)
067                .add("name",asmType.getClassName())
068                .add("member",member)
069                .add("values", values)
070                .toString();
071    }
072    public AnnotationType getType()
073    {
074        return type;
075    }
076    public Type getASMType()
077    {
078        return asmType;
079    }
080    public String getMember()
081    {
082        return member;
083    }
084    public Map<String, Object> getValues()
085    {
086        return values;
087    }
088    public void addArray(String name)
089    {
090        this.arrayList = Lists.newArrayList();
091        this.arrayName = name;
092    }
093    public void addProperty(String key, Object value)
094    {
095        if (this.arrayList != null)
096        {
097            arrayList.add(value);
098        }
099        else
100        {
101            values.put(key, value);
102        }
103    }
104    
105    public void addEnumProperty(String key, String enumName, String value)
106    {
107        values.put(key, new EnumHolder(enumName, value));
108    }
109    
110    public void endArray()
111    {
112        values.put(arrayName, arrayList);
113        arrayList = null;
114    }
115    public ModAnnotation addChildAnnotation(String name, String desc)
116    {
117        return new ModAnnotation(AnnotationType.SUBTYPE, Type.getType(desc), this);
118    }
119}