Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2010 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2010 Sun Microsystems, Inc. |
41 |
*/ |
42 |
|
43 |
package org.netbeans.modules.projectapi; |
44 |
|
45 |
import java.util.List; |
46 |
import java.util.Set; |
47 |
import javax.annotation.processing.Processor; |
48 |
import javax.annotation.processing.RoundEnvironment; |
49 |
import javax.annotation.processing.SupportedAnnotationTypes; |
50 |
import javax.annotation.processing.SupportedSourceVersion; |
51 |
import javax.lang.model.SourceVersion; |
52 |
import javax.lang.model.element.Element; |
53 |
import javax.lang.model.element.ExecutableElement; |
54 |
import javax.lang.model.element.Modifier; |
55 |
import javax.lang.model.element.TypeElement; |
56 |
import javax.lang.model.element.VariableElement; |
57 |
import javax.lang.model.type.TypeMirror; |
58 |
import org.netbeans.api.project.Project; |
59 |
import org.netbeans.spi.project.ProjectRegistration; |
60 |
import org.openide.filesystems.FileObject; |
61 |
import org.openide.filesystems.annotations.LayerBuilder.File; |
62 |
import org.openide.filesystems.annotations.LayerGeneratingProcessor; |
63 |
import org.openide.filesystems.annotations.LayerGenerationException; |
64 |
import org.openide.util.lookup.ServiceProvider; |
65 |
|
66 |
/** |
67 |
* |
68 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
69 |
*/ |
70 |
@SupportedAnnotationTypes("org.netbeans.spi.project.ProjectRegistration") |
71 |
@SupportedSourceVersion(SourceVersion.RELEASE_6) |
72 |
@ServiceProvider(service=Processor.class) |
73 |
public final class GenericProjectProcessor extends LayerGeneratingProcessor { |
74 |
@Override |
75 |
protected boolean handleProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) throws LayerGenerationException { |
76 |
for (Element e : roundEnv.getElementsAnnotatedWith(ProjectRegistration.class)) { |
77 |
ProjectRegistration pr = e.getAnnotation(ProjectRegistration.class); |
78 |
String[] param = getMethod(e); |
79 |
File f = layer(e).file("Services/ProjectFactories/" + |
80 |
param[0].replace('.', '-') + "-" + param[1] + ".instance" |
81 |
); |
82 |
f.methodvalue("instanceCreate", GenericProjectFactory.class.getName(), "create"); |
83 |
if (pr.mimeType().length() > 0) { |
84 |
f.stringvalue("mimeType", pr.mimeType()); |
85 |
} |
86 |
f.stringvalue("iconBase", pr.iconBase()); |
87 |
int cnt = 0; |
88 |
for (String p : pr.relativeFile()) { |
89 |
f.stringvalue("relativeFile." + cnt++, p); |
90 |
} |
91 |
f.stringvalue("class", param[0]); |
92 |
f.stringvalue("method", param[1]); |
93 |
f.write(); |
94 |
} |
95 |
return true; |
96 |
} |
97 |
|
98 |
private String[] getMethod(Element originatingElement) throws LayerGenerationException { |
99 |
TypeMirror projectMirror = processingEnv.getTypeUtils().getDeclaredType( |
100 |
processingEnv.getElementUtils().getTypeElement(Project.class.getName()) |
101 |
); |
102 |
TypeMirror foMirror = processingEnv.getTypeUtils().getDeclaredType( |
103 |
processingEnv.getElementUtils().getTypeElement(FileObject.class.getName()) |
104 |
); |
105 |
String clazz = processingEnv.getElementUtils().getBinaryName((TypeElement) originatingElement.getEnclosingElement()).toString(); |
106 |
String method = originatingElement.getSimpleName().toString(); |
107 |
if (!originatingElement.getModifiers().contains(Modifier.STATIC)) { |
108 |
throw new LayerGenerationException(clazz + "." + method + " must be static", originatingElement); |
109 |
} |
110 |
final List<? extends VariableElement> params = ((ExecutableElement) originatingElement).getParameters(); |
111 |
boolean sameType = processingEnv.getTypeUtils().isSameType(params.get(0).asType(), foMirror); |
112 |
if (params.size() != 1 || !sameType) { |
113 |
throw new LayerGenerationException(clazz + "." + method + " must take org.openide.filesystems.FileObject agrument", originatingElement); |
114 |
} |
115 |
if (projectMirror != null && !processingEnv.getTypeUtils().isAssignable(((ExecutableElement) originatingElement).getReturnType(), projectMirror)) { |
116 |
throw new LayerGenerationException(clazz + "." + method + " is not assignable to " + projectMirror, originatingElement); |
117 |
} |
118 |
return new String[] {clazz, method}; |
119 |
} |
120 |
|
121 |
} |