Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2012 Trillian Mobile AB
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
  */
 package org.robovm.compiler;
 
 import static soot.tagkit.AnnotationConstants.*;
 
 import java.util.List;
 
 import soot.SootClass;

Author(s):
niklas
 
 public class Annotations {
     
     public enum Visibility { RuntimeVisible, RuntimeInvisible, SourceVisible, Any }
     
     public static final String BRIDGE = "Lorg/robovm/rt/bro/annotation/Bridge;";
     public static final String CALLBACK = "Lorg/robovm/rt/bro/annotation/Callback;";
     public static final String STRUCT_MEMBER = "Lorg/robovm/rt/bro/annotation/StructMember;";
     public static final String GLOBAL_VALUE = "Lorg/robovm/rt/bro/annotation/GlobalValue;";
     public static final String ARRAY = "Lorg/robovm/rt/bro/annotation/Array;";
     public static final String BASE_TYPE = "Lorg/robovm/rt/bro/annotation/BaseType;";
     public static final String STRUCT_RET = "Lorg/robovm/rt/bro/annotation/StructRet;";
     public static final String POINTER = "Lorg/robovm/rt/bro/annotation/Pointer;";
     public static final String MACHINE_SIZED_FLOAT = "Lorg/robovm/rt/bro/annotation/MachineSizedFloat;";
     public static final String MACHINE_SIZED_S_INT = "Lorg/robovm/rt/bro/annotation/MachineSizedSInt;";
     public static final String MACHINE_SIZED_U_INT = "Lorg/robovm/rt/bro/annotation/MachineSizedUInt;";
     public static final String MARSHALER = "Lorg/robovm/rt/bro/annotation/Marshaler;";
     public static final String MARSHALERS = "Lorg/robovm/rt/bro/annotation/Marshalers;";
     public static final String MARSHALS_POINTER = "Lorg/robovm/rt/bro/annotation/MarshalsPointer;";
     public static final String MARSHALS_VALUE = "Lorg/robovm/rt/bro/annotation/MarshalsValue;";
     public static final String MARSHALS_ARRAY = "Lorg/robovm/rt/bro/annotation/MarshalsArray;";
     public static final String AFTER_BRIDGE_CALL = "Lorg/robovm/rt/bro/annotation/AfterBridgeCall;";
     public static final String AFTER_CALLBACK_CALL = "Lorg/robovm/rt/bro/annotation/AfterCallbackCall;";
     public static final String BY_VAL = "Lorg/robovm/rt/bro/annotation/ByVal;";
     public static final String BY_REF = "Lorg/robovm/rt/bro/annotation/ByRef;";
 
     public static boolean hasAnnotation(Host hostString annotationType) {
         return getAnnotation(hostannotationType) != null;
     }
     
     public static boolean hasParameterAnnotation(SootMethod methodint paramIndexString annotationType) {
         return getParameterAnnotation(methodparamIndexannotationType) != null;
     }
 
     public static List<AnnotationTaggetAnnotations(Host hostVisibility visibility) {
         if (host instanceof SootClass) {
             SootResolver.v().bringToHierarchy((SootClasshost);
         }
         List<AnnotationTagresult = new ArrayList<>();
         for (Tag tag : host.getTags()) {
             if (tag instanceof VisibilityAnnotationTag) {
                 if (visibility == .
                         || ((VisibilityAnnotationTagtag).getVisibility() == visibility.ordinal()) {
                     result.addAll(((VisibilityAnnotationTagtag).getAnnotations());
                 }
             }
         }
         return result;
     }
     
     public static AnnotationTag getAnnotation(Host hostString annotationType) {
         for (AnnotationTag tag : getAnnotations(host.)) {
             if (annotationType.equals(tag.getType())) {
                 return tag;
             }                    
         }
         return null;
     }
    
    public static List<AnnotationTaggetParameterAnnotations(SootMethod methodint paramIndexVisibility visibility) {
        List<AnnotationTagresult = new ArrayList<>();
        for (Tag tag : method.getTags()) {
            if (tag instanceof VisibilityParameterAnnotationTag) { 
                if (visibility == . 
                        || ((VisibilityParameterAnnotationTagtag).getKind() == visibility.ordinal()) {
                    
                    ArrayList<VisibilityAnnotationTagl = 
                            ((VisibilityParameterAnnotationTagtag).getVisibilityAnnotations();
                    if (l != null && paramIndex < l.size()) {
                        ArrayList<AnnotationTagannotations = l.get(paramIndex).getAnnotations();
                        if (annotations != null) {
                            result.addAll(annotations);
                        }
                    }
                }
            }
        }
        return result;
    }
    public static List<AnnotationTag>[] getParameterAnnotations(SootMethod methodVisibility visibility) {
        @SuppressWarnings("unchecked")
        ArrayList<AnnotationTag>[] result = new ArrayList[method.getParameterCount()];
        for (int i = 0; i < result.lengthi++) {
            result[i] = new ArrayList<>();
        }
        for (Tag tag : method.getTags()) {
            if (tag instanceof VisibilityParameterAnnotationTag) {
                if (visibility == . 
                        || ((VisibilityParameterAnnotationTagtag).getKind() == visibility.ordinal()) {
                    
                    ArrayList<VisibilityAnnotationTagl = 
                            ((VisibilityParameterAnnotationTagtag).getVisibilityAnnotations();
                    if (l != null) {
                        int i = 0;
                        for (VisibilityAnnotationTag t : l) {
                            ArrayList<AnnotationTagannotations = t.getAnnotations();
                            if (annotations != null) {
                                result[i].addAll(annotations);
                            }
                            i++;
                        }
                    }
                }
            }
        }
        return result;
    }
    
    public static AnnotationTag getParameterAnnotation(SootMethod methodint paramIndexString annotationType) {
        for (AnnotationTag tag : getParameterAnnotations(methodparamIndex.)) {
            if (annotationType.equals(tag.getType())) {
                return tag;
            }                    
        }
        return null;
    }
    
    public static boolean hasBridgeAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    
    public static boolean hasCallbackAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    
    public static boolean hasStructMemberAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    
    public static boolean hasGlobalValueAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    
    public static boolean hasArrayAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    public static boolean hasArrayAnnotation(SootMethod methodint paramIndex) {
        return hasParameterAnnotation(methodparamIndex);
    }
    
    public static boolean hasPointerAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    public static boolean hasMachineSizedFloatAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    public static boolean hasMachineSizedSIntAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    public static boolean hasMachineSizedUIntAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    public static boolean hasByValAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    
    public static boolean hasByRefAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    
    public static boolean hasStructRetAnnotation(SootMethod methodint paramIndex) {
        return hasParameterAnnotation(methodparamIndex);
    }
    
    public static boolean hasPointerAnnotation(SootMethod methodint paramIndex) {
        return hasParameterAnnotation(methodparamIndex);
    }
    public static boolean hasMachineSizedFloatAnnotation(SootMethod methodint paramIndex) {
        return hasParameterAnnotation(methodparamIndex);
    }
    public static boolean hasMachineSizedSIntAnnotation(SootMethod methodint paramIndex) {
        return hasParameterAnnotation(methodparamIndex);
    }
    public static boolean hasMachineSizedUIntAnnotation(SootMethod methodint paramIndex) {
        return hasParameterAnnotation(methodparamIndex);
    }
    public static boolean hasByValAnnotation(SootMethod methodint paramIndex) {
        return hasParameterAnnotation(methodparamIndex);
    }
    
    public static boolean hasByRefAnnotation(SootMethod methodint paramIndex) {
        return hasParameterAnnotation(methodparamIndex);
    }
    
    public static boolean hasAfterBridgeCallAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    
    public static boolean hasAfterCallbackCallAnnotation(SootMethod method) {
        return hasAnnotation(method);
    }
    public static AnnotationTag getMarshalerAnnotation(SootMethod methodint paramIndex) {
        return getParameterAnnotation(methodparamIndex);
    }
    
    public static AnnotationTag getMarshalerAnnotation(SootMethod method) {
        return getAnnotation(method);
    }
    
    public static List<AnnotationTaggetMarshalerAnnotations(SootClass clazz) {
        List<AnnotationTagtags = new ArrayList<AnnotationTag>();
        AnnotationTag marshaler = getAnnotation(clazz);
        if (marshaler != null) {
            tags.add(marshaler);
        } else {
            AnnotationTag marshalers = getAnnotation(clazz);
            if (marshalers != null) {
                for (AnnotationElem e : ((AnnotationArrayElemmarshalers.getElemAt(0)).getValues()) {
                    AnnotationAnnotationElem elem = (AnnotationAnnotationEleme;
                    tags.add(elem.getValue());
                }
            }
        }
        
        return tags;
    }
    
    public static AnnotationTag getMarshalsPointerAnnotation(SootMethod method) {
        return getAnnotation(method);
    }
    public static AnnotationTag getMarshalsValueAnnotation(SootMethod method) {
        return getAnnotation(method);
    }
    
    public static AnnotationTag getMarshalsArrayAnnotation(SootMethod method) {
        return getAnnotation(method);
    }
    public static AnnotationTag getStructMemberAnnotation(SootMethod method) {
        return getAnnotation(method);
    }
    
    public static AnnotationTag getArrayAnnotation(SootMethod method) {
        return getAnnotation(method);
    }
    public static AnnotationTag getArrayAnnotation(SootMethod methodint paramIndex) {
        return getParameterAnnotation(methodparamIndex);
    }
    
    public static AnnotationTag getBaseTypeAnnotation(SootMethod method) {
        return getAnnotation(method);
    }
    public static boolean hasByValAnnotation(SootClass clazz) {
        while (!clazz.isPhantom() && clazz.hasSuperclass()) {
            if (hasAnnotation(clazz)) {
                return true;
            }
            if (hasAnnotation(clazz)) {
                return false;
            }
            clazz = clazz.getSuperclass();
        }
        return false;
    }
    
    public static boolean hasByRefAnnotation(SootClass clazz) {
        while (!clazz.isPhantom() && clazz.hasSuperclass()) {
            if (hasAnnotation(clazz)) {
                return true;
            }
            if (hasAnnotation(clazz)) {
                return false;
            }
            clazz = clazz.getSuperclass();
        }
        return false;
    }
    
    public static AnnotationElem getElemByName(AnnotationTag annotationString name) {
        for (int i = 0; i < annotation.getNumElems(); i++) {
            AnnotationElem elem = annotation.getElemAt(i);
            if (name.equals(elem.getName())) {
                return elem;
            }
        }
        return null;
    }
    
    public static String readStringElem(AnnotationTag annotationString nameString def) {
        AnnotationStringElem elem = (AnnotationStringElemgetElemByName(annotationname);
        return elem != null ? elem.getValue() : def;
    }
    public static boolean readBooleanElem(AnnotationTag annotationString nameboolean def) {
        AnnotationIntElem elem = (AnnotationIntElemgetElemByName(annotationname);
        return elem != null ? elem.getValue() == 1 : def;
    }
    
        for (Tag tag : host.getTags()) {
            if (tag instanceof VisibilityAnnotationTag) {
                if (((VisibilityAnnotationTagtag).getVisibility() == ) {
                    return (VisibilityAnnotationTagtag;
                }
            }
        }
        host.addTag(tag);
        return tag;
    }
    
    public static void addRuntimeVisibleAnnotation(Host hostString annotationType) {
        if (!hasAnnotation(hostannotationType)) {
            tag.addAnnotation(new AnnotationTag(annotationType, 0));
        }
    }
    public static void addRuntimeVisibleAnnotation(Host hostAnnotationTag annoTag) {
        removeAnnotation(hostannoTag.getType());
        tag.addAnnotation(annoTag);
    }
    public static void removeAnnotation(Host hostString annotationType) {
        for (Tag tag : host.getTags()) {
            if (tag instanceof VisibilityAnnotationTag) {
                ArrayList<AnnotationTagl = ((VisibilityAnnotationTagtag).getAnnotations();
                for (Iterator<AnnotationTagit = l.iterator(); it.hasNext();) {
                    AnnotationTag annoTag = it.next();
                    if (annoTag.getType().equals(annotationType)) {
                        it.remove();
                    }
                }
            }
        }
    }
    public static void removeParameterAnnotation(SootMethod methodint paramIndexString annotationType) {
        for (Tag tag : method.getTags()) {
            if (tag instanceof VisibilityParameterAnnotationTag) {
                ArrayList<VisibilityAnnotationTagl = 
                        ((VisibilityParameterAnnotationTagtag).getVisibilityAnnotations();
                if (l != null && paramIndex < l.size()) {
                    for (Iterator<AnnotationTagit = l.get(paramIndex).getAnnotations().iterator(); it.hasNext();) {
                        AnnotationTag annoTag = it.next();
                        if (annoTag.getType().equals(annotationType)) {
                            it.remove();
                        }
                    }
                }
            }
        }
    }
    private static void copyAnnotations(SootMethod fromMethodSootMethod toMethodint visibility) {
        for (Tag tag : fromMethod.getTags()) {
            if (tag instanceof VisibilityAnnotationTag) {
                if (((VisibilityAnnotationTagtag).getVisibility() == visibility) {
                    VisibilityAnnotationTag copy = new VisibilityAnnotationTag(visibility);
                    for (AnnotationTag annoTag : ((VisibilityAnnotationTagtag).getAnnotations()) {
                        copy.addAnnotation(annoTag);
                    }
                    toMethod.addTag(copy);
                }
            }
        }
    }
    
    public static void copyAnnotations(SootMethod fromMethodSootMethod toMethodVisibility visibility) {
        if (visibility == .) {
            copyAnnotations(fromMethodtoMethod);
            copyAnnotations(fromMethodtoMethod);
            copyAnnotations(fromMethodtoMethod);
        } else {
            copyAnnotations(fromMethodtoMethodvisibility.ordinal());
        }
    }
    
    private static void copyParameterAnnotations(SootMethod fromMethodSootMethod toMethodint startint endint shiftint visibility) {
        List<AnnotationTag>[] fromAnnos = getParameterAnnotations(fromMethod, Visibility.values()[visibility]);
        List<AnnotationTag>[] toAnnos = getParameterAnnotations(toMethod, Visibility.values()[visibility]);
        for (int i = starti < endi++) {
            toAnnos[i + shift].addAll(fromAnnos[i]);
        }
        
        for (Iterator<Tagit = toMethod.getTags().iterator(); it.hasNext();) {
            Tag tag = it.next();
            if (tag instanceof VisibilityParameterAnnotationTag) {
                if (((VisibilityParameterAnnotationTagtag).getKind() == visibility) {
                    it.remove();
                }
            }
        }
        
        VisibilityParameterAnnotationTag vpaTag = new VisibilityParameterAnnotationTag(toAnnos.lengthvisibility);
        for (List<AnnotationTagannos : toAnnos) {
            VisibilityAnnotationTag vaTag = new VisibilityAnnotationTag(visibility);
            for (AnnotationTag anno : annos) {
                vaTag.addAnnotation(anno);
            }
            vpaTag.addVisibilityAnnotation(vaTag);
        }
        toMethod.addTag(vpaTag);
    }
    
    public static void copyParameterAnnotations(SootMethod fromMethodSootMethod toMethodint startint endint shiftVisibility visibility) {
        if (visibility == .) {
            copyParameterAnnotations(fromMethodtoMethodstartendshift);
            copyParameterAnnotations(fromMethodtoMethodstartendshift);
            copyParameterAnnotations(fromMethodtoMethodstartendshift);
        } else {
            copyParameterAnnotations(fromMethodtoMethodstartendshiftvisibility.ordinal());
        }
    }
    
    public static VisibilityAnnotationTag getOrCreateRuntimeVisibilityAnnotationTag(SootMethod methodint paramIndex) {
        for (Tag tag : method.getTags()) {
            if (tag instanceof VisibilityParameterAnnotationTag) {
                ArrayList<VisibilityAnnotationTagl = 
                        ((VisibilityParameterAnnotationTagtag).getVisibilityAnnotations();
                if (l != null && paramIndex < l.size()) {
                    if ((l.get(paramIndex)).getVisibility() == ) {
                        return l.get(paramIndex);
                    }
                }
            }
        }
        VisibilityParameterAnnotationTag ptag = 
                new VisibilityParameterAnnotationTag(method.getParameterCount(), );
        for (int i = 0; i < method.getParameterCount(); i++) {
            l.add(new VisibilityAnnotationTag());
        }
        method.addTag(ptag);
        return l.get(paramIndex);
    }
    
    public static void addRuntimeVisibleParameterAnnotation(SootMethod methodint paramIndexString annotationType) {
        if (!hasParameterAnnotation(methodparamIndexannotationType)) {
            VisibilityAnnotationTag tag = getOrCreateRuntimeVisibilityAnnotationTag(methodparamIndex);
            tag.addAnnotation(new AnnotationTag(annotationType, 0));
        }
    }
    public static void addRuntimeVisibleParameterAnnotation(SootMethod methodint paramIndexAnnotationTag annoTag) {
        removeParameterAnnotation(methodparamIndexannoTag.getType());
        VisibilityAnnotationTag tag = getOrCreateRuntimeVisibilityAnnotationTag(methodparamIndex);
        tag.addAnnotation(annoTag);
    }
New to GrepCode? Check out our FAQ X