openide.util.lookup/src/org/netbeans/modules/openide/util/ServiceProviderProcessor.java
1.1 --- a/openide.util.lookup/src/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,174 +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 -import org.openide.util.lookup.implspi.AbstractServiceProviderProcessor;
1.67 -
1.68 -@SupportedSourceVersion(SourceVersion.RELEASE_6)
1.69 -public class ServiceProviderProcessor extends AbstractServiceProviderProcessor {
1.70 -
1.71 - public @Override Set<String> getSupportedAnnotationTypes() {
1.72 - return new HashSet<String>(Arrays.asList(
1.73 - ServiceProvider.class.getCanonicalName(),
1.74 - ServiceProviders.class.getCanonicalName()
1.75 - ));
1.76 - }
1.77 -
1.78 - /** public for ServiceLoader */
1.79 - public ServiceProviderProcessor() {}
1.80 -
1.81 - protected boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
1.82 - for (Element el : roundEnv.getElementsAnnotatedWith(ServiceProvider.class)) {
1.83 - TypeElement clazz = (TypeElement) el;
1.84 - ServiceProvider sp = clazz.getAnnotation(ServiceProvider.class);
1.85 - register(clazz, ServiceProvider.class, sp);
1.86 - }
1.87 - for (Element el : roundEnv.getElementsAnnotatedWith(ServiceProviders.class)) {
1.88 - TypeElement clazz = (TypeElement) el;
1.89 - ServiceProviders spp = clazz.getAnnotation(ServiceProviders.class);
1.90 - for (ServiceProvider sp : spp.value()) {
1.91 - register(clazz, ServiceProviders.class, sp);
1.92 - }
1.93 - }
1.94 - return true;
1.95 - }
1.96 -
1.97 - private void register(TypeElement clazz, Class<? extends Annotation> annotation, ServiceProvider svc) {
1.98 - try {
1.99 - svc.service();
1.100 - assert false;
1.101 - return;
1.102 - } catch (MirroredTypeException e) {
1.103 - register(clazz, annotation, e.getTypeMirror(), svc.path(), svc.position(), svc.supersedes());
1.104 - }
1.105 - }
1.106 -
1.107 - @Override
1.108 - public Iterable<? extends Completion> getCompletions(Element annotated, AnnotationMirror annotation, ExecutableElement attr, String userText) {
1.109 - if (processingEnv == null || annotated == null || !annotated.getKind().isClass()) {
1.110 - return Collections.emptyList();
1.111 - }
1.112 -
1.113 - if ( annotation == null
1.114 - || !"org.openide.util.lookup.ServiceProvider".contentEquals(((TypeElement) annotation.getAnnotationType().asElement()).getQualifiedName())) {
1.115 - return Collections.emptyList();
1.116 - }
1.117 -
1.118 - if (!"service".contentEquals(attr.getSimpleName())) {
1.119 - return Collections.emptyList();
1.120 - }
1.121 -
1.122 - TypeElement jlObject = processingEnv.getElementUtils().getTypeElement("java.lang.Object");
1.123 -
1.124 - if (jlObject == null) {
1.125 - return Collections.emptyList();
1.126 - }
1.127 -
1.128 - Collection<Completion> result = new LinkedList<Completion>();
1.129 - List<TypeElement> toProcess = new LinkedList<TypeElement>();
1.130 -
1.131 - toProcess.add((TypeElement) annotated);
1.132 -
1.133 - while (!toProcess.isEmpty()) {
1.134 - TypeElement c = toProcess.remove(0);
1.135 -
1.136 - result.add(new TypeCompletion(c.getQualifiedName().toString() + ".class"));
1.137 -
1.138 - List<TypeMirror> parents = new LinkedList<TypeMirror>();
1.139 -
1.140 - parents.add(c.getSuperclass());
1.141 - parents.addAll(c.getInterfaces());
1.142 -
1.143 - for (TypeMirror tm : parents) {
1.144 - if (tm == null || tm.getKind() != TypeKind.DECLARED) {
1.145 - continue;
1.146 - }
1.147 -
1.148 - TypeElement type = (TypeElement) processingEnv.getTypeUtils().asElement(tm);
1.149 -
1.150 - if (!jlObject.equals(type)) {
1.151 - toProcess.add(type);
1.152 - }
1.153 - }
1.154 - }
1.155 -
1.156 - return result;
1.157 - }
1.158 -
1.159 - private static final class TypeCompletion implements Completion {
1.160 -
1.161 - private final String type;
1.162 -
1.163 - public TypeCompletion(String type) {
1.164 - this.type = type;
1.165 - }
1.166 -
1.167 - public String getValue() {
1.168 - return type;
1.169 - }
1.170 -
1.171 - public String getMessage() {
1.172 - return null;
1.173 - }
1.174 -
1.175 - }
1.176 -
1.177 -}