Moving into implementation package - e.g. org.netbeans.html
1.1 --- a/html4j-maven-plugin/src/main/java/org/apidesign/html/mojo/ProcessJsAnnotationsMojo.java Thu Feb 06 11:09:50 2014 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,221 +0,0 @@
1.4 -/**
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
1.8 - *
1.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 - * Other names may be trademarks of their respective owners.
1.11 - *
1.12 - * The contents of this file are subject to the terms of either the GNU
1.13 - * General Public License Version 2 only ("GPL") or the Common
1.14 - * Development and Distribution License("CDDL") (collectively, the
1.15 - * "License"). You may not use this file except in compliance with the
1.16 - * License. You can obtain a copy of the License at
1.17 - * http://www.netbeans.org/cddl-gplv2.html
1.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 - * specific language governing permissions and limitations under the
1.20 - * License. When distributing the software, include this License Header
1.21 - * Notice in each file and include the License file at
1.22 - * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 - * particular file as subject to the "Classpath" exception as provided
1.24 - * by Oracle in the GPL Version 2 section of the License file that
1.25 - * accompanied this code. If applicable, add the following below the
1.26 - * License Header, with the fields enclosed by brackets [] replaced by
1.27 - * your own identifying information:
1.28 - * "Portions Copyrighted [year] [name of copyright owner]"
1.29 - *
1.30 - * Contributor(s):
1.31 - *
1.32 - * The Original Software is NetBeans. The Initial Developer of the Original
1.33 - * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
1.34 - *
1.35 - * If you wish your version of this file to be governed by only the CDDL
1.36 - * or only the GPL Version 2, indicate your decision by adding
1.37 - * "[Contributor] elects to include this software in this distribution
1.38 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 - * single choice of license, a recipient has the option to distribute
1.40 - * your version of this file under either the CDDL, the GPL Version 2 or
1.41 - * to extend the choice of license to its licensees as provided above.
1.42 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 - * Version 2 license, then the option applies only if the new code is
1.44 - * made subject to such option by the copyright holder.
1.45 - */
1.46 -package org.apidesign.html.mojo;
1.47 -
1.48 -import java.io.BufferedReader;
1.49 -import java.io.File;
1.50 -import java.io.FileInputStream;
1.51 -import java.io.FileNotFoundException;
1.52 -import java.io.FileOutputStream;
1.53 -import java.io.FileReader;
1.54 -import java.io.FileWriter;
1.55 -import java.io.IOException;
1.56 -import java.io.InputStream;
1.57 -import java.lang.reflect.Method;
1.58 -import java.net.MalformedURLException;
1.59 -import java.net.URL;
1.60 -import java.net.URLClassLoader;
1.61 -import java.util.ArrayList;
1.62 -import java.util.List;
1.63 -import org.apache.maven.artifact.Artifact;
1.64 -import org.apache.maven.plugin.AbstractMojo;
1.65 -import org.apache.maven.plugin.MojoExecutionException;
1.66 -import org.apache.maven.plugin.MojoFailureException;
1.67 -import org.apache.maven.plugins.annotations.Component;
1.68 -import org.apache.maven.plugins.annotations.LifecyclePhase;
1.69 -import org.apache.maven.plugins.annotations.Mojo;
1.70 -import org.apache.maven.plugins.annotations.Parameter;
1.71 -import org.apache.maven.plugins.annotations.ResolutionScope;
1.72 -import org.apache.maven.project.MavenProject;
1.73 -import org.objectweb.asm.ClassReader;
1.74 -
1.75 -@Mojo(
1.76 - name="process-js-annotations",
1.77 - requiresDependencyResolution = ResolutionScope.COMPILE,
1.78 - defaultPhase= LifecyclePhase.PROCESS_CLASSES
1.79 -)
1.80 -public final class ProcessJsAnnotationsMojo extends AbstractMojo {
1.81 - @Component
1.82 - private MavenProject prj;
1.83 -
1.84 - @Parameter(defaultValue = "${project.build.directory}/classes")
1.85 - private File classes;
1.86 -
1.87 - public ProcessJsAnnotationsMojo() {
1.88 - }
1.89 -
1.90 - @Override
1.91 - public void execute() throws MojoExecutionException, MojoFailureException {
1.92 - List<URL> arr = new ArrayList<URL>();
1.93 - boolean foundAsm = false;
1.94 - for (Artifact a : prj.getArtifacts()) {
1.95 - final File f = a.getFile();
1.96 - if (f != null) {
1.97 - if (a.getArtifactId().equals("asm")) {
1.98 - foundAsm = true;
1.99 - }
1.100 - try {
1.101 - arr.add(f.toURI().toURL());
1.102 - } catch (MalformedURLException ex) {
1.103 - throw new IllegalStateException(ex);
1.104 - }
1.105 - }
1.106 - }
1.107 - if (!foundAsm) {
1.108 - URL loc = ClassReader.class.getProtectionDomain().getCodeSource().getLocation();
1.109 - arr.add(loc);
1.110 - }
1.111 - URLClassLoader l = new URLClassLoader(arr.toArray(new URL[arr.size()]));
1.112 - try {
1.113 - File master = new File(new File(classes, "META-INF"), "net.java.html.js.classes");
1.114 - processClasses(l, master, classes);
1.115 - } catch (IOException ex) {
1.116 - throw new MojoExecutionException("Problem converting JavaScriptXXX annotations", ex);
1.117 - }
1.118 - }
1.119 -
1.120 - private void processClasses(ClassLoader l, File master, File f) throws IOException, MojoExecutionException {
1.121 - if (!f.exists()) {
1.122 - return;
1.123 - }
1.124 - if (f.isDirectory()) {
1.125 - boolean classes = new File(f, "net.java.html.js.classes").exists();
1.126 - File[] arr = f.listFiles();
1.127 - if (arr != null) {
1.128 - for (File file : arr) {
1.129 - if (classes || file.isDirectory()) {
1.130 - processClasses(l, master, file);
1.131 - }
1.132 - }
1.133 - }
1.134 - return;
1.135 - }
1.136 -
1.137 - if (!f.getName().endsWith(".class")) {
1.138 - return;
1.139 - }
1.140 -
1.141 - byte[] arr = new byte[(int)f.length()];
1.142 - FileInputStream is = new FileInputStream(f);
1.143 - try {
1.144 - readArr(arr, is);
1.145 - } finally {
1.146 - is.close();
1.147 - }
1.148 -
1.149 - byte[] newArr = null;
1.150 - try {
1.151 - Class<?> fnUtils = l.loadClass("org.netbeans.html.boot.impl.FnUtils");
1.152 - Method transform = fnUtils.getMethod("transform", byte[].class, ClassLoader.class);
1.153 -
1.154 - newArr = (byte[]) transform.invoke(null, arr, l);
1.155 - if (newArr == null || newArr == arr) {
1.156 - return;
1.157 - }
1.158 - filterClass(new File(f.getParentFile(), "net.java.html.js.classes"), f.getName());
1.159 - filterClass(master, f.getName());
1.160 - } catch (Exception ex) {
1.161 - throw new MojoExecutionException("Can't process " + f, ex);
1.162 - }
1.163 - getLog().info("Processing " + f);
1.164 - writeArr(f, newArr);
1.165 - }
1.166 -
1.167 - private void writeArr(File f, byte[] newArr) throws IOException, FileNotFoundException {
1.168 - FileOutputStream os = new FileOutputStream(f);
1.169 - try {
1.170 - os.write(newArr);
1.171 - } finally {
1.172 - os.close();
1.173 - }
1.174 - }
1.175 -
1.176 - private static void readArr(byte[] arr, InputStream is) throws IOException {
1.177 - int off = 0;
1.178 - while (off< arr.length) {
1.179 - int read = is.read(arr, off, arr.length - off);
1.180 - if (read == -1) {
1.181 - break;
1.182 - }
1.183 - off += read;
1.184 - }
1.185 - }
1.186 -
1.187 - private static void filterClass(File f, String className) throws IOException {
1.188 - if (!f.exists()) {
1.189 - return;
1.190 - }
1.191 - if (className.endsWith(".class")) {
1.192 - className = className.substring(0, className.length() - 6);
1.193 - }
1.194 -
1.195 - BufferedReader r = new BufferedReader(new FileReader(f));
1.196 - List<String> arr = new ArrayList<String>();
1.197 - boolean modified = false;
1.198 - for (;;) {
1.199 - String line = r.readLine();
1.200 - if (line == null) {
1.201 - break;
1.202 - }
1.203 - if (line.endsWith(className)) {
1.204 - modified = true;
1.205 - continue;
1.206 - }
1.207 - arr.add(line);
1.208 - }
1.209 - r.close();
1.210 -
1.211 - if (modified) {
1.212 - if (arr.isEmpty()) {
1.213 - f.delete();
1.214 - } else {
1.215 - FileWriter w = new FileWriter(f);
1.216 - for (String l : arr) {
1.217 - w.write(l);
1.218 - w.write("\n");
1.219 - }
1.220 - w.close();
1.221 - }
1.222 - }
1.223 - }
1.224 -}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/html4j-maven-plugin/src/main/java/org/netbeans/html/mojo/ProcessJsAnnotationsMojo.java Thu Feb 06 11:10:09 2014 +0100
2.3 @@ -0,0 +1,221 @@
2.4 +/**
2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2.6 + *
2.7 + * Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
2.8 + *
2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
2.10 + * Other names may be trademarks of their respective owners.
2.11 + *
2.12 + * The contents of this file are subject to the terms of either the GNU
2.13 + * General Public License Version 2 only ("GPL") or the Common
2.14 + * Development and Distribution License("CDDL") (collectively, the
2.15 + * "License"). You may not use this file except in compliance with the
2.16 + * License. You can obtain a copy of the License at
2.17 + * http://www.netbeans.org/cddl-gplv2.html
2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
2.19 + * specific language governing permissions and limitations under the
2.20 + * License. When distributing the software, include this License Header
2.21 + * Notice in each file and include the License file at
2.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
2.23 + * particular file as subject to the "Classpath" exception as provided
2.24 + * by Oracle in the GPL Version 2 section of the License file that
2.25 + * accompanied this code. If applicable, add the following below the
2.26 + * License Header, with the fields enclosed by brackets [] replaced by
2.27 + * your own identifying information:
2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
2.29 + *
2.30 + * Contributor(s):
2.31 + *
2.32 + * The Original Software is NetBeans. The Initial Developer of the Original
2.33 + * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
2.34 + *
2.35 + * If you wish your version of this file to be governed by only the CDDL
2.36 + * or only the GPL Version 2, indicate your decision by adding
2.37 + * "[Contributor] elects to include this software in this distribution
2.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
2.39 + * single choice of license, a recipient has the option to distribute
2.40 + * your version of this file under either the CDDL, the GPL Version 2 or
2.41 + * to extend the choice of license to its licensees as provided above.
2.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
2.43 + * Version 2 license, then the option applies only if the new code is
2.44 + * made subject to such option by the copyright holder.
2.45 + */
2.46 +package org.netbeans.html.mojo;
2.47 +
2.48 +import java.io.BufferedReader;
2.49 +import java.io.File;
2.50 +import java.io.FileInputStream;
2.51 +import java.io.FileNotFoundException;
2.52 +import java.io.FileOutputStream;
2.53 +import java.io.FileReader;
2.54 +import java.io.FileWriter;
2.55 +import java.io.IOException;
2.56 +import java.io.InputStream;
2.57 +import java.lang.reflect.Method;
2.58 +import java.net.MalformedURLException;
2.59 +import java.net.URL;
2.60 +import java.net.URLClassLoader;
2.61 +import java.util.ArrayList;
2.62 +import java.util.List;
2.63 +import org.apache.maven.artifact.Artifact;
2.64 +import org.apache.maven.plugin.AbstractMojo;
2.65 +import org.apache.maven.plugin.MojoExecutionException;
2.66 +import org.apache.maven.plugin.MojoFailureException;
2.67 +import org.apache.maven.plugins.annotations.Component;
2.68 +import org.apache.maven.plugins.annotations.LifecyclePhase;
2.69 +import org.apache.maven.plugins.annotations.Mojo;
2.70 +import org.apache.maven.plugins.annotations.Parameter;
2.71 +import org.apache.maven.plugins.annotations.ResolutionScope;
2.72 +import org.apache.maven.project.MavenProject;
2.73 +import org.objectweb.asm.ClassReader;
2.74 +
2.75 +@Mojo(
2.76 + name="process-js-annotations",
2.77 + requiresDependencyResolution = ResolutionScope.COMPILE,
2.78 + defaultPhase= LifecyclePhase.PROCESS_CLASSES
2.79 +)
2.80 +public final class ProcessJsAnnotationsMojo extends AbstractMojo {
2.81 + @Component
2.82 + private MavenProject prj;
2.83 +
2.84 + @Parameter(defaultValue = "${project.build.directory}/classes")
2.85 + private File classes;
2.86 +
2.87 + public ProcessJsAnnotationsMojo() {
2.88 + }
2.89 +
2.90 + @Override
2.91 + public void execute() throws MojoExecutionException, MojoFailureException {
2.92 + List<URL> arr = new ArrayList<URL>();
2.93 + boolean foundAsm = false;
2.94 + for (Artifact a : prj.getArtifacts()) {
2.95 + final File f = a.getFile();
2.96 + if (f != null) {
2.97 + if (a.getArtifactId().equals("asm")) {
2.98 + foundAsm = true;
2.99 + }
2.100 + try {
2.101 + arr.add(f.toURI().toURL());
2.102 + } catch (MalformedURLException ex) {
2.103 + throw new IllegalStateException(ex);
2.104 + }
2.105 + }
2.106 + }
2.107 + if (!foundAsm) {
2.108 + URL loc = ClassReader.class.getProtectionDomain().getCodeSource().getLocation();
2.109 + arr.add(loc);
2.110 + }
2.111 + URLClassLoader l = new URLClassLoader(arr.toArray(new URL[arr.size()]));
2.112 + try {
2.113 + File master = new File(new File(classes, "META-INF"), "net.java.html.js.classes");
2.114 + processClasses(l, master, classes);
2.115 + } catch (IOException ex) {
2.116 + throw new MojoExecutionException("Problem converting JavaScriptXXX annotations", ex);
2.117 + }
2.118 + }
2.119 +
2.120 + private void processClasses(ClassLoader l, File master, File f) throws IOException, MojoExecutionException {
2.121 + if (!f.exists()) {
2.122 + return;
2.123 + }
2.124 + if (f.isDirectory()) {
2.125 + boolean classes = new File(f, "net.java.html.js.classes").exists();
2.126 + File[] arr = f.listFiles();
2.127 + if (arr != null) {
2.128 + for (File file : arr) {
2.129 + if (classes || file.isDirectory()) {
2.130 + processClasses(l, master, file);
2.131 + }
2.132 + }
2.133 + }
2.134 + return;
2.135 + }
2.136 +
2.137 + if (!f.getName().endsWith(".class")) {
2.138 + return;
2.139 + }
2.140 +
2.141 + byte[] arr = new byte[(int)f.length()];
2.142 + FileInputStream is = new FileInputStream(f);
2.143 + try {
2.144 + readArr(arr, is);
2.145 + } finally {
2.146 + is.close();
2.147 + }
2.148 +
2.149 + byte[] newArr = null;
2.150 + try {
2.151 + Class<?> fnUtils = l.loadClass("org.netbeans.html.boot.impl.FnUtils");
2.152 + Method transform = fnUtils.getMethod("transform", byte[].class, ClassLoader.class);
2.153 +
2.154 + newArr = (byte[]) transform.invoke(null, arr, l);
2.155 + if (newArr == null || newArr == arr) {
2.156 + return;
2.157 + }
2.158 + filterClass(new File(f.getParentFile(), "net.java.html.js.classes"), f.getName());
2.159 + filterClass(master, f.getName());
2.160 + } catch (Exception ex) {
2.161 + throw new MojoExecutionException("Can't process " + f, ex);
2.162 + }
2.163 + getLog().info("Processing " + f);
2.164 + writeArr(f, newArr);
2.165 + }
2.166 +
2.167 + private void writeArr(File f, byte[] newArr) throws IOException, FileNotFoundException {
2.168 + FileOutputStream os = new FileOutputStream(f);
2.169 + try {
2.170 + os.write(newArr);
2.171 + } finally {
2.172 + os.close();
2.173 + }
2.174 + }
2.175 +
2.176 + private static void readArr(byte[] arr, InputStream is) throws IOException {
2.177 + int off = 0;
2.178 + while (off< arr.length) {
2.179 + int read = is.read(arr, off, arr.length - off);
2.180 + if (read == -1) {
2.181 + break;
2.182 + }
2.183 + off += read;
2.184 + }
2.185 + }
2.186 +
2.187 + private static void filterClass(File f, String className) throws IOException {
2.188 + if (!f.exists()) {
2.189 + return;
2.190 + }
2.191 + if (className.endsWith(".class")) {
2.192 + className = className.substring(0, className.length() - 6);
2.193 + }
2.194 +
2.195 + BufferedReader r = new BufferedReader(new FileReader(f));
2.196 + List<String> arr = new ArrayList<String>();
2.197 + boolean modified = false;
2.198 + for (;;) {
2.199 + String line = r.readLine();
2.200 + if (line == null) {
2.201 + break;
2.202 + }
2.203 + if (line.endsWith(className)) {
2.204 + modified = true;
2.205 + continue;
2.206 + }
2.207 + arr.add(line);
2.208 + }
2.209 + r.close();
2.210 +
2.211 + if (modified) {
2.212 + if (arr.isEmpty()) {
2.213 + f.delete();
2.214 + } else {
2.215 + FileWriter w = new FileWriter(f);
2.216 + for (String l : arr) {
2.217 + w.write(l);
2.218 + w.write("\n");
2.219 + }
2.220 + w.close();
2.221 + }
2.222 + }
2.223 + }
2.224 +}