Skip to content

Commit a9d0001

Browse files
authored
Merge pull request #137 from BobHanson/hanson1
3.2.7 adds @j2sAlias to indicate an alternative name for a method.
2 parents 80b1cc2 + d6758c2 commit a9d0001

File tree

6 files changed

+41
-43
lines changed

6 files changed

+41
-43
lines changed
-37 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200108182527
1+
20200109070504
-37 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200108182527
1+
20200109070504

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -136,35 +136,37 @@
136136

137137
//todo: j2sdoc in static field showing up in default static block only, not in initializer block.
138138

139+
//BH 2020.01.09 -- 3.2.7-v2 introduces @j2sAlias as a way of adding a custom method name, as in exports.
139140
//BH 2020.01.08 -- 3.2.7-v1 sets generic references to their erasures; adds implicit synthetic default methods to interfaces
140-
//BH 2020.01.05 -- 3.2.6-v2 fixes synthetic methods missing for generics
141-
142-
// There was a limitation discovered in relation to Spliterators. Specifically,
143-
// Spliterators.EmptySpliterator.OfInt (|Long|Double) did not have the
144-
// synthetic bridge tryAdvance(IntConsumer consumer). This was due to the fact that
145-
// Java considers tryAdvance(C consumer) in the Spliterator.EmptySpliterator superclass<T,C,S>
146-
// to do that:
147-
148-
// public boolean tryAdvance(C consumer) {
149-
// Objects.requireNonNull(consumer);
150-
// return false;
151-
// }
152-
153-
// where class OfInt extends EmptySpliterator<Integer, Spliterator.OfInt, IntConsumer>
154-
155-
// Java handles this with a synthetic bridge in Spliterators$EmptySpliterator$OfInt that maps
156-
// java.util.Spliterators$EmptySpliterator$OfInt.tryAdvance(IntConsumer)
157-
// to Spliterator.OfInt.tryAdvance(Object):
158-
159-
//// Method descriptor #17 (Ljava/util/function/IntConsumer;)Z
160-
//// Stack: 2, Locals: 2
161-
//public bridge synthetic boolean tryAdvance(java.util.function.IntConsumer arg0);
162-
// 0 aload_0 [this]
163-
// 1 aload_1 [arg0]
164-
// 2 checkcast java.lang.Object [18]
165-
// 5 invokevirtual java.util.Spliterators$EmptySpliterator$OfInt.tryAdvance(java.lang.Object) : boolean [20]
166-
// 8 ireturn
167-
141+
142+
// Note: There was a limitation discovered in relation to Spliterators. Specifically,
143+
// Spliterators$EmptySpliterator$OfInt (OfLong, OfDouble, etc) did not have the
144+
// the method tryAdvance(IntConsumer consumer). This was due to the fact that
145+
// interface Spliterator$OfInt.tryAdvance(C consumer) overrides interface Spliterator$ofPrimitive.tryAdvance(C action)
146+
// but does not have the same signature.
147+
// Java handles this with a synthetic bridge in Spliterators$EmptySpliterator$OfInt, which maps
148+
// java.util.Spliterators$EmptySpliterator$OfInt.tryAdvance(IntConsumer)
149+
// to Spliterator.OfInt.tryAdvance(Object):
150+
//
151+
// public bridge synthetic boolean tryAdvance(java.util.function.IntConsumer arg0);
152+
// 0 aload_0 [this]
153+
// 1 aload_1 [arg0]
154+
// 2 checkcast java.lang.Object [18]
155+
// 5 invokevirtual java.util.Spliterators$EmptySpliterator$OfInt.tryAdvance(java.lang.Object) : boolean [20]
156+
// 8 ireturn
157+
//
158+
// But, actually, we can do one better. All that was necessary was to add to the $defaults$ function of
159+
// the Spliterator$OfInt interface an alias that equates tryAdvace$java_util_function_IntConsumer
160+
// to tryAdvance$O:
161+
//
162+
// C$.$defaults$ = function(C$){
163+
// ...
164+
// C$.prototype['tryAdvance$java_util_function_IntConsumer']=C$.prototype['tryAdvance$O'];
165+
// };
166+
//
167+
// In this way, any implementing class gets that synthetic bridge method. Ta-DA!
168+
169+
// BH 2020.01.05 -- 3.2.6-v2 fixes synthetic methods missing for generics
168170
// BH 2020.01.03 -- 3.2.6-v1 fixes for $__T and some synthetic methods missing
169171
// BH 2020.01.01 3.2.6-v1 fixes for generic varargs with only one parameter
170172
// BH 2019.12.19 3.2.6-v0 C$.$clinit$=2 adds C$.$fields$, Clazz._getFields
@@ -282,8 +284,6 @@ public class Java2ScriptVisitor extends ASTVisitor {
282284
private final static int METHOD_ISQUALIFIED = 16;
283285
private final static int METHOD_NULLEXPRESSION = 32;
284286

285-
private final static int METHOD_ALIAS = 64;
286-
287287
private static final int NOT_LOCAL = 0;
288288
private static final int REALLY_LOCAL_CLASS = 1;
289289
private static final int ANON_CLASS = 2;
@@ -1292,6 +1292,7 @@ private void processMethodDeclaration(MethodDeclaration mnode, IMethodBinding mB
12921292
String quotedFinalNameOrArray = getMethodNameWithSyntheticBridgeForDeclaration(mBinding, isConstructor, alias, qualification,
12931293
isAbstract ? abstractMethodList : null);
12941294
if (isAbstract) {
1295+
// allows us to catalog method names for an interface or abstract class
12951296
return;
12961297
}
12971298
boolean isMain = (isStatic && isPublic && mBinding.getName().equals("main")
@@ -5292,8 +5293,6 @@ private Map<String, Object> getGenericClassTypes(ITypeBinding type) {
52925293
for (int i = 0; i < tokens.length; i++) {
52935294
String key = tokens[i].trim();
52945295
key = key.substring(0, (key + " ").indexOf(" "));
5295-
// if (i >= types.length)
5296-
// System.out.println("???getGeneric??? " + i + "/" + types.length + " key=" + key + " temp=" + temp + " sb=" + sb);
52975296
classTypes.put(key, (i < types.length ? types[i] : "O"));
52985297
}
52995298
// note: enabling this line causes an intense memory situation.
@@ -5324,28 +5323,24 @@ private List<String[]> getGenericMethodList(ITypeBinding methodClass, String met
53245323
* @param node
53255324
* @param mBinding
53265325
* @param isConstructor
5327-
* @param aliases
5326+
* @param alias name provided using at_j2sAlias
53285327
* @param mode
53295328
* @param declaredMethodList
53305329
* @return j2s-qualified name or an array of j2s-qualified names
53315330
*/
53325331
String getMethodNameWithSyntheticBridgeForDeclaration(IMethodBinding mBinding, boolean isConstructor,
5333-
String aliases, int mode, List<String> declaredMethodList) {
5332+
String alias, int mode, List<String> declaredMethodList) {
53345333
List<String> names = (declaredMethodList == null ? new ArrayList<String>() : declaredMethodList);
53355334
int pt = names.size();
5335+
if (alias != null)
5336+
names.add(alias);
53365337
String nodeName = mBinding.getName();
53375338
String methodName = (isConstructor ? "c$" : nodeName);
53385339
String qname = getFinalMethodNameWith$Params(methodName, mBinding, null, false, METHOD_NOTSPECIAL);
53395340
names.add(qname);
53405341
ITypeBinding methodClass = mBinding.getDeclaringClass();
53415342
List<String[]> methodList = getGenericMethodList(methodClass, nodeName);
5342-
if (aliases != null) {
5343-
String[] types = aliases.split(",");
5344-
String pname = getFinalMethodNameWith$Params(methodName, mBinding, types, false, METHOD_ALIAS);
5345-
if (pname != null) {
5346-
names.add(pname);
5347-
}
5348-
} else if (methodList != null) {
5343+
if (methodList != null) {
53495344
for (int i = methodList.size(); --i >= 0;) {
53505345
String pname = getFinalMethodNameWith$Params(methodName, mBinding, methodList.get(i), false, METHOD_NOTSPECIAL);
53515346
if (pname != null)

sources/net.sf.j2s.java.core/src/test/Test_Native.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
class Test_Native extends Test_ {
2626

27+
/**
28+
* @j2sAlias test123
29+
*/
2730
@Override
2831
public void test123(int a, int b, int c) {
2932

0 commit comments

Comments
 (0)