Skip to content

Commit 6a5a1ab

Browse files
authored
Merge pull request #1371 from NativeScript/vmutafov/service-oncreate
Add internal onCreate method implementation when user has not overrid…
2 parents c25a0e5 + 25dfe25 commit 6a5a1ab

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/Generator.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,10 +463,18 @@ private void writeMethodsToWriter(Writer writer, GenericHierarchyView genericHie
463463
methodsWriter.writeGetInstanceMethod(normalizedClassName);
464464
}
465465

466+
if (!hasOverriddenOnCreateMethod(userImplementedMethods) && isServiceClass) {
467+
methodsWriter.writeInternalServiceOnCreateMethod();
468+
}
469+
466470
methodsWriter.writeInternalRuntimeHashCodeMethod();
467471
methodsWriter.writeInternalRuntimeEqualsMethod();
468472
}
469473

474+
private boolean hasOverriddenOnCreateMethod(List<String> overriddenMethods) {
475+
return overriddenMethods.contains("onCreate");
476+
}
477+
470478
private boolean areAllArgumentsAndReturnTypePublic(ReifiedJavaMethod method) {
471479
String returnType = BcelNamingUtil.resolveClassName(method.getReifiedReturnType());
472480
if (nonPublicNestedClasses.contains(returnType)) {

test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/generating/writing/MethodsWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ public interface MethodsWriter extends JavaCodeWriter {
88
void writeGetInstanceMethod(String className);
99
void writeInternalRuntimeEqualsMethod();
1010
void writeInternalRuntimeHashCodeMethod();
11+
void writeInternalServiceOnCreateMethod();
1112
}

test-app/build-tools/static-binding-generator/src/main/java/org/nativescript/staticbindinggenerator/generating/writing/impl/MethodsWriterImpl.java

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ public class MethodsWriterImpl implements MethodsWriter {
3535
private static final String INTERNAL_RUNTIME_EQUALS_METHOD_RETURN_STATEMENT = "return super.equals(other);";
3636
private static final String INTERNAL_RUNTIME_HASHCODE_METHOD_RETURN_STATEMENT = "return super.hashCode();";
3737
private static final String ANDROID_LOG_METHOD_CALL_STATEMENT = "android.util.Log.w(\"Warning\", \"NativeScript discarding uncaught JS exception!\");";
38-
private static final String RUNTIME_INIT_METHOD_CALL_STATEMENT = "com.tns.Runtime " + RUNTIME_VARIABLE_NAME + " = RuntimeHelper.initRuntime(this);";
38+
private static final String RUNTIME_INIT_METHOD_CALL_STATEMENT = "com.tns.Runtime " + RUNTIME_VARIABLE_NAME + " = com.tns.RuntimeHelper.initRuntime(this);";
3939
private static final String RUNTIME_RUN_METHOD_CALL_STATEMENT = RUNTIME_VARIABLE_NAME + ".run();";
4040
private static final String RUNTIME_INIT_INSTANCE_METHOD_CALL_STATEMENT = "com.tns.Runtime.initInstance(this);";
4141

42-
private static final String RUNTIME_IS_INITIALIZED_METHOD_CALL = "Runtime.isInitialized()";
42+
private static final String RUNTIME_IS_INITIALIZED_METHOD_CALL = "com.tns.Runtime.isInitialized()";
4343

4444
private static final String INTERNAL_RUNTIME_EQUALS_METHOD_SIGNATURE = "public boolean equals__super(java.lang.Object other)";
4545
private static final String INTERNAL_RUNTIME_HASHCODE_METHOD_SIGNATURE = "public int hashCode__super()";
46+
private static final String INTERNAL_SERVICES_ONCREATE_METHOD_SIGNATURE = "public void " + ON_CREATE_METHOD_NAME + "()";
4647

4748
private static final String RETURN_KEYWORD = "return";
4849
private static final String IF_STATEMENT_BEGINNING = "if";
@@ -73,8 +74,8 @@ public void writeMethod(ReifiedJavaMethod method) {
7374
if (isForApplicationClass) {
7475
writeRuntimeInitCallForApplication(method);
7576
writeRuntimeInitializationCheckForApplication(method);
76-
} else if (isForServiceClass) {
77-
writeRuntimeInitializationForService(method);
77+
} else if (isForServiceClass && method.getName().equals(ON_CREATE_METHOD_NAME)) {
78+
writeRuntimeInitializationForService();
7879
}
7980

8081
writeMethodBody(method);
@@ -157,22 +158,20 @@ private void writeRuntimeRunCallForApplication(ReifiedJavaMethod method) {
157158
}
158159
}
159160

160-
private void writeRuntimeInitializationForService(ReifiedJavaMethod method) {
161-
if (method.getName().equals(ON_CREATE_METHOD_NAME)) {
162-
writer.write(IF_STATEMENT_BEGINNING);
163-
writer.write(OPENING_ROUND_BRACKET_LITERAL);
164-
writer.write(NEGATE_LITERAL);
165-
writer.write(RUNTIME_IS_INITIALIZED_METHOD_CALL);
166-
writer.write(CLOSING_ROUND_BRACKET_LITERAL);
167-
writer.write(OPENING_CURLY_BRACKET_LITERAL);
161+
private void writeRuntimeInitializationForService() {
162+
writer.write(IF_STATEMENT_BEGINNING);
163+
writer.write(OPENING_ROUND_BRACKET_LITERAL);
164+
writer.write(NEGATE_LITERAL);
165+
writer.write(RUNTIME_IS_INITIALIZED_METHOD_CALL);
166+
writer.write(CLOSING_ROUND_BRACKET_LITERAL);
167+
writer.write(OPENING_CURLY_BRACKET_LITERAL);
168168

169-
writer.write(RUNTIME_INIT_METHOD_CALL_STATEMENT);
170-
writer.write(RUNTIME_RUN_METHOD_CALL_STATEMENT);
171-
172-
writer.write(CLOSING_CURLY_BRACKET_LITERAL);
169+
writer.write(RUNTIME_INIT_METHOD_CALL_STATEMENT);
170+
writer.write(RUNTIME_RUN_METHOD_CALL_STATEMENT);
173171

174-
writer.write(RUNTIME_INIT_INSTANCE_METHOD_CALL_STATEMENT);
175-
}
172+
writer.write(CLOSING_CURLY_BRACKET_LITERAL);
173+
174+
writer.write(RUNTIME_INIT_INSTANCE_METHOD_CALL_STATEMENT);
176175
}
177176

178177
private void writeRuntimeInitializationCheckForApplication(ReifiedJavaMethod method) {
@@ -366,4 +365,14 @@ private void writeInternalRuntimeMethod(String signature, String returnStatement
366365
writer.write(returnStatement);
367366
writer.write(CLOSING_CURLY_BRACKET_LITERAL);
368367
}
368+
369+
@Override
370+
public void writeInternalServiceOnCreateMethod() {
371+
writer.write(INTERNAL_SERVICES_ONCREATE_METHOD_SIGNATURE);
372+
writer.write(OPENING_CURLY_BRACKET_LITERAL);
373+
374+
writeRuntimeInitializationForService();
375+
376+
writer.write(CLOSING_CURLY_BRACKET_LITERAL);
377+
}
369378
}

0 commit comments

Comments
 (0)