001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.replacements.verifier;
024
025import java.lang.annotation.*;
026
027import javax.annotation.processing.*;
028import javax.lang.model.element.*;
029import javax.lang.model.type.*;
030import javax.tools.Diagnostic.Kind;
031
032import com.oracle.graal.api.replacements.*;
033
034public final class ClassSubstitutionVerifier extends AbstractVerifier {
035
036    private static final String TYPE_VALUE = "value";
037    private static final String STRING_VALUE = "className";
038    private static final String OPTIONAL = "optional";
039
040    public ClassSubstitutionVerifier(ProcessingEnvironment env) {
041        super(env);
042    }
043
044    @Override
045    public Class<? extends Annotation> getAnnotationClass() {
046        return ClassSubstitution.class;
047    }
048
049    @Override
050    public void verify(Element element, AnnotationMirror classSubstitution) {
051        if (!element.getKind().isClass()) {
052            assert false : "Element is guaranteed to be a class.";
053            return;
054        }
055        TypeElement type = (TypeElement) element;
056
057        TypeElement substitutionType = resolveOriginalType(env, type, classSubstitution);
058        if (substitutionType == null) {
059            return;
060        }
061    }
062
063    static TypeElement resolveOriginalType(ProcessingEnvironment env, Element sourceElement, AnnotationMirror classSubstition) {
064        AnnotationValue typeValue = findAnnotationValue(classSubstition, TYPE_VALUE);
065        AnnotationValue stringValue = findAnnotationValue(classSubstition, STRING_VALUE);
066        AnnotationValue optionalValue = findAnnotationValue(classSubstition, OPTIONAL);
067
068        assert typeValue != null && stringValue != null && optionalValue != null;
069
070        TypeMirror type = resolveAnnotationValue(TypeMirror.class, typeValue);
071        String[] classNames = resolveAnnotationValue(String[].class, stringValue);
072        boolean optional = resolveAnnotationValue(Boolean.class, optionalValue);
073
074        if (type.getKind() != TypeKind.DECLARED) {
075            env.getMessager().printMessage(Kind.ERROR, "The provided class must be a declared type.", sourceElement, classSubstition, typeValue);
076            return null;
077        }
078
079        if (!classSubstition.getAnnotationType().asElement().equals(((DeclaredType) type).asElement())) {
080            if (classNames.length != 0) {
081                String msg = "The usage of value and className is exclusive.";
082                env.getMessager().printMessage(Kind.ERROR, msg, sourceElement, classSubstition, stringValue);
083                env.getMessager().printMessage(Kind.ERROR, msg, sourceElement, classSubstition, typeValue);
084            }
085
086            return (TypeElement) ((DeclaredType) type).asElement();
087        }
088
089        if (classNames.length != 0) {
090            TypeElement typeElement = null;
091            for (String className : classNames) {
092                typeElement = env.getElementUtils().getTypeElement(className);
093                if (typeElement == null && !optional) {
094                    env.getMessager().printMessage(Kind.ERROR, String.format("The class '%s' was not found on the classpath.", stringValue), sourceElement, classSubstition, stringValue);
095                }
096            }
097
098            return typeElement;
099        }
100
101        if (!optional) {
102            env.getMessager().printMessage(Kind.ERROR, String.format("No value for '%s' or '%s' provided but required.", TYPE_VALUE, STRING_VALUE), sourceElement, classSubstition);
103        }
104
105        return null;
106    }
107
108}