< prev index next >

src/jdk.jextract/share/classes/com/sun/tools/jextract/JavaSourceFactoryExt.java

Print this page




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package com.sun.tools.jextract;
  24 
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.util.ArrayList;

  28 import java.util.List;

  29 import java.util.Set;
  30 import java.util.logging.Level;
  31 
  32 import com.sun.tools.jextract.parser.MacroParser;
  33 import com.sun.tools.jextract.tree.Tree;
  34 import com.sun.tools.jextract.tree.EnumTree;
  35 import com.sun.tools.jextract.tree.FieldTree;
  36 import com.sun.tools.jextract.tree.FunctionTree;
  37 import com.sun.tools.jextract.tree.MacroTree;
  38 import com.sun.tools.jextract.tree.VarTree;
  39 
  40 /**
  41  * This extended factory generates a class with only static methods and fields. A native
  42  * library interface instance (of the given header file) is kept as a static private field.
  43  * One static method is generated for every library interface method. Enum and macro constants
  44  * are mapped to static final fields. By importing the "static forwarder" class, the user code
  45  * looks more or less like C code. Libraries.bind and header interface usage is hidden.
  46  */
  47 final class JavaSourceFactoryExt extends JavaSourceFactory {
  48     // field name for the header interface instance.
  49     private static final String STATICS_LIBRARY_FIELD_NAME = "_theLibrary";
  50     private final JavaSourceBuilderExt header_jsb;
  51     private final List<JavaSourceBuilderExt> enums;
  52 
  53     JavaSourceFactoryExt(Context ctx, HeaderFile header) {
  54         super(ctx, header);
  55         log.print(Level.INFO, () -> "Instantiate StaticForwarderGenerator for " + header.path);
  56         header_jsb = new JavaSourceBuilderExt();
  57         enums = new ArrayList<>();
  58     }
  59 
  60     @Override
  61     public void generate(List<Tree> decls) {
  62         header_jsb.addPackagePrefix(headerFile.pkgName);
  63         String ifaceClsName = headerFile.headerClsName;
  64         String forwarderName = headerFile.staticForwarderClsName;
  65         header_jsb.classBegin(forwarderName, false);
  66 
  67         header_jsb.addLibraryField(ifaceClsName, STATICS_LIBRARY_FIELD_NAME);
  68         header_jsb.emitScopeAccessor(STATICS_LIBRARY_FIELD_NAME);
  69 
  70         super.generate(decls);
  71         enums.forEach(header_jsb::addNestedType);
  72 
  73         header_jsb.classEnd();
  74         String src = header_jsb.build();

  75         try {
  76             Path srcPath = srcDir.resolve(forwarderName + ".java");
  77             Files.write(srcPath, List.of(src));
  78         } catch (Exception ex) {
  79             handleException(ex);
  80         }
  81     }



  82 
  83     @Override
  84     public Boolean visitVar(VarTree varTree, JType jt) {
  85         if (super.visitVar(varTree, jt)) {
  86             String fieldName = varTree.name();
  87             assert !fieldName.isEmpty();
  88             header_jsb.emitStaticForwarder(varTree, jt, STATICS_LIBRARY_FIELD_NAME);
  89             return true;
  90         } else {
  91             return false;
  92         }
  93     }
  94 
  95     @Override
  96     public Boolean visitEnum(EnumTree enumTree, JType jt) {
  97         if (super.visitEnum(enumTree, jt)) {
  98             if (enumTree.name().isEmpty()) {
  99                 enumTree.constants().forEach(constant -> addConstant(header_jsb, constant.name(),
 100                     headerFile.dictionary().lookup(constant.type()),
 101                     constant.enumConstant().get()));




   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package com.sun.tools.jextract;
  24 
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.util.ArrayList;
  28 import java.util.HashMap;
  29 import java.util.List;
  30 import java.util.Map;
  31 import java.util.Set;
  32 import java.util.logging.Level;
  33 
  34 import com.sun.tools.jextract.parser.MacroParser;
  35 import com.sun.tools.jextract.tree.Tree;
  36 import com.sun.tools.jextract.tree.EnumTree;
  37 import com.sun.tools.jextract.tree.FieldTree;
  38 import com.sun.tools.jextract.tree.FunctionTree;
  39 import com.sun.tools.jextract.tree.MacroTree;
  40 import com.sun.tools.jextract.tree.VarTree;
  41 
  42 /**
  43  * This extended factory generates a class with only static methods and fields. A native
  44  * library interface instance (of the given header file) is kept as a static private field.
  45  * One static method is generated for every library interface method. Enum and macro constants
  46  * are mapped to static final fields. By importing the "static forwarder" class, the user code
  47  * looks more or less like C code. Libraries.bind and header interface usage is hidden.
  48  */
  49 final class JavaSourceFactoryExt extends JavaSourceFactory {
  50     // field name for the header interface instance.
  51     private static final String STATICS_LIBRARY_FIELD_NAME = "_theLibrary";
  52     private final JavaSourceBuilderExt header_jsb;
  53     private final List<JavaSourceBuilderExt> enums;
  54 
  55     JavaSourceFactoryExt(Context ctx, HeaderFile header) {
  56         super(ctx, header);
  57         log.print(Level.INFO, () -> "Instantiate StaticForwarderGenerator for " + header.path);
  58         header_jsb = new JavaSourceBuilderExt();
  59         enums = new ArrayList<>();
  60     }
  61 
  62     @Override
  63     public Map<String, String> generate(List<Tree> decls) {
  64         header_jsb.addPackagePrefix(headerFile.pkgName);
  65         String ifaceClsName = headerFile.headerClsName;
  66         String forwarderName = headerFile.staticForwarderClsName;
  67         header_jsb.classBegin(forwarderName, false);
  68 
  69         header_jsb.addLibraryField(ifaceClsName, STATICS_LIBRARY_FIELD_NAME);
  70         header_jsb.emitScopeAccessor(STATICS_LIBRARY_FIELD_NAME);
  71 
  72         Map<String, String> srcMap = super.generate(decls);
  73         enums.forEach(header_jsb::addNestedType);
  74 
  75         header_jsb.classEnd();
  76         String src = header_jsb.build();
  77         if (srcDir != null) {
  78             try {
  79                 Path srcPath = srcDir.resolve(forwarderName + ".java");
  80                 Files.write(srcPath, List.of(src));
  81             } catch (Exception ex) {
  82                 handleException(ex);
  83             }
  84         }
  85         srcMap.put(headerFile.pkgName + "." + forwarderName, src);
  86         return srcMap;
  87     }
  88 
  89     @Override
  90     public Boolean visitVar(VarTree varTree, JType jt) {
  91         if (super.visitVar(varTree, jt)) {
  92             String fieldName = varTree.name();
  93             assert !fieldName.isEmpty();
  94             header_jsb.emitStaticForwarder(varTree, jt, STATICS_LIBRARY_FIELD_NAME);
  95             return true;
  96         } else {
  97             return false;
  98         }
  99     }
 100 
 101     @Override
 102     public Boolean visitEnum(EnumTree enumTree, JType jt) {
 103         if (super.visitEnum(enumTree, jt)) {
 104             if (enumTree.name().isEmpty()) {
 105                 enumTree.constants().forEach(constant -> addConstant(header_jsb, constant.name(),
 106                     headerFile.dictionary().lookup(constant.type()),
 107                     constant.enumConstant().get()));


< prev index next >