test/tools/javac/processing/6350124/HelloWorldAP.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/processing/6350124/HelloWorldAP.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,87 @@
     1.4 +/*
     1.5 + * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +import java.io.*;
    1.28 +import javax.annotation.processing.*;
    1.29 +import javax.lang.model.element.*;
    1.30 +import javax.lang.model.type.*;
    1.31 +import javax.lang.model.util.*;
    1.32 +import static javax.tools.Diagnostic.Kind.*;
    1.33 +
    1.34 +import java.util.Set;
    1.35 +
    1.36 +@SupportedAnnotationTypes({"*"})
    1.37 +public class HelloWorldAP extends AbstractProcessor {
    1.38 +    Messager msgr = null;
    1.39 +    Filer filer = null;
    1.40 +    boolean DONE=false;
    1.41 +
    1.42 +    @Override
    1.43 +    public void init(ProcessingEnvironment penv) {
    1.44 +        processingEnv = penv;
    1.45 +        msgr=penv.getMessager();
    1.46 +        filer=penv.getFiler();
    1.47 +    }
    1.48 +
    1.49 +    public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv ) {
    1.50 +        boolean ret = true;
    1.51 +        if(!renv.processingOver() && !DONE) {
    1.52 +            msgr.printMessage(NOTE, "running process to create HelloWorld.");
    1.53 +            try {
    1.54 +                Writer pw = filer.createSourceFile("HelloWorld").openWriter();
    1.55 +                pw.write("public class HelloWorld {\n");
    1.56 +                pw.write("  public static void main (String argv[]) {\n");
    1.57 +                pw.write("    System.out.println(\"Hello apt world.\");\n");
    1.58 +                pw.write("  }\n");
    1.59 +                pw.write("}\n");
    1.60 +                pw.flush();
    1.61 +                pw.close();
    1.62 +
    1.63 +                OutputStream os = filer.createClassFile("HelloWorldAP").openOutputStream();
    1.64 +                // the easiest way to create a class file is to copy another one
    1.65 +                InputStream is = getClass().getResourceAsStream("HelloWorldAP.class");
    1.66 +                copy(is, os);
    1.67 +                is.close();
    1.68 +                os.flush();
    1.69 +                os.close();
    1.70 +                DONE=true;
    1.71 +            }
    1.72 +            catch (IOException ioe) {
    1.73 +                msgr.printMessage(ERROR, ioe.getMessage());
    1.74 +                ret = false;
    1.75 +            }
    1.76 +            catch (Exception e) {
    1.77 +                msgr.printMessage(ERROR, e.getMessage());
    1.78 +                ret = false;
    1.79 +            }
    1.80 +        }
    1.81 +        return ret;
    1.82 +    }
    1.83 +
    1.84 +    void copy(InputStream is, OutputStream os) throws IOException {
    1.85 +        byte[] buf = new byte[8192];
    1.86 +        int n;
    1.87 +        while ((n = is.read(buf, 0, buf.length)) > 0)
    1.88 +            os.write(buf, 0, n);
    1.89 +    }
    1.90 +}

mercurial