GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
ProcAddressJavaMethodBindingEmitter.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010-2023 JogAmp Community. All rights reserved.
3 * Copyright (c) 2003-2005 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.procaddress;
41
42import com.jogamp.gluegen.MethodBinding;
43import com.jogamp.gluegen.FunctionEmitter;
44import com.jogamp.gluegen.JavaMethodBindingEmitter;
45
46import java.io.*;
47
48/** A specialization of JavaMethodBindingEmitter with knowledge of how
49to call through a function pointer. */
51
52 protected boolean callThroughProcAddress;
53 protected boolean changeNameAndArguments;
54
55 protected String getProcAddressTableExpr;
57
60
61 super(methodToWrap);
62
63 this.callThroughProcAddress = callThroughProcAddress;
64 this.getProcAddressTableExpr = getProcAddressTableExpr;
65 this.changeNameAndArguments = changeNameAndArguments;
66 this.emitter = emitter;
67
70 }
71
72 if (methodToWrap.getBinding().hasContainingType()) {
73 throw new IllegalArgumentException(
74 "Cannot create proc. address wrapper; method has containing type: \""
75 + methodToWrap.getBinding() + "\"");
76 }
77 }
78
80 this(methodToWrap, methodToWrap.callThroughProcAddress, methodToWrap.getProcAddressTableExpr,
81 methodToWrap.changeNameAndArguments, methodToWrap.emitter);
82 }
83
84 @Override
85 public String getImplName() {
86 final String res = super.getImplName();
88 return ProcAddressEmitter.WRAP_PREFIX + res;
89 }
90 return res;
91 }
92
93 @Override
94 protected int appendArguments(final StringBuilder buf) {
95 int numEmitted = super.appendArguments(buf);
98 if (numEmitted > 0) {
99 buf.append(", ");
100 }
101
102 buf.append("long procAddress");
103 ++numEmitted;
104 }
105 }
106 return numEmitted;
107 }
108
109 @Override
110 protected String getNativeImplMethodName() {
111 final String name = super.getNativeImplMethodName();
113 return ProcAddressEmitter.WRAP_PREFIX + name;
114 }
115 return name;
116 }
117
118 @Override
119 protected void emitPreCallSetup(final MethodBinding binding) {
120 super.emitPreCallSetup(binding);
121
123 final String procAddressVariable = ProcAddressEmitter.PROCADDRESS_VAR_PREFIX + binding.getNativeName();
124 unit.emitln(" final long __addr_ = " + getProcAddressTableExpr + "." + procAddressVariable + ";");
125 unit.emitln(" if (__addr_ == 0) {");
126 unit.emitf(" throw new %s(String.format(\"Method \\\"%%s\\\" not available\", \"%s\"));%n",
128 unit.emitln(" }");
129 }
130 }
131
132 @Override
134 int numEmitted = super.emitCallArguments(binding);
136 if (numEmitted > 0) {
137 unit.emit(", ");
138 }
139 unit.emit("__addr_");
140 ++numEmitted;
141 }
142
143 return numEmitted;
144 }
145
146 /** This class emits the comment for the wrapper method */
148
149 @Override
150 protected void emitBeginning(final FunctionEmitter methodEmitter, final PrintWriter writer) {
151 writer.print("Entry point (through function pointer) to C language function: <br> ");
152 }
153 }
154}
void emit(final String s)
Definition: CodeUnit.java:81
void emitf(final String s, final Object... args)
Definition: CodeUnit.java:84
Generic function emitter to produce C (JNI) or Java code stubs to its CodeUnit, invoking a native fun...
void setCommentEmitter(final CommentEmitter cEmitter)
Set the object that will emit the comment for this function.
Class that emits a generic comment for JavaMethodBindingEmitters; the comment includes the C signatur...
Emits the Java-side component (interface and.or implementation) of the Java<->C JNI binding to its Co...
Represents the binding of a C function to a Java method.
String getName()
Returns the FunctionSymbol's current aliased API name.
boolean hasContainingType()
Indicates whether this MethodBinding is for a function pointer contained in a struct,...
String getNativeName()
Returns the FunctionSymbol's name for the native function which is the original C API name per defaul...
A subclass of JavaEmitter that modifies the normal emission of C and Java code to allow dynamic looku...
A specialization of JavaMethodBindingEmitter with knowledge of how to call through a function pointer...
ProcAddressJavaMethodBindingEmitter(final JavaMethodBindingEmitter methodToWrap, final boolean callThroughProcAddress, final String getProcAddressTableExpr, final boolean changeNameAndArguments, final ProcAddressEmitter emitter)
int appendArguments(final StringBuilder buf)
Returns the number of arguments emitted.
ProcAddressJavaMethodBindingEmitter(final ProcAddressJavaMethodBindingEmitter methodToWrap)