add an annotation KeepUses to allow the user to avoir invocation erasing

This commit is contained in:
Renaud Pawlak 2020-02-21 09:14:20 +01:00
parent d324c26bfe
commit 974b62881d
3 changed files with 48 additions and 6 deletions

View File

@ -0,0 +1,35 @@
/*
* JSweet - http://www.jsweet.org
* Copyright (C) 2015 CINCHEO SAS <renaud.pawlak@cincheo.fr>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jsweet.lang;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation type should be used with {@link Erased} in order to keep the
* invocation or accesses to the erased elements in the client code.
*
* @author Renaud Pawlak
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.PACKAGE, ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD })
@Documented
public @interface KeepUses {
}

View File

@ -255,6 +255,10 @@ public abstract class JSweetConfig {
* Fully-qualified name for the JSweet <code>@Erased</code> annotation (see JSweet core API).
*/
public static final String ANNOTATION_ERASED = JSweetConfig.LANG_PACKAGE + ".Erased";
/**
* Fully-qualified name for the JSweet <code>@KeepUses</code> annotation (see JSweet core API).
*/
public static final String ANNOTATION_KEEP_USES = JSweetConfig.LANG_PACKAGE + ".KeepUses";
/**
* Fully-qualified name for the JSweet <code>@Async</code> annotation (see JSweet core API).
*/

View File

@ -19,6 +19,7 @@
package org.jsweet.transpiler.extension;
import static org.jsweet.JSweetConfig.ANNOTATION_ERASED;
import static org.jsweet.JSweetConfig.ANNOTATION_KEEP_USES;
import static org.jsweet.JSweetConfig.ANNOTATION_OBJECT_TYPE;
import static org.jsweet.JSweetConfig.ANNOTATION_STRING_TYPE;
import static org.jsweet.JSweetConfig.DEPRECATED_UTIL_CLASSNAME;
@ -347,7 +348,8 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
}
private boolean substituteUnresolvedMethodInvocation(JCMethodInvocation invocation) {
// this is a patch that should be removed when Class.isInstance gets supported by J4TS
// this is a patch that should be removed when Class.isInstance gets
// supported by J4TS
if (invocation.meth instanceof JCFieldAccess) {
JCFieldAccess fieldAccess = (JCFieldAccess) invocation.meth;
String methName = fieldAccess.name.toString();
@ -356,11 +358,11 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
if (typeName != null && "isInstance".equals(methName) && Class.class.getName().equals(typeName)) {
printMacroName(fieldAccess.toString());
print("((c:any,o:any) => { if(typeof c === 'string') return (o.constructor && o.constructor")
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME + "\"] && o.constructor")
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME + "\"].indexOf(c) >= 0) || (o")
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME + "\"] && o")
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME
+ "\"].indexOf(c) >= 0); else if(typeof c === 'function') return (o instanceof c) || (o.constructor && o.constructor === c); })(");
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME + "\"] && o.constructor")
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME + "\"].indexOf(c) >= 0) || (o")
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME + "\"] && o")
.print("[\"" + Java2TypeScriptTranslator.INTERFACES_FIELD_NAME
+ "\"].indexOf(c) >= 0); else if(typeof c === 'function') return (o instanceof c) || (o.constructor && o.constructor === c); })(");
getPrinter().print(fieldAccess.selected).print(",").print(invocation.args.head).print(")");
return true;
}
@ -386,6 +388,7 @@ public class Java2TypeScriptAdapter extends PrinterAdapter {
// So, we should probably find a better way to erase invocations (or at
// least do it conditionally).
if (hasAnnotationType(invocationElement.getMethod(), ANNOTATION_ERASED)
&& !hasAnnotationType(invocationElement.getMethod(), ANNOTATION_KEEP_USES)
&& !isAmbientDeclaration(invocationElement.getMethod())) {
print("null/*erased method " + ((MethodInvocationElementSupport) invocationElement).getTree().meth + "*/");
return true;