1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   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 
  24 package com.sun.tools.jextract;
  25 
  26 import java.foreign.layout.Function;
  27 import java.foreign.layout.Layout;
  28 import java.util.function.BiFunction;
  29 import javax.lang.model.SourceVersion;
  30 import jdk.internal.clang.Type;
  31 import com.sun.tools.jextract.tree.LayoutUtils;
  32 
  33 /**
  34  * General utility functions
  35  */
  36 public class Utils {
  37     public static void validSimpleIdentifier(String name) {
  38         int length = name.length();
  39         if (length == 0) {
  40             throw new IllegalArgumentException();
  41         }
  42 
  43         int ch = name.codePointAt(0);
  44         if (length == 1 && ch == '_') {
  45             throw new IllegalArgumentException("'_' is no longer valid identifier.");
  46         }
  47 
  48         if (!Character.isJavaIdentifierStart(ch)) {
  49             throw new IllegalArgumentException("Invalid start character for an identifier: " + ch);
  50         }
  51 
  52         for (int i = 1; i < length; i++) {
  53             ch = name.codePointAt(i);
  54             if (!Character.isJavaIdentifierPart(ch)) {
  55                 throw new IllegalArgumentException("Invalid character for an identifier: " + ch);
  56             }
  57         }
  58     }
  59 
  60     public static void validPackageName(String name) {
  61         if (name.isEmpty()) {
  62             throw new IllegalArgumentException();
  63         }
  64         int idx = name.lastIndexOf('.');
  65         if (idx == -1) {
  66            validSimpleIdentifier(name);
  67         } else {
  68             validSimpleIdentifier(name.substring(idx + 1));
  69             validPackageName(name.substring(0, idx));
  70         }
  71     }
  72 
  73     public static String toJavaIdentifier(String str) {
  74         final int size = str.length();
  75         StringBuilder sb = new StringBuilder(size);
  76         if (! Character.isJavaIdentifierStart(str.charAt(0))) {
  77             sb.append('_');
  78         }
  79         for (int i = 0; i < size; i++) {
  80             char ch = str.charAt(i);
  81             if (Character.isJavaIdentifierPart(ch)) {
  82                 sb.append(ch);
  83             } else {
  84                 sb.append('_');
  85             }
  86         }
  87         return sb.toString();
  88     }
  89 
  90     public static String toClassName(String cname) {
  91         StringBuilder sb = new StringBuilder(cname.length());
  92         cname = toJavaIdentifier(cname);
  93         sb.append(cname);
  94         if (SourceVersion.isKeyword(cname)) {
  95             sb.append("$");
  96         }
  97         return sb.toString();
  98     }
  99 
 100     public static String toInternalName(String pkg, String name, String... nested) {
 101         if ((pkg == null || pkg.isEmpty()) && nested == null) {
 102             return name;
 103         }
 104 
 105         StringBuilder sb = new StringBuilder();
 106         if (pkg != null && ! pkg.isEmpty()) {
 107             sb.append(pkg.replace('.', '/'));
 108             if (sb.charAt(sb.length() - 1) != '/') {
 109                 sb.append('/');
 110             }
 111         }
 112         sb.append(name);
 113         for (String n: nested) {
 114             sb.append('$');
 115             sb.append(n);
 116         }
 117         return sb.toString();
 118     }
 119 
 120     public static String getName(Type type) {
 121         return LayoutUtils.getName(type);
 122     }
 123 
















































































 124     public static Layout getLayout(Type type) {
 125         return LayoutUtils.getLayout(type);
 126     }
 127 
 128     public static Function getFunction(Type type) {
 129         return LayoutUtils.getFunction(type);
 130     }
 131 
 132     public static Class<?> unboxIfNeeded(Class<?> clazz) {
 133         if (clazz == Boolean.class) {
 134             return boolean.class;
 135         } else if (clazz == Void.class) {
 136             return void.class;
 137         } else if (clazz == Byte.class) {
 138             return byte.class;
 139         } else if (clazz == Character.class) {
 140             return char.class;
 141         } else if (clazz == Short.class) {
 142             return short.class;
 143         } else if (clazz == Integer.class) {
 144             return int.class;
 145         } else if (clazz == Long.class) {
 146             return long.class;
 147         } else if (clazz == Float.class) {
 148             return float.class;
 149         } else if (clazz == Double.class) {
 150             return double.class;
 151         } else {
 152             return clazz;
 153         }
 154     }
 155 }
--- EOF ---