1/*
2 * Copyright (c) 2015, 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/*
25 * Native test for ToStringInInterfaceTest.
26 */
27
28#include "jni.h"
29
30#define checkException(env) if ((*env)->ExceptionCheck(env)) { return; }
31
32jstring callStringMethod(JNIEnv* env, jobject jobj, jmethodID id, ...)
33{
34    jstring value;
35
36    va_list ap;
37    va_start(ap, id);
38    value = (jstring)(*env)->CallObjectMethodV(env, jobj, id, ap);
39    va_end(ap);
40    return value;
41}
42
43JNIEXPORT void JNICALL Java_ToStringTest_nTest(JNIEnv* env, jclass jclazz)
44{
45    jclass classOfInterfaceWithToString;
46    jclass classOfImplementationOfWithToString;
47    jmethodID constructorOfImplementationOfWithToString;
48    jobject instanceOfImplementationOfWithToString;
49    jmethodID toStringOfInterfaceWithToString;
50    jmethodID toStringOfImplementationOfWithToString;
51    jstring jstr;
52    const char *chars;
53
54    classOfInterfaceWithToString = (*env)->FindClass(env, "InterfaceWithToString");
55    checkException(env);
56    classOfImplementationOfWithToString = (*env)->FindClass(env, "ImplementationOfWithToString");
57    checkException(env);
58
59    constructorOfImplementationOfWithToString = (*env)->GetMethodID(env, classOfImplementationOfWithToString, "<init>", "()V");
60    checkException(env);
61
62    instanceOfImplementationOfWithToString = (*env)->NewObject(env, classOfImplementationOfWithToString, constructorOfImplementationOfWithToString);
63    checkException(env);
64
65    toStringOfInterfaceWithToString = (*env)->GetMethodID(env, classOfInterfaceWithToString, "toString", "()Ljava/lang/String;");
66    checkException(env);
67
68    toStringOfImplementationOfWithToString = (*env)->GetMethodID(env, classOfImplementationOfWithToString, "toString", "()Ljava/lang/String;");
69    checkException(env);
70
71    jstr = callStringMethod(env, instanceOfImplementationOfWithToString, toStringOfImplementationOfWithToString);
72    checkException(env);
73
74    chars = (*env)->GetStringUTFChars(env, jstr, NULL);
75    (*env)->ReleaseStringUTFChars(env, jstr, chars);
76
77    jstr = callStringMethod(env, instanceOfImplementationOfWithToString, toStringOfInterfaceWithToString);
78    checkException(env);
79
80    chars = (*env)->GetStringUTFChars(env, jstr, NULL);
81    (*env)->ReleaseStringUTFChars(env, jstr, chars);
82}
83