* NOT PART OF TBOX BUILD *

r=idk@eng.sun.com
 - generate compilable java interfaces when identifiers
   in idls coincide with java keywords
 - correctly deal with methods which names in idls
   coincide with  some Object class methods


git-svn-id: svn://10.0.0.236/trunk@80840 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
sdv%sparc.spb.su
2000-10-10 19:41:27 +00:00
parent 5c3fba0bbc
commit 0d218094b7
4 changed files with 138 additions and 28 deletions

View File

@@ -30,6 +30,7 @@ public class InterfaceRegistry {
private static String IID_STRING = "IID";
private static Hashtable interfaces = null;
private static Hashtable iMethods = null;
private static Hashtable keywords = null;
private static boolean debug = true;
private InterfaceRegistry() {
@@ -43,6 +44,14 @@ public class InterfaceRegistry {
register(cl);
}
public static void register(String name) {
try {
register(Class.forName(name));
} catch (Exception e) {
debug(e.toString());
}
}
public static void register(Class cl) {
if (cl == null) {
return;
@@ -53,6 +62,12 @@ public class InterfaceRegistry {
if (iMethods == null) {
iMethods = new Hashtable();
}
if (keywords == null) {
keywords = new Hashtable(javaKeywords.length);
for (int i = 0; i < javaKeywords.length; i++) {
keywords.put(javaKeywords[i], javaKeywords[i]);
}
}
if (!cl.isInterface()) {
Class[] ifaces = cl.getInterfaces();
for (int i = 0; i < ifaces.length; i++) {
@@ -84,7 +99,7 @@ public class InterfaceRegistry {
} while (ifaces.length == 1);
for (int j = methodNames.length - 1; j >= 0; j--) {
rmethods[j] = (Method)mhash.get(methodNames[j]);
rmethods[j] = (Method)mhash.get(subscriptMethodName(methodNames[j]));
}
interfaces.put(iid, cl);
@@ -134,22 +149,23 @@ public class InterfaceRegistry {
public static int getIndexByMethod(Method method, IID iid) {
int result = -1;
MethodArray m = (MethodArray)iMethods.get(iid);
String methodName = method.getName();
if (m != null && m.methods !=null) {
if (m != null && m.methods != null) {
for (int i = 0; i < m.methods.length; i++) {
if (m.methods[i] != null) {
if (methodName.equals(m.methods[i].getName())) {
result = i;
break;
}
if (method.equals(m.methods[i])) {
result = i;
break;
}
}
}
return result;
}
private static String subscriptMethodName(String str) {
if (keywords.get(str) != null) {
return str + "_";
}
return str;
}
// methods for debugging
@@ -185,6 +201,19 @@ public class InterfaceRegistry {
}
}
private static String[] javaKeywords = {
"abstract", "default", "if" , "private" , "this" ,
"boolean" , "do" , "implements", "protected" , "throw" ,
"break" , "double" , "import", "public" , "throws" ,
"byte" , "else" , "instanceof", "return" , "transient",
"case" , "extends", "int" , "short" , "try" ,
"catch" , "final" , "interface" , "static" , "void" ,
"char" , "finally", "long" , "strictfp" , "volatile" ,
"class" , "float" , "native" , "super" , "while" ,
"const" , "for" , "new" , "switch" ,
"continue", "goto" , "package" , "synchronized",
// non-final method names of Object class
"toString", "clone" , "finalize" , "hashCode" , "equals"};
}
class MethodArray {

View File

@@ -33,14 +33,28 @@ class ProxyHandler implements InvocationHandler {
Method method,
Object[] args) throws Throwable {
System.out.println("--[java]ProxyHandler.invoke "+method);
/*
*if ("toString".equals(method.getName()) &&
* (method.getParameterTypes()).length == 0) { //it is String toString() method
* return "ProxyObject@{oid = "+oid+" iid = "+iid+"}";
*}
*/
return Utilities.callMethod(oid, method, iid, orb, args);
String str = method.getName();
if (str.equals("toString")) {
return "ProxyObject@{oid = "+oid+" iid = "+iid+"}";
} else if (str.equals("clone")) {
throw new java.lang.CloneNotSupportedException();
} else if (str.equals("finalize")) {
finalize();
} else if (str.equals("equals")) {
if (args[0] instanceof ProxyHandler) {
ProxyHandler p = (ProxyHandler)args[0];
return new Boolean((oid == p.oid) &&
iid.equals(p.iid) &&
(orb == p.orb));
}
} else if (str.equals("hashCode")) {
return new Integer(hashCode());
} else {
return Utilities.callMethod(oid, method, iid, orb, args);
}
return null;
}
private long oid;
private IID iid;
private long orb;

View File

@@ -25,7 +25,7 @@ public interface nsISupportsString extends nsISupports
public void setData(String value);
/* string toString (); */
public String toString();
public String toString_();
/* void setDataWithLength (in unsigned long length, [size_is (length)] in string data); */
public void setDataWithLength(int length, String data);

View File

@@ -36,13 +36,29 @@
* source decompiled from state->tree.
*/
void write_comment(TreeState *state);
char* subscriptIdentifier(TreeState *state, char *str);
char* subscriptMethodName(TreeState *state, char *str);
struct java_priv_data {
GHashTable *typedefTable;
GHashTable *keywords;
FILE *file;
};
char* javaKeywords[] = {
"abstract", "default", "if" , "private" , "this" ,
"boolean" , "do" , "implements", "protected" , "throw" ,
"break" , "double" , "import", "public" , "throws" ,
"byte" , "else" , "instanceof", "return" , "transient",
"case" , "extends", "int" , "short" , "try" ,
"catch" , "final" , "interface" , "static" , "void" ,
"char" , "finally", "long" , "strictfp" , "volatile" ,
"class" , "float" , "native" , "super" , "while" ,
"const" , "for" , "new" , "switch" ,
"continue", "goto" , "package" , "synchronized"};
#define TYPEDEFS(state) (((struct java_priv_data *)state->priv)->typedefTable)
#define KEYWORDS(state) (((struct java_priv_data *)state->priv)->keywords)
#define FILENAME(state) (((struct java_priv_data *)state->priv)->file)
static gboolean
@@ -69,6 +85,7 @@ write_classname_iid_define(FILE *file, const char *className)
static gboolean
java_prolog(TreeState *state)
{
int len, i;
state->priv = calloc(1, sizeof(struct java_priv_data));
if (!state->priv)
return FALSE;
@@ -79,7 +96,19 @@ java_prolog(TreeState *state)
free(state->priv);
return FALSE;
}
KEYWORDS(state) = 0;
KEYWORDS(state) = g_hash_table_new(g_str_hash, g_str_equal);
if (!KEYWORDS(state)) {
g_hash_table_destroy(TYPEDEFS(state));
free(state->priv);
return FALSE;
}
len = sizeof(javaKeywords)/sizeof(*javaKeywords);
for (i = 0; i < len; i++) {
g_hash_table_insert(KEYWORDS(state),
javaKeywords[i],
javaKeywords[i]);
}
return TRUE;
}
@@ -88,6 +117,7 @@ java_epilog(TreeState *state)
{
/* points to other elements of the tree, so just destroy the table */
g_hash_table_destroy(TYPEDEFS(state));
g_hash_table_destroy(KEYWORDS(state));
free(state->priv);
state->priv = NULL;
@@ -121,7 +151,8 @@ interface_declaration(TreeState *state)
char *outname;
IDL_tree interface = state->tree;
IDL_tree iterator = NULL;
char *interface_name = IDL_IDENT(IDL_INTERFACE(interface).ident).str;
char *interface_name =
subscriptIdentifier(state, IDL_IDENT(IDL_INTERFACE(interface).ident).str);
const char *iid = NULL;
GSList *doc_comments = IDL_IDENT(IDL_INTERFACE(interface).ident).comments;
char *prefix = IDL_GENTREE(IDL_NS(state->ns).current)._cur_prefix;
@@ -392,7 +423,7 @@ xpcom_to_java_type (TreeState *state)
state->tree = orig_tree;
}
else {
fputs(ident_str, FILENAME(state));
fputs(subscriptIdentifier(state, ident_str), FILENAME(state));
}
}
}
@@ -446,7 +477,9 @@ xpcom_to_java_param(TreeState *state)
fputc(' ', FILENAME(state));
fputs(IDL_IDENT(IDL_PARAM_DCL(param).simple_declarator).str, FILENAME(state));
fputs(subscriptIdentifier(state,
IDL_IDENT(IDL_PARAM_DCL(param).simple_declarator).str),
FILENAME(state));
return TRUE;
}
@@ -484,7 +517,10 @@ method_declaration(TreeState *state)
(IDL_tree_property_get(method->ident, "noscript") != NULL);
IDL_tree iterator = NULL;
IDL_tree retval_param = NULL;
const char *method_name = IDL_IDENT(method->ident).str;
char *method_name =
g_strdup_printf("%c%s",
tolower(IDL_IDENT(method->ident).str[0]),
IDL_IDENT(method->ident).str + 1);
if (doc_comments != NULL) {
fputs(" ", FILENAME(state));
@@ -559,7 +595,8 @@ method_declaration(TreeState *state)
/*
* Write method name
*/
fprintf(FILENAME(state), " %c%s(", tolower(method_name[0]), method_name + 1);
fprintf(FILENAME(state), " %s(", subscriptMethodName(state, method_name));
free(method_name);
/*
* Write parameters
@@ -667,10 +704,14 @@ constant_declaration(TreeState *state)
default:
/*
* Whoops, it's some other kind of number
*/
*/
success = FALSE;
}
} else {
IDL_tree_error(state->tree,
"const declaration \'%s\' must be of type short or long",
name);
return FALSE;
}
if (doc_comments != NULL) {
@@ -683,8 +724,9 @@ constant_declaration(TreeState *state)
write_comment(state);
fprintf(FILENAME(state), " public static final %s %s = %d;\n",
(isshort ? "short" : "int"),
name, (int) IDL_INTEGER(declaration->const_exp).value);
(isshort ? "short" : "int"),
subscriptIdentifier(state, name),
(int) IDL_INTEGER(declaration->const_exp).value);
} else {
XPIDL_WARNING((state->tree, IDL_WARNING1,
"A constant \"%s\" was not of type short or long."
@@ -857,3 +899,28 @@ void write_comment(TreeState *state)
IDLF_OUTPUT_PROPERTIES);
fputs(" */\n", FILENAME(state));
}
char* subscriptMethodName(TreeState *state, char *str)
{
char *sstr = NULL;
if (strcmp(str, "toString") &&
strcmp(str, "clone") &&
strcmp(str, "finalize") &&
strcmp(str, "equals") &&
strcmp(str, "hashCode")) {
return subscriptIdentifier(state, str);
}
sstr = g_strdup_printf("%s_", str);
return sstr;
}
char* subscriptIdentifier(TreeState *state, char *str)
{
char *sstr = NULL;
char *keyword = g_hash_table_lookup(KEYWORDS(state), str);
if (keyword) {
sstr = g_strdup_printf("%s_", keyword);
return sstr;
}
return str;
}