143 this.F = fac.F; |
143 this.F = fac.F; |
144 this.log = fac.log; |
144 this.log = fac.log; |
145 this.names = fac.names; |
145 this.names = fac.names; |
146 this.source = fac.source; |
146 this.source = fac.source; |
147 this.allowTWR = source.allowTryWithResources(); |
147 this.allowTWR = source.allowTryWithResources(); |
|
148 this.allowEffectivelyFinalVariablesInTWR = |
|
149 source.allowEffectivelyFinalVariablesInTryWithResources(); |
148 this.allowDiamond = source.allowDiamond(); |
150 this.allowDiamond = source.allowDiamond(); |
149 this.allowMulticatch = source.allowMulticatch(); |
151 this.allowMulticatch = source.allowMulticatch(); |
150 this.allowStringFolding = fac.options.getBoolean("allowStringFolding", true); |
152 this.allowStringFolding = fac.options.getBoolean("allowStringFolding", true); |
151 this.allowLambda = source.allowLambda(); |
153 this.allowLambda = source.allowLambda(); |
152 this.allowMethodReferences = source.allowMethodReferences(); |
154 this.allowMethodReferences = source.allowMethodReferences(); |
181 boolean allowMulticatch; |
183 boolean allowMulticatch; |
182 |
184 |
183 /** Switch: should we recognize try-with-resources? |
185 /** Switch: should we recognize try-with-resources? |
184 */ |
186 */ |
185 boolean allowTWR; |
187 boolean allowTWR; |
|
188 |
|
189 /** Switch: should we allow (effectively) final variables as resources in try-with-resources? |
|
190 */ |
|
191 boolean allowEffectivelyFinalVariablesInTWR; |
186 |
192 |
187 /** Switch: should we fold strings? |
193 /** Switch: should we fold strings? |
188 */ |
194 */ |
189 boolean allowStringFolding; |
195 boolean allowStringFolding; |
190 |
196 |
3001 defs.append(resource()); |
3007 defs.append(resource()); |
3002 } |
3008 } |
3003 return defs.toList(); |
3009 return defs.toList(); |
3004 } |
3010 } |
3005 |
3011 |
3006 /** Resource = VariableModifiersOpt Type VariableDeclaratorId = Expression |
3012 /** Resource = VariableModifiersOpt Type VariableDeclaratorId "=" Expression |
|
3013 * | Expression |
3007 */ |
3014 */ |
3008 protected JCTree resource() { |
3015 protected JCTree resource() { |
3009 JCModifiers optFinal = optFinal(Flags.FINAL); |
3016 int startPos = token.pos; |
3010 JCExpression type = parseType(); |
3017 if (token.kind == FINAL || token.kind == MONKEYS_AT) { |
3011 int pos = token.pos; |
3018 JCModifiers mods = optFinal(Flags.FINAL); |
3012 Name ident = ident(); |
3019 JCExpression t = parseType(); |
3013 return variableDeclaratorRest(pos, optFinal, type, ident, true, null); |
3020 return variableDeclaratorRest(token.pos, mods, t, ident(), true, null); |
|
3021 } |
|
3022 JCExpression t = term(EXPR | TYPE); |
|
3023 if ((lastmode & TYPE) != 0 && LAX_IDENTIFIER.accepts(token.kind)) { |
|
3024 JCModifiers mods = toP(F.at(startPos).Modifiers(Flags.FINAL)); |
|
3025 return variableDeclaratorRest(token.pos, mods, t, ident(), true, null); |
|
3026 } else { |
|
3027 checkVariableInTryWithResources(startPos); |
|
3028 if (!t.hasTag(IDENT) && !t.hasTag(SELECT)) { |
|
3029 log.error(t.pos(), "try.with.resources.expr.needs.var"); |
|
3030 } |
|
3031 |
|
3032 return t; |
|
3033 } |
3014 } |
3034 } |
3015 |
3035 |
3016 /** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration} |
3036 /** CompilationUnit = [ { "@" Annotation } PACKAGE Qualident ";"] {ImportDeclaration} {TypeDeclaration} |
3017 */ |
3037 */ |
3018 public JCTree.JCCompilationUnit parseCompilationUnit() { |
3038 public JCTree.JCCompilationUnit parseCompilationUnit() { |
3931 if (!allowTWR) { |
3951 if (!allowTWR) { |
3932 error(token.pos, "try.with.resources.not.supported.in.source", source.name); |
3952 error(token.pos, "try.with.resources.not.supported.in.source", source.name); |
3933 allowTWR = true; |
3953 allowTWR = true; |
3934 } |
3954 } |
3935 } |
3955 } |
|
3956 void checkVariableInTryWithResources(int startPos) { |
|
3957 if (!allowEffectivelyFinalVariablesInTWR) { |
|
3958 error(startPos, "var.in.try.with.resources.not.supported.in.source", source.name); |
|
3959 allowEffectivelyFinalVariablesInTWR = true; |
|
3960 } |
|
3961 } |
3936 void checkLambda() { |
3962 void checkLambda() { |
3937 if (!allowLambda) { |
3963 if (!allowLambda) { |
3938 log.error(token.pos, "lambda.not.supported.in.source", source.name); |
3964 log.error(token.pos, "lambda.not.supported.in.source", source.name); |
3939 allowLambda = true; |
3965 allowLambda = true; |
3940 } |
3966 } |