lookup/src/main/java/org/netbeans/modules/openide/util/ServiceProviderProcessor.java
1.1 --- a/lookup/src/main/java/org/netbeans/modules/openide/util/ServiceProviderProcessor.java Wed Jan 27 17:46:23 2010 -0500
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,173 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1.8 - *
1.9 - * The contents of this file are subject to the terms of either the GNU
1.10 - * General Public License Version 2 only ("GPL") or the Common
1.11 - * Development and Distribution License("CDDL") (collectively, the
1.12 - * "License"). You may not use this file except in compliance with the
1.13 - * License. You can obtain a copy of the License at
1.14 - * http://www.netbeans.org/cddl-gplv2.html
1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 - * specific language governing permissions and limitations under the
1.17 - * License. When distributing the software, include this License Header
1.18 - * Notice in each file and include the License file at
1.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 - * particular file as subject to the "Classpath" exception as provided
1.21 - * by Sun in the GPL Version 2 section of the License file that
1.22 - * accompanied this code. If applicable, add the following below the
1.23 - * License Header, with the fields enclosed by brackets [] replaced by
1.24 - * your own identifying information:
1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
1.26 - *
1.27 - * If you wish your version of this file to be governed by only the CDDL
1.28 - * or only the GPL Version 2, indicate your decision by adding
1.29 - * "[Contributor] elects to include this software in this distribution
1.30 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.31 - * single choice of license, a recipient has the option to distribute
1.32 - * your version of this file under either the CDDL, the GPL Version 2 or
1.33 - * to extend the choice of license to its licensees as provided above.
1.34 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.35 - * Version 2 license, then the option applies only if the new code is
1.36 - * made subject to such option by the copyright holder.
1.37 - *
1.38 - * Contributor(s):
1.39 - *
1.40 - * Portions Copyrighted 2008 Sun Microsystems, Inc.
1.41 - */
1.42 -
1.43 -package org.netbeans.modules.openide.util;
1.44 -
1.45 -import java.lang.annotation.Annotation;
1.46 -import java.util.Arrays;
1.47 -import java.util.Collection;
1.48 -import java.util.Collections;
1.49 -import java.util.HashSet;
1.50 -import java.util.LinkedList;
1.51 -import java.util.List;
1.52 -import java.util.Set;
1.53 -import javax.annotation.processing.Completion;
1.54 -import javax.annotation.processing.RoundEnvironment;
1.55 -import javax.annotation.processing.SupportedSourceVersion;
1.56 -import javax.lang.model.SourceVersion;
1.57 -import javax.lang.model.element.AnnotationMirror;
1.58 -import javax.lang.model.element.Element;
1.59 -import javax.lang.model.element.ExecutableElement;
1.60 -import javax.lang.model.element.TypeElement;
1.61 -import javax.lang.model.type.MirroredTypeException;
1.62 -import javax.lang.model.type.TypeKind;
1.63 -import javax.lang.model.type.TypeMirror;
1.64 -import org.openide.util.lookup.ServiceProvider;
1.65 -import org.openide.util.lookup.ServiceProviders;
1.66 -
1.67 -@SupportedSourceVersion(SourceVersion.RELEASE_6)
1.68 -public class ServiceProviderProcessor extends AbstractServiceProviderProcessor {
1.69 -
1.70 - public @Override Set<String> getSupportedAnnotationTypes() {
1.71 - return new HashSet<String>(Arrays.asList(
1.72 - ServiceProvider.class.getCanonicalName(),
1.73 - ServiceProviders.class.getCanonicalName()
1.74 - ));
1.75 - }
1.76 -
1.77 - /** public for ServiceLoader */
1.78 - public ServiceProviderProcessor() {}
1.79 -
1.80 - protected boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
1.81 - for (Element el : roundEnv.getElementsAnnotatedWith(ServiceProvider.class)) {
1.82 - TypeElement clazz = (TypeElement) el;
1.83 - ServiceProvider sp = clazz.getAnnotation(ServiceProvider.class);
1.84 - register(clazz, ServiceProvider.class, sp);
1.85 - }
1.86 - for (Element el : roundEnv.getElementsAnnotatedWith(ServiceProviders.class)) {
1.87 - TypeElement clazz = (TypeElement) el;
1.88 - ServiceProviders spp = clazz.getAnnotation(ServiceProviders.class);
1.89 - for (ServiceProvider sp : spp.value()) {
1.90 - register(clazz, ServiceProviders.class, sp);
1.91 - }
1.92 - }
1.93 - return true;
1.94 - }
1.95 -
1.96 - private void register(TypeElement clazz, Class<? extends Annotation> annotation, ServiceProvider svc) {
1.97 - try {
1.98 - svc.service();
1.99 - assert false;
1.100 - return;
1.101 - } catch (MirroredTypeException e) {
1.102 - register(clazz, annotation, e.getTypeMirror(), svc.path(), svc.position(), svc.supersedes());
1.103 - }
1.104 - }
1.105 -
1.106 - @Override
1.107 - public Iterable<? extends Completion> getCompletions(Element annotated, AnnotationMirror annotation, ExecutableElement attr, String userText) {
1.108 - if (processingEnv == null || annotated == null || !annotated.getKind().isClass()) {
1.109 - return Collections.emptyList();
1.110 - }
1.111 -
1.112 - if ( annotation == null
1.113 - || !"org.openide.util.lookup.ServiceProvider".contentEquals(((TypeElement) annotation.getAnnotationType().asElement()).getQualifiedName())) {
1.114 - return Collections.emptyList();
1.115 - }
1.116 -
1.117 - if (!"service".contentEquals(attr.getSimpleName())) {
1.118 - return Collections.emptyList();
1.119 - }
1.120 -
1.121 - TypeElement jlObject = processingEnv.getElementUtils().getTypeElement("java.lang.Object");
1.122 -
1.123 - if (jlObject == null) {
1.124 - return Collections.emptyList();
1.125 - }
1.126 -
1.127 - Collection<Completion> result = new LinkedList<Completion>();
1.128 - List<TypeElement> toProcess = new LinkedList<TypeElement>();
1.129 -
1.130 - toProcess.add((TypeElement) annotated);
1.131 -
1.132 - while (!toProcess.isEmpty()) {
1.133 - TypeElement c = toProcess.remove(0);
1.134 -
1.135 - result.add(new TypeCompletion(c.getQualifiedName().toString() + ".class"));
1.136 -
1.137 - List<TypeMirror> parents = new LinkedList<TypeMirror>();
1.138 -
1.139 - parents.add(c.getSuperclass());
1.140 - parents.addAll(c.getInterfaces());
1.141 -
1.142 - for (TypeMirror tm : parents) {
1.143 - if (tm == null || tm.getKind() != TypeKind.DECLARED) {
1.144 - continue;
1.145 - }
1.146 -
1.147 - TypeElement type = (TypeElement) processingEnv.getTypeUtils().asElement(tm);
1.148 -
1.149 - if (!jlObject.equals(type)) {
1.150 - toProcess.add(type);
1.151 - }
1.152 - }
1.153 - }
1.154 -
1.155 - return result;
1.156 - }
1.157 -
1.158 - private static final class TypeCompletion implements Completion {
1.159 -
1.160 - private final String type;
1.161 -
1.162 - public TypeCompletion(String type) {
1.163 - this.type = type;
1.164 - }
1.165 -
1.166 - public String getValue() {
1.167 - return type;
1.168 - }
1.169 -
1.170 - public String getMessage() {
1.171 - return null;
1.172 - }
1.173 -
1.174 - }
1.175 -
1.176 -}