Home | History | Annotate | Download | only in Code
      1 /*
      2  * [The "BSD licence"]
      3  * Copyright (c) 2010 Ben Gruver (JesusFreke)
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 package org.jf.dexlib.Code;
     30 
     31 import org.jf.dexlib.*;
     32 
     33 public enum ReferenceType
     34 {
     35     string(-1),
     36     type(0),
     37     field(1),
     38     method(2),
     39     none(-1);
     40 
     41     private int validationErrorReferenceType;
     42 
     43     private ReferenceType(int validationErrorReferenceType) {
     44         this.validationErrorReferenceType = validationErrorReferenceType;
     45     }
     46 
     47     public boolean checkItem(Item item) {
     48         switch (this) {
     49             case string:
     50                 return item instanceof StringIdItem;
     51             case type:
     52                 return item instanceof TypeIdItem;
     53             case field:
     54                 return item instanceof FieldIdItem;
     55             case method:
     56                 return item instanceof MethodIdItem;
     57         }
     58         return false;
     59     }
     60 
     61     public static ReferenceType fromValidationErrorReferenceType(int validationErrorReferenceType) {
     62         switch (validationErrorReferenceType) {
     63             case 0:
     64                 return type;
     65             case 1:
     66                 return field;
     67             case 2:
     68                 return method;
     69         }
     70         return null;
     71     }
     72 
     73     public int getValidationErrorReferenceType() {
     74         if (validationErrorReferenceType == -1) {
     75             throw new RuntimeException("This reference type cannot be referenced from a throw-validation-error" +
     76                     " instruction");
     77         }
     78         return validationErrorReferenceType;
     79     }
     80 }