@@ -731,9 +731,10 @@ private String processLocalInstance(ASTNode node, ASTNode anonymousClassDeclarat
731731 // creation time and recover it here.
732732
733733 // String finals;
734+ boolean isStatic = true ;
734735 if (localType != REALLY_LOCAL_CLASS ) {
735736 // lambda and anonymous classes are defined inline.
736- addInnerDeclaration (anonymousClassDeclaration == null ? node : anonymousClassDeclaration , binding ,
737+ isStatic = addInnerDeclaration (anonymousClassDeclaration == null ? node : anonymousClassDeclaration , binding ,
737738 lambdaType , false , null );
738739 }
739740 IMethodBinding constructorDeclaration ;
@@ -762,7 +763,7 @@ private String processLocalInstance(ASTNode node, ASTNode anonymousClassDeclarat
762763 class_localType = oldLocalType ;
763764 if (lambdaType != LAMBDA_METHOD && localType != REALLY_LOCAL_CLASS )
764765 buffer .append (")" ); // end of line (..., ...)
765- return (package_htClassKeyToVisitedFinalVars .get (binding .getKey ()).isEmpty () ? anonName : null );
766+ return (isStatic && package_htClassKeyToVisitedFinalVars .get (binding .getKey ()).isEmpty () ? anonName : null );
766767 }
767768
768769 /**
@@ -775,17 +776,18 @@ private String processLocalInstance(ASTNode node, ASTNode anonymousClassDeclarat
775776 * @param isTrulyLocal not anonymous and not lambda -- private to a method
776777 * @param bodyDeclarations
777778 */
778- private void addInnerDeclaration (ASTNode node , ITypeBinding binding , int lambdaType , boolean isTrulyLocal ,
779+ private boolean addInnerDeclaration (ASTNode node , ITypeBinding binding , int lambdaType , boolean isTrulyLocal ,
779780 List <?> bodyDeclarations ) {
780781
782+ boolean isStatic = true ;
781783 boolean wasAnonymous = class_isAnonymousOrLocal ;
782784 String key = binding .getKey ();
783785 class_isAnonymousOrLocal = true ;
784786 Set <IVariableBinding > lastVisitedVars = class_visitedFinalVars ;
785787 Set <IVariableBinding > myVisitedVars = class_visitedFinalVars = new HashSet <>();
786788 package_htClassKeyToVisitedFinalVars .put (key , myVisitedVars );
787789 if (lambdaType != NOT_LAMBDA ) {
788- addClassOrInterface (node , binding , null , 'm' );
790+ isStatic = addClassOrInterface (node , binding , null , 'm' );
789791 if (lambdaType == LAMBDA_METHOD )
790792 buffer .append ("); return " );
791793 else
@@ -807,10 +809,9 @@ private void addInnerDeclaration(ASTNode node, ITypeBinding binding, int lambdaT
807809 }
808810 class_visitedFinalVars = lastVisitedVars ;
809811 class_isAnonymousOrLocal = wasAnonymous ;
812+ return isStatic ;
810813 }
811814
812- static int test = 0 ;
813-
814815
815816 /**
816817 * Generated final variable list for anonymous class creation. Update the
@@ -1158,7 +1159,7 @@ public boolean visit(MethodInvocation node) {
11581159 * @param mBinding
11591160 * @param expression
11601161 */
1161- private void addMethodInvocation (SimpleName javaQualifier , List <?> arguments , IMethodBinding mBinding ,
1162+ private boolean addMethodInvocation (SimpleName javaQualifier , List <?> arguments , IMethodBinding mBinding ,
11621163 Expression expression , int lambdaArity ) {
11631164 if (javaQualifier == null ) {
11641165 // not possible?
@@ -1259,23 +1260,24 @@ private void addMethodInvocation(SimpleName javaQualifier, List<?> arguments, IM
12591260 //
12601261 boolean classIsTarget = (class_localType == LAMBDA_WRAPPED && (
12611262 (isStatic
1262- || isVariableBinding (expression )
12631263 || expression instanceof ClassInstanceCreation
1264+ || isVariableBinding (expression )
12641265 || expression instanceof ThisExpression
12651266 || "java.lang.Class" .equals (removeBracketsAndFixNullPackageName (declaringClassJavaClassName )) // String.class::cast
12661267 ) // BH Added 2019.05.13
12671268 && lambdaArity == mBinding .getParameterTypes ().length ));
12681269
1269- // ////bufferDebug("addmeth isclasstarget " + isStatic + " "+ classIsTarget
1270- // + " " + (expression == null ? null : expression.getClass().getName())
1271- // + " " + declaringClassJavaClassName
1272- // );
1270+ bufferDebug ("addmeth isclasstarget " + isStatic + " classIsTarget=" + classIsTarget
1271+ + " ivarbinding=" +isVariableBinding (expression ) + " exp=" + (expression == null ?
1272+ null : expression .getClass ().getName ())
1273+ + " " + declaringClassJavaClassName
1274+ );
12731275 String opening = (classIsTarget ? "$$." : "t." ) + finalMethodNameWith$Params + ".apply("
12741276 + (isStatic ? "null" : classIsTarget ? "$$" : "t" ) + ",[" ;
12751277 buffer .append (opening );
12761278 buffer .append (getLambdaParamList (mBinding , lambdaArity ));
12771279 buffer .append ("])" );
1278- return ;
1280+ return isStatic ;
12791281 }
12801282 if (finalMethodNameWith$Params .indexOf ('|' ) >= 0 ) {
12811283 // cover multiple parameter options to cover older versions of java
@@ -1310,6 +1312,7 @@ private void addMethodInvocation(SimpleName javaQualifier, List<?> arguments, IM
13101312 buffer .append ("(" );
13111313 addMethodParameterList (arguments , mBinding , null , null , isIndexOf ? METHOD_INDEXOF : METHOD_NOTSPECIAL );
13121314 buffer .append (term );
1315+ return true ;
13131316 }
13141317
13151318 /**
@@ -1466,31 +1469,18 @@ public boolean visit(TryStatement node) {
14661469 List <CatchClause > catchClauses = node .catchClauses ();
14671470 int size = catchClauses .size ();
14681471 Block finallyBlock = node .getFinally ();
1469- buffer .append (size > 0 || finallyBlock != null ? "try " : "/*try*/ " );
14701472 List <ASTNode > resources = node .resources ();
14711473 // Returns the live ordered list of resources for this try statement (added in
14721474 // JLS4 API).
14731475 // [ooh...JSL9 change...]
14741476 // A resource is either a VariableDeclarationExpression or (since JLS9) a Name.
1475-
1476- int pt = (resources == null || resources .size () == 0 ? -1 : buffer .length ());
1477- node .getBody ().accept (this );
1478- if (pt >= 0 ) {
1479- // just after first "{"
1480- String buf = buffer .substring ((pt = buffer .indexOf ("{" , pt ) + 1 ));
1481- buffer .setLength (pt );
1482- for (int i = 0 ; i < resources .size (); i ++) {
1483- ASTNode resource = resources .get (i );
1484- if (resource instanceof VariableDeclarationExpression ) {
1485- resource .accept (this );
1486- buffer .append (";" );
1487- } else {
1488- // Java 9 -- just a Name.
1489- buffer .append ("/* Java 9 resource " + resource + "*/\r \n " );
1490- }
1491- }
1492- buffer .append (buf );
1477+ int pt = -1 ;
1478+ if (resources != null && resources .size () > 0 ) {
1479+ buffer .append ("try {\r \n " );
1480+ pt = buffer .length ();
14931481 }
1482+ buffer .append (size > 0 || finallyBlock != null ? "try " : "/*try*/ " );
1483+ node .getBody ().accept (this );
14941484 if (size > 0 ) {
14951485 String catchEName = "e$$" ;
14961486 if (size == 1 ) {
@@ -1554,11 +1544,38 @@ public boolean visit(TryStatement node) {
15541544 if (finallyBlock != null ) {
15551545 buffer .append (" finally " );
15561546 finallyBlock .accept (this );
1547+ }
1548+ if (pt >= 0 ) {
1549+ // just after first "{"
1550+ String buf = buffer .substring (pt );
1551+ buffer .setLength (pt );
1552+ String closing = "" ;
1553+ for (int i = 0 ; i < resources .size (); i ++) {
1554+ ASTNode resource = resources .get (i );
1555+ pt = buffer .length ();
1556+ resource .accept (this );
1557+ buffer .append (";\r \n " );
1558+ closing = getResourceClosing (pt ) + closing ;
1559+ }
1560+ buffer .append (buf );
1561+ buffer .append ("\r \n }finally{/*res*/" ).append (closing ).append ("}" );
15571562 }
15581563 buffer .append ("\r \n " );
15591564 return false ;
15601565 }
15611566
1567+ private String getResourceClosing (int pt ) {
1568+ String name = buffer .substring (pt );
1569+ // Java 9 try(res) or Java 8 try(OutputStream os = ....)
1570+ if ((pt = name .indexOf ("=" )) >= 0 ||
1571+ (pt = name .indexOf (";" )) >= 0 ) {
1572+ name = name .substring (0 , pt );
1573+ }
1574+ if (name .startsWith ("var " ))
1575+ name = name .substring (4 );
1576+ return "\r \n try{" + name + "&&" + name + ".close$&&" + name + ".close$()}catch(_){}" ;
1577+ }
1578+
15621579 /**
15631580 * A class or interface is being declared.
15641581 *
@@ -1652,9 +1669,9 @@ private void appendClinit() {
16521669 * @return localName
16531670 */
16541671 @ SuppressWarnings ({ "null" , "unchecked" })
1655- private void addClassOrInterface (ASTNode node , ITypeBinding binding , List <?> bodyDeclarations , char type ) {
1672+ private boolean addClassOrInterface (ASTNode node , ITypeBinding binding , List <?> bodyDeclarations , char type ) {
16561673 if (binding == null )
1657- return ;
1674+ return false ;
16581675
16591676 checkGenericClass (binding , binding );
16601677
@@ -1712,7 +1729,7 @@ private void addClassOrInterface(ASTNode node, ITypeBinding binding, List<?> bod
17121729
17131730 trailingBuffer .append (tempVisitor .buffer .toString ());
17141731
1715- return ;
1732+ return false ;
17161733 }
17171734
17181735 // set up key fields and local variables
@@ -2071,8 +2088,9 @@ && checkAnnotations(element, CHECK_J2S_IGNORE_AND_ANNOTATIONS)) {
20712088
20722089 StringBuffer defaults = new StringBuffer ();
20732090
2091+ boolean isStatic = true ;
20742092 if (isLambda ) {
2075- addLambdaClass (node , binding .getFunctionalInterfaceMethod ());
2093+ isStatic = addLambdaClass (node , binding .getFunctionalInterfaceMethod ());
20762094 } else {
20772095 for (Iterator <?> iter = bodyDeclarations .iterator (); iter .hasNext ();) {
20782096 ASTNode element = (ASTNode ) iter .next ();
@@ -2171,6 +2189,7 @@ && checkAnnotations(element, CHECK_J2S_IGNORE_AND_ANNOTATIONS)) {
21712189 setClassAndBinding (oldShortClassName , oldBinding );
21722190 }
21732191 }
2192+ return isStatic ;
21742193 }
21752194
21762195// private boolean checkDeclarationType(BodyDeclaration element, int type) {
@@ -6848,7 +6867,8 @@ private boolean addLambdaMethodReference(MethodReference node, Expression exp) {
68486867 buffer .append ("})(" );
68496868 appendFinalMethodQualifier (exp , declaringClass , null , FINAL_ESCAPECACHE | FINAL_LAMBDA );
68506869 buffer .append (")" );
6851- addLambdaReuse (pt , anonName );
6870+ if (anonName != null )
6871+ addLambdaReuse (pt , anonName );
68526872 return false ;
68536873 }
68546874
@@ -6861,6 +6881,9 @@ private boolean addLambdaMethodReference(MethodReference node, Expression exp) {
68616881 private void addLambdaReuse (int pt , String anonName ) {
68626882 String tmp = buffer .substring (pt );
68636883 buffer .setLength (pt );
6884+
6885+ bufferDebug ("addLambdaReuse " + anonName );
6886+
68646887 anonName = getFinalJ2SClassName (anonName , FINAL_P );
68656888 buffer .append ("(" + anonName + "$||(" + anonName + "$=(" )
68666889 .append (tmp ).append (")))" );
@@ -6894,7 +6917,7 @@ private String getLambdaParamList(IMethodBinding mBinding, int arity0) {
68946917 * @param lnode
68956918 * @param mBinding
68966919 */
6897- private void addLambdaClass (ASTNode lnode , IMethodBinding mBinding ) {
6920+ private boolean addLambdaClass (ASTNode lnode , IMethodBinding mBinding ) {
68986921 if (lnode instanceof LambdaExpression ) {
68996922 buffer .append ("/*lambda_E*/" );
69006923 LambdaExpression node = (LambdaExpression ) lnode ;
@@ -6907,7 +6930,7 @@ private void addLambdaClass(ASTNode lnode, IMethodBinding mBinding) {
69076930 processMethodDeclaration (mBinding , params , node .getBody (), false , LAMBDA_EXPRESSION );
69086931 ////bufferDebug("procmethoddecl2");
69096932 class_localType = localType ;
6910- return ;
6933+ return true ;
69116934 }
69126935 if (lnode instanceof CreationReference ) {
69136936 buffer .append ("/*lambda_C*/" );
@@ -6923,7 +6946,7 @@ private void addLambdaClass(ASTNode lnode, IMethodBinding mBinding) {
69236946 addConstructor (binding , mBinding , null , mBinding .getParameterTypes ().length );
69246947 }
69256948 buffer .append ("});\r \n " );
6926- return ;
6949+ return true ;
69276950 }
69286951 // method of one type or another
69296952 SimpleName identifier ;
@@ -6951,11 +6974,12 @@ private void addLambdaClass(ASTNode lnode, IMethodBinding mBinding) {
69516974 } else {
69526975 buffer .append ("/*lambda_?*/" );
69536976 log ("??? addLambdaMethod " + lnode .getClass ().getName ());
6954- return ;
6977+ return false ;
69556978 }
69566979 processMethodDeclaration (mBinding , null , null , false , LAMBDA_METHOD );
6957- addMethodInvocation (identifier , null , mBinding1 , exp , mBinding .getParameterTypes ().length );
6980+ boolean isStatic = addMethodInvocation (identifier , null , mBinding1 , exp , mBinding .getParameterTypes ().length );
69586981 buffer .append ("});\r \n " );
6982+ return isStatic ;
69596983 }
69606984
69616985 /**
0 commit comments