Skip to content

Commit ca00318

Browse files
committed
annotation/lambda issue; string literal cache
- fixes issue that class with lambda expression and annotation fails to transpile - clears string literal cache prior to clean build
1 parent 2e8db52 commit ca00318

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ boolean initializeProject(IJavaProject project, boolean isCompilationParticipant
314314
Java2ScriptVisitor.NameMapper.setNonQualifiedNamePackages(nonqualifiedPackages);
315315
Java2ScriptVisitor.NameMapper.setClassReplacements(classReplacements);
316316

317+
if (isCleanBuild)
318+
Java2ScriptVisitor.clearStringLiteralCache();
319+
317320
} catch (Exception e) {
318321
System.out.println("error " + e + " " + e.getStackTrace());
319322
e.printStackTrace();

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@
135135
import org.eclipse.jdt.core.dom.WhileStatement;
136136
import org.eclipse.jdt.core.dom.WildcardType;
137137

138+
// todo: j2sdoc in static field showing up in default static block only, not in initializer block.
139+
140+
//BH 2019.11.18 3.2.5-v0 fix for lambda expressions in classes with annotations
138141
// BH 2019.11.12 3.2.5-v0 fix for string literals with \n \nn \nnn octals, but "use strict" does not allow for this.
139142
// BH 2019.11.12 3.2.5-v0 fix for static object being created before initialization is complete.
140143
// BH 2019.11.12 3.2.5-v0 proper semantic versioning
@@ -2156,7 +2159,8 @@ && checkAnnotations(element, CHECK_J2S_IGNORE_AND_ANNOTATIONS)) {
21562159
// add any recently defined static field definitions, assert strings
21572160
// and Enum constants
21582161

2159-
if (class_annotationType != ANNOTATION_TYPE_UNKNOWN) {
2162+
if (class_annotationType != ANNOTATION_TYPE_UNKNOWN && methods != null) {
2163+
// lambda expressions may have an enclosing annotation type, but they will not have methods
21602164
ClassAnnotation.addClassAnnotations(class_annotationType, class_annotations, enums, fields, methods,
21612165
innerClasses, trailingBuffer);
21622166
class_annotations = null;
@@ -2323,8 +2327,9 @@ private boolean addFieldDeclaration(FieldDeclaration field, int mode) {
23232327
boolean needDefault = (mode == FIELD_DECL_NONSTATIC_ALL ||
23242328
mode == FIELD_DECL_STATIC_DEFAULTS);
23252329

2326-
if (needDefault)
2330+
if (needDefault) {
23272331
addJ2SDoc(field);
2332+
}
23282333
int len0 = buffer.length();
23292334
for (Iterator<?> iter = fragments.iterator(); iter.hasNext();) {
23302335
VariableDeclarationFragment fragment = (VariableDeclarationFragment) iter.next();
@@ -2347,24 +2352,24 @@ private boolean addFieldDeclaration(FieldDeclaration field, int mode) {
23472352

23482353
// but it cannot be bye or short, because those will use $b$ or $s$,
23492354
// which are not defined until the end.
2350-
if (isStatic && isPrimitive && (initializer instanceof NumberLiteral
2351-
&& code != PrimitiveType.SHORT && code != PrimitiveType.BYTE
2352-
|| initializer instanceof BooleanLiteral
2353-
|| initializer instanceof CharacterLiteral)
2354-
) {
2355-
// let primitives be their default value. This allows, for example,
2356-
// setting a static value to something read just by loading the class.
2357-
addExpressionAsTargetType(initializer, field.getType(), "v", null);
2358-
} else {
2355+
// if (isStatic && isPrimitive && (
2356+
// initializer instanceof NumberLiteral
2357+
// && code != PrimitiveType.SHORT && code != PrimitiveType.BYTE
2358+
// || initializer instanceof BooleanLiteral
2359+
// || initializer instanceof CharacterLiteral)
2360+
// ) {
2361+
// // let primitives be their default value. This allows, for example,
2362+
// // setting a static value to something read just by loading the class.
2363+
// addExpressionAsTargetType(initializer, field.getType(), "v", null);
2364+
// } else {
23592365
buffer.append(code == null ? "null" : getPrimitiveDefault(code));
2360-
}
2366+
// }
23612367
buffer.append(";\r\n");
23622368
//
2363-
// $clinit$ -- statics; once only
2364-
// $init0$ -- from within Clazz.newInstance, before any
2365-
// constructors
2366-
// $init$ -- from the constructor, just after any super()
2367-
// call or whenever there is no this() call
2369+
// $clinit$ -- just runs Clazz.load(cl,1) for getting dependencies
2370+
// $static$ -- statics; once only; processed later by Clazz.load(cl,2)
2371+
// $init0$ -- from within Clazz.newInstance, before any constructors
2372+
// $init$ -- from the constructor, just after any super() call or whenever there is no this() call
23682373

23692374
// com.falstad.Diffraction.CrossAperature initialization was
23702375
// failing. Sequence was:
@@ -3505,6 +3510,10 @@ public boolean visit(SimpleType node) {
35053510

35063511
private static Map<String,String> htStrLitCache = new Hashtable<>();
35073512

3513+
static void clearStringLiteralCache() {
3514+
htStrLitCache = new Hashtable<>();
3515+
}
3516+
35083517
public boolean visit(StringLiteral node) {
35093518
String s = node.getEscapedValue();
35103519
if (s.indexOf('\\') < 0) {
@@ -5061,7 +5070,7 @@ private String getFinalMethodNameOrArrayForDeclaration(IMethodBinding mBinding,
50615070
&& !classHasNoParameterMethod(methodClass, methodName)) {
50625071
if (names == null)
50635072
names = new ArrayList<String>();
5064-
names.add(methodName + (methodName.indexOf("$") >= 0 ? "" : "$"));
5073+
names.add(methodName + (methodName.indexOf("$") >= 0 ? "" : methodName.equals("c") ? "$$" : "$"));
50655074
}
50665075
if ((qualification & METHOD_UNQUALIFIED) != 0) {
50675076
if (names == null)
@@ -5114,8 +5123,10 @@ private static boolean classHasNoParameterMethod(ITypeBinding methodClass, Strin
51145123
* @return
51155124
*/
51165125
private static String ensureMethod$Name(String j2sName, IMethodBinding mBinding, String className) {
5117-
if (isPrivate(mBinding) && !isStatic(mBinding) || NameMapper.fieldNameCoversMethod(j2sName)
5118-
|| j2sName.indexOf("$", 2) >= 0 || j2sName.equals("c$")
5126+
if (isPrivate(mBinding) && !isStatic(mBinding)
5127+
|| NameMapper.fieldNameCoversMethod(j2sName)
5128+
|| j2sName.indexOf("$", 2) >= 0
5129+
|| j2sName.equals("c$")
51195130
|| className != null && NameMapper.isMethodNonqualified(className, mBinding.getName()))
51205131
return j2sName;
51215132
// c() must be changed to c$$, not c$, which is the constructor
@@ -5372,6 +5383,7 @@ private boolean getConstantValue(Expression node, boolean andWrite) {
53725383
sb = new StringBuffer();
53735384
addString((String) constValue, sb);
53745385
}
5386+
53755387
if (sb == null)
53765388
return false;
53775389
if (andWrite) {
@@ -5728,10 +5740,10 @@ public List<String> getElementList() {
57285740
String header_noIncludes = header.replace(",I$=[[]]", "");
57295741
header = header.replace(",I$=[]", privateVarString + (package_includes.length() == 0 ? ""
57305742
: package_includes.append("]],"
5731-
+ "$I$=function(i,n){return ("
5743+
+ "$I$=function(i,n){return"
57325744
+ "(i=(I$[i]||(I$[i]=Clazz.load(I$[0][i])))),"
57335745
+ "!n&&i.$load$&&Clazz.load(i,2),"
5734-
+ "i)}"
5746+
+ "i}"
57355747
)));
57365748
for (int i = 1; i < parts.length; i++) {
57375749
js = parts[i];

0 commit comments

Comments
 (0)