GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
CodeGenUtils.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2023 JogAmp Community. All rights reserved.
3 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use
34 * in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 *
37 * Sun gratefully acknowledges that this software was originally authored
38 * and developed by Kenneth Bradley Russell and Christopher John Kline.
39 */
40package com.jogamp.gluegen;
41
42import java.io.*;
43import java.util.*;
44
45public class CodeGenUtils {
46
47 /**
48 * Converts first letter to upper case.
49 */
50 public static String capitalizeString(final String string) {
51 return Character.toUpperCase(string.charAt(0)) + string.substring(1);
52 }
53
54 /**
55 * Converts first letter to lower case.
56 */
57 public static String decapitalizeString(final String string) {
58 return Character.toLowerCase(string.charAt(0)) + string.substring(1);
59 }
60
61 /**
62 * Given a java package name (e.g., "java.lang"), return the package as a
63 * directory path (i.e., "java/lang").
64 */
65 public static String packageAsPath(final String packageName) {
66 final String path = packageName.replace('.', File.separatorChar);
67 //System.out.println("Converted package [" + packageName + "] to path [" + path +"]");
68 return path;
69 }
70
71 /**
72 * @param generator the object that is emitting the autogenerated code. If
73 * null, the generator will not be mentioned in the warning message.
74 * @param customLine maybe null
75 */
76 public static void emitAutogeneratedWarning(final PrintWriter w, final Object generator, final String customLine) {
77 w.print("/* !---- DO NOT EDIT: This file autogenerated ");
78 if (generator != null) {
79 w.print("by ");
80 w.print(packageAsPath(generator.getClass().getName()));
81 w.print(".java ");
82 }
83 w.print("on ");
84 w.print((new Date()).toString());
85 w.println(" ----! */");
86 if( null != customLine && customLine.length() > 0 ) {
87 w.print("/* !---- ");
88 w.print(customLine);
89 w.println(" ----! */");
90 }
91 w.println();
92 }
93
94 /**
95 * Emit the opening headers for one java class/interface file.
96 */
97 public static void emitJavaHeaders(final PrintWriter w,
98 final String packageName,
99 final String className,
100 final boolean isClassNotInterface,
101 final List<String> imports,
102 final String[] accessModifiers,
103 final String[] interfaces,
104 final String classExtended,
105 final EmissionCallback classDocComment) throws IOException {
106 w.println("package " + packageName + ";");
107 w.println();
108
109 for (final String imp : imports) {
110 w.print("import ");
111 w.print(imp);
112 w.println(';');
113 }
114
115 w.println();
116
117 if (classDocComment != null) {
118 classDocComment.emit(w);
119 }
120
121 for (int i = 0; accessModifiers != null && i < accessModifiers.length; ++i) {
122 w.print(accessModifiers[i]);
123 w.print(' ');
124 }
125
126 if (isClassNotInterface) {
127 w.print("class ");
128 w.print(className);
129 w.print(' ');
130 if (classExtended != null) {
131 w.print("extends ");
132 w.print(classExtended);
133 w.print(' ');
134 }
135 } else {
136 if (classExtended != null) {
137 throw new IllegalArgumentException("Autogenerated interface class " + className + " cannot extend class " + classExtended);
138 }
139 w.print("interface ");
140 w.print(className);
141 w.print(' ');
142 }
143
144 for (int i = 0; interfaces != null && i < interfaces.length; ++i) {
145 if (i == 0) {
146 w.print(isClassNotInterface ? "implements " : "extends ");
147 }
148 w.print(interfaces[i]);
149 if (i < interfaces.length - 1) {
150 w.print(", ");
151 }
152 }
153
154 w.println('{');
155 }
156
157 //-----------------------------------------
158 /** A class that emits source code of some time when activated. */
159 public interface EmissionCallback {
160
161 /** Emit appropriate source code through the given writer. */
162 public void emit(PrintWriter output);
163 }
164}
static String decapitalizeString(final String string)
Converts first letter to lower case.
static void emitAutogeneratedWarning(final PrintWriter w, final Object generator, final String customLine)
static void emitJavaHeaders(final PrintWriter w, final String packageName, final String className, final boolean isClassNotInterface, final List< String > imports, final String[] accessModifiers, final String[] interfaces, final String classExtended, final EmissionCallback classDocComment)
Emit the opening headers for one java class/interface file.
static String packageAsPath(final String packageName)
Given a java package name (e.g., "java.lang"), return the package as a directory path (i....
static String capitalizeString(final String string)
Converts first letter to upper case.
A class that emits source code of some time when activated.
void emit(PrintWriter output)
Emit appropriate source code through the given writer.