jaxws/src/jdk.xml.bind/share/classes/com/sun/codemodel/internal/fmt/JStaticJavaFile.java
changeset 36523 116e5d5cdade
parent 25871 b80b84e87032
equal deleted inserted replaced
36522:0017f3cf1657 36523:116e5d5cdade
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    31 import java.io.InputStream;
    31 import java.io.InputStream;
    32 import java.io.InputStreamReader;
    32 import java.io.InputStreamReader;
    33 import java.io.OutputStream;
    33 import java.io.OutputStream;
    34 import java.io.OutputStreamWriter;
    34 import java.io.OutputStreamWriter;
    35 import java.io.PrintWriter;
    35 import java.io.PrintWriter;
    36 import java.net.URL;
       
    37 import java.text.ParseException;
    36 import java.text.ParseException;
    38 import java.util.Iterator;
    37 import java.util.Iterator;
    39 import java.util.List;
    38 import java.util.List;
    40 
    39 
    41 import com.sun.codemodel.internal.JClass;
    40 import com.sun.codemodel.internal.JClass;
    67  */
    66  */
    68 public final class JStaticJavaFile extends JResourceFile {
    67 public final class JStaticJavaFile extends JResourceFile {
    69 
    68 
    70     private final JPackage pkg;
    69     private final JPackage pkg;
    71     private final String className;
    70     private final String className;
    72     private final URL source;
    71     private final ResourceLoader source;
    73     private final JStaticClass clazz;
    72     private final JStaticClass clazz;
    74     private final LineFilter filter;
    73     private final LineFilter filter;
    75 
    74 
    76     public JStaticJavaFile(JPackage _pkg, String className, String _resourceName) {
    75     public JStaticJavaFile(JPackage _pkg, String _className, Class<?> loadingClass, LineFilter _filter) {
    77         this( _pkg, className,
    76         super(_className + ".java");
    78             SecureLoader.getClassClassLoader(JStaticJavaFile.class).getResource(_resourceName), null );
    77         if (loadingClass == null) throw new NullPointerException();
    79     }
       
    80 
       
    81     public JStaticJavaFile(JPackage _pkg, String _className, URL _source, LineFilter _filter ) {
       
    82         super(_className+".java");
       
    83         if(_source==null)   throw new NullPointerException();
       
    84         this.pkg = _pkg;
    78         this.pkg = _pkg;
    85         this.clazz = new JStaticClass();
    79         this.clazz = new JStaticClass();
    86         this.className = _className;
    80         this.className = _className;
    87         this.source = _source;
    81         this.source = new ResourceLoader(_className, loadingClass);
    88         this.filter = _filter;
    82         this.filter = _filter;
    89     }
    83     }
    90 
    84 
    91     /**
    85     /**
    92      * Returns a class object that represents a statically generated code.
    86      * Returns a class object that represents a statically generated code.
    98     protected boolean isResource() {
    92     protected boolean isResource() {
    99         return false;
    93         return false;
   100     }
    94     }
   101 
    95 
   102     protected  void build(OutputStream os) throws IOException {
    96     protected  void build(OutputStream os) throws IOException {
   103         InputStream is = source.openStream();
       
   104 
       
   105         BufferedReader r = new BufferedReader(new InputStreamReader(is));
       
   106         PrintWriter w = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
       
   107         LineFilter filter = createLineFilter();
       
   108         int lineNumber=1;
    97         int lineNumber=1;
   109 
    98         try (
   110         try {
    99                 InputStream is = source.getResourceAsStream();
       
   100                 BufferedReader r = new BufferedReader(new InputStreamReader(is));
       
   101                 PrintWriter w = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os)));
       
   102         ) {
       
   103             LineFilter filter = createLineFilter();
   111             String line;
   104             String line;
   112             while((line=r.readLine())!=null) {
   105             while((line=r.readLine())!=null) {
   113                 line = filter.process(line);
   106                 line = filter.process(line);
   114                 if(line!=null)
   107                 if(line!=null)
   115                     w.println(line);
   108                     w.println(line);
   116                 lineNumber++;
   109                 lineNumber++;
   117             }
   110             }
   118         } catch( ParseException e ) {
   111         } catch( ParseException e ) {
   119             throw new IOException("unable to process "+source+" line:"+lineNumber+"\n"+e.getMessage());
   112             throw new IOException("unable to process "+source+" line:"+lineNumber+"\n"+e.getMessage());
   120         }
   113         }
   121 
       
   122         w.close();
       
   123         r.close();
       
   124     }
   114     }
   125 
   115 
   126     /**
   116     /**
   127      * Creates a {@link LineFilter}.
   117      * Creates a {@link LineFilter}.
   128      * <p>
   118      * <p>
   233         }
   223         }
   234 
   224 
   235         protected JClass substituteParams(JTypeVar[] variables, List<JClass> bindings) {
   225         protected JClass substituteParams(JTypeVar[] variables, List<JClass> bindings) {
   236             return this;
   226             return this;
   237         }
   227         }
   238     };
   228     }
       
   229 
       
   230     static class ResourceLoader {
       
   231         Class<?> loadingClass;
       
   232         String shortName;
       
   233 
       
   234         ResourceLoader(String shortName, Class<?> loadingClass) {
       
   235             this.loadingClass = loadingClass;
       
   236             this.shortName = shortName;
       
   237         }
       
   238 
       
   239         InputStream getResourceAsStream() {
       
   240             // some people didn't like our jars to contain files with .java extension,
       
   241             // so when we build jars, we'' use ".java_". But when we run from the workspace,
       
   242             // we want the original source code to be used, so we check both here.
       
   243             // see bug 6211503.
       
   244             InputStream stream = loadingClass.getResourceAsStream(shortName + ".java");
       
   245             if (stream == null) {
       
   246                 stream = loadingClass.getResourceAsStream(shortName + ".java_");
       
   247             }
       
   248             if (stream == null) {
       
   249                 throw new InternalError("Unable to load source code of " + loadingClass.getName() + " as a resource");
       
   250             }
       
   251             return stream;
       
   252         }
       
   253 
       
   254     }
       
   255 
   239 }
   256 }