html4j-maven-plugin/src/main/java/org/netbeans/html/mojo/ProcessJsAnnotationsMojo.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
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]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package org.netbeans.html.mojo;
45 import java.io.BufferedReader;
47 import java.io.FileInputStream;
48 import java.io.FileNotFoundException;
49 import java.io.FileOutputStream;
50 import java.io.FileReader;
51 import java.io.FileWriter;
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.lang.reflect.Method;
55 import java.net.MalformedURLException;
57 import java.net.URLClassLoader;
58 import java.util.ArrayList;
59 import java.util.List;
60 import org.apache.maven.artifact.Artifact;
61 import org.apache.maven.plugin.AbstractMojo;
62 import org.apache.maven.plugin.MojoExecutionException;
63 import org.apache.maven.plugin.MojoFailureException;
64 import org.apache.maven.plugins.annotations.Component;
65 import org.apache.maven.plugins.annotations.LifecyclePhase;
66 import org.apache.maven.plugins.annotations.Mojo;
67 import org.apache.maven.plugins.annotations.Parameter;
68 import org.apache.maven.plugins.annotations.ResolutionScope;
69 import org.apache.maven.project.MavenProject;
70 import org.objectweb.asm.ClassReader;
73 name="process-js-annotations",
74 requiresDependencyResolution = ResolutionScope.COMPILE,
75 defaultPhase= LifecyclePhase.PROCESS_CLASSES
77 public final class ProcessJsAnnotationsMojo extends AbstractMojo {
79 private MavenProject prj;
81 @Parameter(defaultValue = "${project.build.directory}/classes")
84 public ProcessJsAnnotationsMojo() {
88 public void execute() throws MojoExecutionException, MojoFailureException {
89 List<URL> arr = new ArrayList<URL>();
90 boolean foundAsm = false;
91 for (Artifact a : prj.getArtifacts()) {
92 final File f = a.getFile();
94 if (a.getArtifactId().equals("asm")) {
98 arr.add(f.toURI().toURL());
99 } catch (MalformedURLException ex) {
100 throw new IllegalStateException(ex);
105 URL loc = ClassReader.class.getProtectionDomain().getCodeSource().getLocation();
108 URLClassLoader l = new URLClassLoader(arr.toArray(new URL[arr.size()]));
110 File master = new File(new File(classes, "META-INF"), "net.java.html.js.classes");
111 processClasses(l, master, classes);
112 } catch (IOException ex) {
113 throw new MojoExecutionException("Problem converting JavaScriptXXX annotations", ex);
117 private void processClasses(ClassLoader l, File master, File f) throws IOException, MojoExecutionException {
121 if (f.isDirectory()) {
122 boolean classes = new File(f, "net.java.html.js.classes").exists();
123 File[] arr = f.listFiles();
125 for (File file : arr) {
126 if (classes || file.isDirectory()) {
127 processClasses(l, master, file);
134 if (!f.getName().endsWith(".class")) {
138 byte[] arr = new byte[(int)f.length()];
139 FileInputStream is = new FileInputStream(f);
146 byte[] newArr = null;
148 Class<?> fnUtils = l.loadClass("org.netbeans.html.boot.impl.FnUtils");
149 Method transform = fnUtils.getMethod("transform", byte[].class, ClassLoader.class);
151 newArr = (byte[]) transform.invoke(null, arr, l);
152 if (newArr == null || newArr == arr) {
155 filterClass(new File(f.getParentFile(), "net.java.html.js.classes"), f.getName());
156 filterClass(master, f.getName());
157 } catch (Exception ex) {
158 throw new MojoExecutionException("Can't process " + f, ex);
160 getLog().info("Processing " + f);
164 private void writeArr(File f, byte[] newArr) throws IOException, FileNotFoundException {
165 FileOutputStream os = new FileOutputStream(f);
173 private static void readArr(byte[] arr, InputStream is) throws IOException {
175 while (off< arr.length) {
176 int read = is.read(arr, off, arr.length - off);
184 private static void filterClass(File f, String className) throws IOException {
188 if (className.endsWith(".class")) {
189 className = className.substring(0, className.length() - 6);
192 BufferedReader r = new BufferedReader(new FileReader(f));
193 List<String> arr = new ArrayList<String>();
194 boolean modified = false;
196 String line = r.readLine();
200 if (line.endsWith(className)) {
212 FileWriter w = new FileWriter(f);
213 for (String l : arr) {