1 /*
   2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.replacements.verifier;
  24 
  25 import java.lang.annotation.Annotation;
  26 import java.lang.reflect.Array;
  27 import java.util.ArrayList;
  28 import java.util.List;
  29 
  30 import javax.annotation.processing.ProcessingEnvironment;
  31 import javax.lang.model.element.AnnotationMirror;
  32 import javax.lang.model.element.AnnotationValue;
  33 import javax.lang.model.element.Element;
  34 import javax.lang.model.element.ExecutableElement;
  35 import javax.lang.model.type.TypeMirror;
  36 import javax.lang.model.util.ElementFilter;
  37 
  38 public abstract class AbstractVerifier {
  39 
  40     protected final ProcessingEnvironment env;
  41 
  42     public AbstractVerifier(ProcessingEnvironment env) {
  43         this.env = env;
  44     }
  45 
  46     public abstract void verify(Element element, AnnotationMirror annotation, PluginGenerator generator);
  47 
  48     public abstract Class<? extends Annotation> getAnnotationClass();
  49 
  50     @SuppressWarnings("unchecked")
  51     protected static <T> T resolveAnnotationValue(Class<T> expectedType, AnnotationValue value) {
  52         if (value == null) {
  53             return null;
  54         }
  55         if (expectedType.isArray()) {
  56             ArrayList<Object> result = new ArrayList<>();
  57             List<AnnotationValue> l = (List<AnnotationValue>) value.getValue();
  58             for (AnnotationValue el : l) {
  59                 result.add(resolveAnnotationValue(expectedType.getComponentType(), el));
  60             }
  61             return (T) result.toArray((Object[]) Array.newInstance(expectedType.getComponentType(), result.size()));
  62         }
  63         Object unboxedValue = value.getValue();
  64         if (unboxedValue != null) {
  65             if (expectedType == TypeMirror.class && unboxedValue instanceof String) {
  66                 /*
  67                  * Happens if type is invalid when using the ECJ compiler. The ECJ does not match
  68                  * AP-API specification here.
  69                  */
  70                 return null;
  71             }
  72             if (!expectedType.isAssignableFrom(unboxedValue.getClass())) {
  73                 throw new ClassCastException(unboxedValue.getClass().getName() + " not assignable from " + expectedType.getName());
  74             }
  75         }
  76         return (T) unboxedValue;
  77     }
  78 
  79     protected static AnnotationValue findAnnotationValue(AnnotationMirror mirror, String name) {
  80         ExecutableElement valueMethod = null;
  81         for (ExecutableElement method : ElementFilter.methodsIn(mirror.getAnnotationType().asElement().getEnclosedElements())) {
  82             if (method.getSimpleName().toString().equals(name)) {
  83                 valueMethod = method;
  84                 break;
  85             }
  86         }
  87 
  88         if (valueMethod == null) {
  89             return null;
  90         }
  91 
  92         AnnotationValue value = mirror.getElementValues().get(valueMethod);
  93         if (value == null) {
  94             value = valueMethod.getDefaultValue();
  95         }
  96 
  97         return value;
  98     }
  99 
 100 }