Skip to content

Commit

Permalink
[TIMOB-24684] Generate method metadata from implemented interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
janvennemann committed Jan 30, 2018
1 parent 7fa54ff commit 6a4265b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 51 deletions.
71 changes: 48 additions & 23 deletions android/hooks/metabase/src/JavaMetabaseGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,41 @@ private static void asJSON(JavaClass javaClass, JSONWriter writer)
writer.key("methods");
JSONObject methodsJSON = new JSONObject();
Method methods[] = javaClass.getMethods();
generateMethodMetadata(methods, methodsJSON);

if (javaClass.isInterface()) {
generateMethodsFromImplementedInterfaces(javaClass, methodsJSON);
}

writer.value(methodsJSON);

// properties
writer.key("properties");
JSONObject propertiesJSON = new JSONObject();
Field fields[] = javaClass.getFields();
for (Field field : fields)
{
// Skip private and package-level fields entirely to save space
// since we don't want them for now
if (!field.isPublic() && !field.isProtected()) {
continue;
}

JSONObject fieldJSON = new JSONObject();
fieldJSON.put("name", field.getName());
fieldJSON.put("attributes", addAttributes(field));
fieldJSON.put("type", field.getType());
fieldJSON.put("value", field.getConstantValue());
fieldJSON.put("metatype", field.getConstantValue() != null ? "constant" : "field");
fieldJSON.put("attributes", addAttributes(field));
fieldJSON.put("instance",!field.isStatic());
propertiesJSON.put(field.getName(), fieldJSON);
}
writer.value(propertiesJSON);
}

private static void generateMethodMetadata(Method methods[], JSONObject methodsJSON)
{
for (Method method : methods)
{
// Skip private and package-level methods entirely to save space
Expand All @@ -211,9 +246,9 @@ private static void asJSON(JavaClass javaClass, JSONWriter writer)

JSONObject methodJSON = new JSONObject();
methodJSON.put("attributes", addAttributes(method));
methodJSON.put("signature",method.getSignature());
methodJSON.put("instance",!method.isStatic());
methodJSON.put("name",method.getName());
methodJSON.put("signature", method.getSignature());
methodJSON.put("instance", !method.isStatic());
methodJSON.put("name", method.getName());

JSONArray overloads;
if (methodsJSON.has(method.getName()))
Expand Down Expand Up @@ -246,30 +281,20 @@ private static void asJSON(JavaClass javaClass, JSONWriter writer)
}
methodJSON.put("exceptions", exceptionsJSON);
}
writer.value(methodsJSON);
}

// properties
writer.key("properties");
JSONObject propertiesJSON = new JSONObject();
Field fields[] = javaClass.getFields();
for (Field field : fields)
{
// Skip private and package-level fields entirely to save space
// since we don't want them for now
if (!field.isPublic() && !field.isProtected()) {
private static void generateMethodsFromImplementedInterfaces(JavaClass javaClass, JSONObject methodssJSON)
{
String[] implementedInterfacesNames = javaClass.getInterfaceNames();
for (String interfaceName : implementedInterfacesNames) {
JavaClass interfaceClass = repo.findClass(interfaceName);
if (interfaceClass == null) {
continue;
}

JSONObject fieldJSON = new JSONObject();
fieldJSON.put("name", field.getName());
fieldJSON.put("attributes", addAttributes(field));
fieldJSON.put("type", field.getType());
fieldJSON.put("value", field.getConstantValue());
fieldJSON.put("metatype", field.getConstantValue() != null ? "constant" : "field");
fieldJSON.put("attributes", addAttributes(field));
fieldJSON.put("instance",!field.isStatic());
propertiesJSON.put(field.getName(), fieldJSON);
Method methods[] = javaClass.getMethods();
generateMethodsFromImplementedInterfaces(interfaceClass, methodssJSON);
generateMethodMetadata(methods, methodssJSON);
}
writer.value(propertiesJSON);
}
}
17 changes: 1 addition & 16 deletions android/hooks/metabase/templates/class.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,7 @@ Object.defineProperty(<%= sanitizedName %>.prototype, 'super', {
},
enumerable: true
});
<%
} else {
-%>
<%= sanitizedName %>.toString = function() {
return '[object ' + this.className + ']';
};
<%= sanitizedName %>.prototype.toString = function() {
if (this._hasPointer) {
return '[object ' + this.className + ']';
}
return null;
};
<%
}
-%>
<% } -%>

<%= sanitizedName %>.className = '<%= classDefinition.name %>';
<%= sanitizedName %>.prototype.className = '<%= classDefinition.name %>';
Expand Down
24 changes: 12 additions & 12 deletions android/hooks/metabase/templates/interface.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,19 @@ var <%= sanitizedName %> = function() {
global.<%= baseName %> = <%= sanitizedName %>;
<% } -%>

<%= sanitizedName %>.toString = function() {
return "[object " + this.className + "]";
};

<%= sanitizedName %>.prototype.toString = function() {
if (!this._hasPointer) return null;
<% if (classDefinition.superClass) { -%>
var SuperClass = require('./<%- classDefinition.superClass %>');
<%= sanitizedName %>.prototype = Object.create(SuperClass.prototype);
<%= sanitizedName %>.prototype.constructor = <%= sanitizedName %>;
var result = this.$native.callNativeFunction({
func: 'toString',
instanceMethod: true
});
return result;
};
Object.defineProperty(<%= sanitizedName %>.prototype, 'super', {
get: function() {
if (!this._hasPointer) return null;
return new <%= sanitizedName %>(this.$native.super);
},
enumerable: true
});
<% } -%>

<%= sanitizedName %>.isInstanceOf = function (self, cls) {
if (typeof cls !== "function" || typeof self !== "function") { return false; }
Expand Down

0 comments on commit 6a4265b

Please sign in to comment.