Mercurial > hg > release > icedtea7-forest-2.6 > langtools
changeset 2307:0581b1282c3e icedtea-2.6.21 icedtea-2.6.22pre00
Merge jdk7u251-b02
author | andrew |
---|---|
date | Thu, 06 Feb 2020 01:43:02 +0000 |
parents | 25cfe877d6cf (diff) da36eab15b91 (current diff) |
children | c81c03906a10 |
files | .hgtags |
diffstat | 12 files changed, 1070 insertions(+), 732 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgtags Wed Feb 05 07:28:48 2020 +0000 +++ b/.hgtags Thu Feb 06 01:43:02 2020 +0000 @@ -50,6 +50,7 @@ 9596dff460935f09684c11d156ce591f92584f0d jdk7-b73 1a66b08deed0459054b5b1bea3dfbead30d258fa jdk7-b74 2485f5641ed0829205aaaeb31ad711c2c2ef0de3 jdk7-b75 +83367f01297bf255f511f5291bbbbaa24a9c8459 icedtea7-1.12 8fb9b4be3cb1574302acde90549a4d333ef51e93 jdk7-b76 0398ae15b90ac76d87ee21844453e95ff8613e43 jdk7-b77 acc1e40a5874ebf32bebcb6ada565b3b40b7461c jdk7-b78 @@ -63,6 +64,7 @@ ef07347428f2198ae6b8144ac0b9086bbe39fd16 jdk7-b86 409db93d19c002333980df5b797c6b965150c7a0 jdk7-b87 f9b5d4867a26f8c4b90ad37fe2c345b721e93d6b jdk7-b88 +681f1f51926faf4c73d8905a429ff4ead6e9d622 icedtea7-1.13 6cea9a143208bc1185ced046942c0f4e45dbeba5 jdk7-b89 71c2c23a7c35b2896c87004023b9743b6d1b7758 jdk7-b90 97b6fa97b8ddb3a49394011c2a0ec5d6535e594c jdk7-b91 @@ -111,6 +113,7 @@ 3d7acdbb72cab55deedfd35f60d4732abc9d6ac4 jdk7-b134 9d0a61ac567b983da7cc8f4a7030f2245bb6dbab jdk7-b135 ed0f7f1f9511db4f9615b1426d22f8b961629275 jdk7-b136 +8e26c4aee63c04ee129bf9068f5eea47cc385177 icedtea-1.14 a15c9b058ae007d4ccb7e35ce44e4dfa977f090b jdk7-b137 53f212bed4f4304dce7f0bf0fa01c998c65bacd6 jdk7-b138 853b6bb99f9b58eb7cf8211c67d3b6e4f1228a3e jdk7-b139 @@ -123,6 +126,7 @@ 9425dd4f53d5bfcd992d9aecea0eb7d8b2d4f62b jdk7-b146 d34578643d1c6c752d4a6b5e79c6ab1b60850b4a jdk7u1-b01 58bc532d63418ac3c9b42460d89cdaf595c6f3e1 jdk7-b147 +fb7fb3071b642334520e5b9f4a87ce28717af61c icedtea-2.0-branchpoint cd2cc8b5edb045b950aed46d159b4fb8fc2fd1df jdk7u1-b02 82820a30201dbf4b80f1916f3d0f4a92ad21b61a jdk7u1-b03 baa2c13c70fea3d6e259a34f0903197fdceb64b5 jdk7u1-b04 @@ -141,6 +145,7 @@ f0802d8a0909f66ce19d3d44b33ddf4943aee076 jdk7u2-b12 f474527e77e4797d78bd6c3b31923fddcfd9d5c6 jdk7u2-b13 fc0769df8cd03fffc38c7a1ab6b2e2e7cc2506a8 jdk7u2-b21 +58f6a950cd726220e81eddb126ca5c57e3b368f2 icedtea-2.1-branchpoint fc0769df8cd03fffc38c7a1ab6b2e2e7cc2506a8 jdk7u3-b02 0ffc4995457773085f61c39f6d33edc242b41bcf jdk7u3-b03 f6de36b195cd315646213c7affd2cc15702edbfb jdk7u3-b04 @@ -157,6 +162,7 @@ 8919b2b02fcba65f833c68374f3bfdd9bc3ba814 jdk7u4-b10 4672e092f0968d503dc37f860b15ae7e2653f8d7 jdk7u4-b11 a4bf6a1aff54a98e9ff2b3fb53c719f658bec677 jdk7u4-b12 +e3537a4f75c7fcca16c349c3175bb0cdc2fbc29c icedtea-2.2-branchpoint 56eb9150d9ffdb71c47d72871e8ecc98b5f402de jdk7u4-b13 0e55881c2ee2984048c179d1e031cefb56a36bec jdk7u4-b14 0bea057f7ce1577e1b0306f2027c057e35394398 jdk7u5-b01 @@ -186,11 +192,15 @@ 21d2313dfeac8c52a04b837d13958c86346a4b12 jdk7u6-b10 13d3c624291615593b4299a273085441b1dd2f03 jdk7u6-b11 f0be10a26af08c33d9afe8fe51df29572d431bac jdk7u6-b12 +e3eeee75b861baf378d41adcd29ae70ed047eae7 ppc-aix-port-b01 +e3eeee75b861baf378d41adcd29ae70ed047eae7 ppc-aix-port-b02 +e3eeee75b861baf378d41adcd29ae70ed047eae7 ppc-aix-port-b03 fcebf337f5c1d342973573d9c6f758443c8aefcf jdk7u6-b13 35b2699c6243e9fb33648c2c25e97ec91d0e3553 jdk7u6-b14 47ae28da508861d77ee6dd408d822acf507b28ec jdk7u6-b15 5c7763489f4d2727c6d9de11f4114fb8ed839042 jdk7u6-b16 66c671f28cb2840ceec5b44c44bac073fc0b4256 jdk7u6-b17 +cee31ee38a190f77b1e21c0515bb28802dcd9678 icedtea-2.3-branchpoint 6aa859ef42876c51bb1b1d7fb4db32a916a7dcaa jdk7u6-b18 474a52eeeafb1feccffda68b96f651e65415c01d jdk7u6-b19 32acb67a79531daf678577c7ef1bde1867da807d jdk7u6-b20 @@ -258,11 +268,13 @@ 382bab6d9682eefa2185a1643dfa32d65b6c20e5 jdk7u12-b07 7c0c3aeb2c603baba2cabba9adc5a0a49afb4f47 jdk7u12-b08 96c4f3ec63552a87a825baabd7f0dfafec299483 jdk7u12-b09 +85fb9d7ce4af53f0a47d2b73d983c96239f9ff33 icedtea-2.4-branchpoint e5b1403fa68abe3ac7174c031f19e6ecf77624a0 jdk7u14-b10 db94066df63468172e074d59e71d82dc874ed7cb jdk7u14-b11 f9a326e92fafc4724f0af550c2cba82fea202a31 jdk7u14-b12 5a52c6cc8db94b68eaacb42a9b4df30a40b09d82 jdk7u14-b13 5febc4e479fad801424cdcce90a0d463a2ef9223 jdk7u14-b14 +d52538e72925a1da7b1fcff051b591beeb2452b4 ppc-aix-port-b04 5fdb509d1f1a0533b14c61c92d77ff21e0ce2488 jdk7u14-b15 1298307076c2f0c2a4acd3a2a132cbe98d399009 jdk7u15-b01 8db0105f00ce9fe6899ece52d46d78995111c456 jdk7u15-b02 @@ -382,6 +394,7 @@ ba3ff27d4082f2cf0d06e635b2b6e01f80e78589 jdk7u45-b18 164cf7491ba2f371354ba343a604eee4c61c529d jdk7u45-b30 7f5cfaedb25c2c2774d6839810d6ae543557ca01 jdk7u45-b31 +849b17bc6e9a08fa41e0ef631e51366a09842e64 jdk7u60-b00 ef7bdbe7f1fa42fd58723e541d9cdedcacb2649a jdk7u45-b33 bcb3e939d046d75436c7c8511600b6edce42e6da jdk7u45-b34 efbda7abd821f280ec3a3aa6819ad62d45595e55 jdk7u45-b35 @@ -430,8 +443,11 @@ 849b17bc6e9a08fa41e0ef631e51366a09842e64 jdk7u60-b00 b19e375d9829daf207b1bdc7f908a3e1d548462c jdk7u60-b01 954e1616449af74f68aed57261cbeb62403377f1 jdk7u60-b02 +0d89cc5766d72e870eaf16696ec9b7b1ca4901fd icedtea-2.5pre01 +f75a642c2913e1ecbd22fc46812cffa2e7739169 icedtea-2.5pre02 4170784840d510b4e8ae7ae250b92279aaf5eb25 jdk7u60-b03 772aad4e9681828b8ee193b9ed971cbfe6c7f347 jdk7u60-b04 +702454ac1a074e81890fb07da06ebf00370e42ed icedtea-2.6pre01 744287fccf3b2c4fba2abf105863f0a44c3bd4da jdk7u60-b05 8f6db72756f3e4c3cca8731d20e978fb741846d2 jdk7u60-b06 02f050bc5569fb058ace44ed705bbb0f9022a6fe jdk7u60-b07 @@ -441,7 +457,11 @@ 3cc64ba8cf85942929b15c5ef21360f96db3b99c jdk7u60-b11 b79b8b1dc88faa73229b2bce04e979ff5ec854f5 jdk7u60-b12 3dc3e59e9580dfdf95dac57c54fe1a4209401125 jdk7u60-b13 +2040d4afc89815f6bf54a597ff58a70798b68e3d icedtea-2.6pre02 +2950924c2b80dc4d3933a8ab15a0ebb39522da5a icedtea-2.6pre03 a8b9c1929e50a9f3ae9ae1a23c06fa73a57afce3 jdk7u60-b14 +fa084876cf02f2f9996ad8a0ab353254f92c5564 icedtea-2.6pre04 +5f917c4b87a952a8bf79de08f3e2dd3e56c41657 icedtea-2.6pre05 7568ebdada118da1d1a6addcf6316ffda21801fd jdk7u60-b15 057caf9e0774e7c530c5710127f70c8d5f46deab jdk7u60-b16 b7cc00c573c294b144317d44803758a291b3deda jdk7u60-b17 @@ -581,10 +601,27 @@ e5e807700ff84f7bd9159ebc828891ae3ddb859c jdk7u79-b15 772aad4e9681828b8ee193b9ed971cbfe6c7f347 jdk7u80-b00 6c307a0b7a94e002d8a2532ffd8146d6c53f42d3 jdk7u80-b01 +3eab691bd9ac5222c11dbabb7b5fbc8463c62df6 icedtea-2.6pre07 +f43a81252f827395020fe71099bfa62f2ca0de50 icedtea-2.6pre06 +cdf407c97754412b02ebfdda111319dbd3cb9ca9 icedtea-2.6pre08 5bd6f3adf690dc2de8881b6f9f48336db4af7865 jdk7u80-b02 +55486a406d9f111eea8996fdf6144befefd86aff icedtea-2.6pre09 +cf836e0ed10de1179ec398a7db323e702b60ca35 icedtea-2.6pre10 +510234036e06ec8d7ed2a39ee11faf1b9a4257b0 icedtea-2.6pre11 bcbd241df6cd0a643480c8de183c541a662dd506 jdk7u80-b03 +987d772301e91c896178f47f39d82d87e9da1e39 icedtea-2.6pre12 +a072de9f83ed85a6a86d052d13488009230d7d4b icedtea-2.6pre13 +ecf2ec173dd2c19b63d7cf543db23ec7d4f4732a icedtea-2.6pre14 +029dd486cd1a8f6d7684b1633aae41c613055dd2 icedtea-2.6pre15 +c802d4cdd4cbfa8116e4f612cf536de32d67221a icedtea-2.6pre16 +e1dd8fea9abd3663838008063715b4b7ab5a58a4 icedtea-2.6pre17 04b56f4312b62d8bdf4eb1159132de8437994d34 jdk7u80-b04 f40fb76025c798cab4fb0e1966be1bceb8234527 jdk7u80-b05 +bb9d09219d3e74954b46ad53cb99dc307e39e120 icedtea-2.6pre18 +4c600e18a7e415702f6a62073c8c60f6b2cbfc11 icedtea-2.6pre19 +1a60fa408f57762abe32f19e4f3d681fb9c4960b icedtea-2.6pre20 +5331b041c88950058f8bd8e9669b9763be6ee03f icedtea-2.6pre21 +a322987c412f5f8584b15fab0a4505b94c016c22 icedtea-2.6pre22 335ee524dc68a42863f3fa3f081b781586e7ba2d jdk7u80-b06 6f7b359c4e9f82cbd399edc93c3275c3e668d2ea jdk7u80-b07 e6db2a97b3696fb5e7786b23f77af346a935a370 jdk7u80-b08 @@ -597,47 +634,86 @@ d0cc1c8ace99283d7b2354d2c0e5cd58787163c8 jdk7u80-b30 f2b4d5e42318ed93d35006ff7d1b3b0313b5a71f jdk7u80-b15 f1ffea3bd4a4df0f74ce0c127aeacf6bd11ee612 jdk7u80-b32 +403eeedf70f4b0e3c88f094d324e5c85959610e2 icedtea-2.6pre23 +bc95d2472055d96a712db09ecd8ab42e52058481 icedtea-2.6pre24 +bc95d2472055d96a712db09ecd8ab42e52058481 icedtea-2.6.0 1b20ca77fa98bb29d1f5601f027b3055e9eb28ee jdk7u85-b00 dce5a828bdd56d228724f1e9c6253920f613cec5 jdk7u85-b01 +bc95d2472055d96a712db09ecd8ab42e52058481 icedtea-2.6-branchpoint +9c6e1de67d7d26809d02c8ce3d6629503cb67d19 icedtea-2.6.1 b22cdae823bac193338d928e86319cd3741ab5fd jdk7u85-b02 +aef681a80dc1e8a8b69c1a06b463bda7999801ea icedtea-2.6.2pre01 +d627a940b6ca8fb4353f844e4f91163a3dcde0bc icedtea-2.6.2pre02 2741575d96f3985d41de8ebe1ba7fae8afbb0fde jdk7u91-b00 1a9e2dcc91dc3d0c103b09c478b3ac31ac45733f jdk7u91-b01 +73356b81c5c773a29729ae3b641516e0ac4a015d icedtea-2.6.2 08e99c45e470ce8b87875c1cbe78ac2f341555a3 jdk7u91-b02 +91fdb0c83e50c398bee5f0550600d20650f2a6ef icedtea-2.6.3 3c71abf7435352aee6e74ba2581274181ad3d17e jdk7u95-b00 +fd0a34cb97b40c622fc6d3370f5eca062e280979 icedtea-2.6.4 93a2788178e6ebebfbd30075f51ab35ac4f1b2a1 jdk7u99-b00 +f6593c32cc46f60438804b6a5b0d61e532f4a235 icedtea-2.6.5 +83f998d6c0afda7f3882ecc03ea873d988174c19 icedtea-2.6.6pre01 5713b8d2db3f1fc0c9802fdaf30ca802cddf8f65 jdk7u101-b00 +e42dd50480d3aab87668202ed79fb87f2dedcb91 icedtea-2.6.6 +264448dfed4e1bddd2da25aa3d3976bcabbb9ad6 icedtea-2.6.7pre01 e65eb66727550af75293996fc42e2c49c2002659 jdk7u111-b00 a5002845bff276d1a8e4bdb4a0dcb972509f7d07 jdk7u111-b01 +ca9d8b242a10f1987375b8eca38b368ee23db334 icedtea-2.6.7 +2e6c46aea99fdd650e5ff40877a98f466361e9dc icedtea-2.6.8pre01 3715f0221fd743e878ae5198dbaebe61ebd4fa97 jdk7u121-b00 +545e512eb4de948a44e6dd2187f386c644149aa7 icedtea-2.6.8 +4f7f0c054b2d447b4a283810abbba7a0558f0a87 icedtea-2.6.9pre01 240ca8086e7151b5c516b5b46d84201e4e8f5806 jdk7u131-b00 +dd8e22d986c8b0f50c25b1a35f981933a926d992 icedtea-2.6.9 +fd8dde40d9dd105f07880bd002feb73555fbb16b icedtea-2.6.10pre01 b1802aa8be3a49d2a5c07160f2406165e309efde jdk7u141-b00 853798299f7a4f38745632dc256219fbf75f861e jdk7u141-b01 ba8ba083c4392a1ddfe40467de1e08d77443807e jdk7u141-b02 +6e30439005f6e6a63661192738e27e8d374d72d8 icedtea-2.6.10 +a4814b6ff893d36aea91ccb16990d8a34840c07f icedtea-2.6.11pre01 4624941508c189c7b81c3393664741640a9b0467 jdk7u151-b00 205349c408f4715dbbaf428ceafca676e2333979 jdk7u151-b01 +cddb1f9f8b9cee5f9c54ececf59ecb51e1bbb730 icedtea-2.6.11 +e24f25323b84858f039ff1a780805ca93dd60af8 icedtea-2.6.12pre01 6f72d747fa58d54e2d2e27755c0bcf2c7291682f jdk7u161-b00 e162e8d76cdd9f579e25f307f5485550fec2d024 jdk7u161-b01 +b87dbe0db6aa70ffb0443b5ef55e696e0ec5b124 icedtea-2.6.12 551dfae6260d4cd2cd929e14934b031d9319ad52 jdk7u171-b00 89a92f5cd9c112dc8f73d2cd25a54a437b753642 jdk7u171-b01 a10f2bfd1ce34bbbfd959a371eb709cc113039f2 jdk7u171-b02 +e952982cfa000f6fd9186d784112417148d33206 icedtea-2.6.13 +870812ffc2c6d6c2a1b1eddb53f9b9cb4eebf02a icedtea-2.6.14pre01 1f82b6ff3455cb2408f4501c1a12e911271d10fb jdk7u181-b00 1c8c4da612ee78fef56eb892921d6f228f99e6b7 jdk7u181-b01 +3633e24edab763325da38137161d8a74c2d5b07b icedtea-2.6.14 +7c19cbad92914c9dc1c270c70892a4cfb78063e5 icedtea-2.6.15pre01 f58aabbb507d5f48c110ab5aa75857da700deae3 jdk7u191-b00 2709f9b3b97e2a774a78cb3eeeacb68efce45cc2 jdk7u191-b01 31641d1885f155db206d7097ee13716d4c800c24 jdk7u191-b02 +5d348df3700daa615c3e503b12f93134a2ff9c54 icedtea-2.6.15 f1f14c413b20f1d4abef840d65587d2839f6cfdf jdk7u201-b00 +1b532e3cc1da0b36b43fa03a28ec10a08e83523a icedtea-2.6.16 +8e1ace09837f1310ccfc5cb5d42ca8a1017485d0 icedtea-2.6.17pre01 353ea2f317ff940242316d0069c2239d178bd7d3 jdk7u211-b00 b8196c545f3e11c704c020d89a4e61cea54fb4f7 jdk7u211-b01 b22fd0eed3e1a00754635ab45799ed6c4c49645b jdk7u211-b02 +6cdbf382f3e28112b6ec3aa3f90be2df532ec656 icedtea-2.6.17 +6cdbf382f3e28112b6ec3aa3f90be2df532ec656 icedtea-2.6.18pre00 4bd7f8263d2f7116697d82e6bb9029a5d5163310 jdk7u221-b00 940b8aa665d443ad7d166bcd341b35572ce614fd jdk7u221-b01 64fe722a5b06a6f7e5346e8ced98503f0239e509 jdk7u221-b02 +9f0890e75bcb68daebe48fc74015a2564dbac220 icedtea-2.6.18 64fe722a5b06a6f7e5346e8ced98503f0239e509 jdk7u221-ga 64fe722a5b06a6f7e5346e8ced98503f0239e509 jdk7u231-b00 54de827b5db66fb0fd25dc492db1f25aa87c8e11 jdk7u231-b01 +219e16f3659b8c87bd3c1751152c95ff2fd83cf9 icedtea-2.6.19 +219e16f3659b8c87bd3c1751152c95ff2fd83cf9 icedtea-2.6.20pre00 54de827b5db66fb0fd25dc492db1f25aa87c8e11 jdk7u231-ga 54de827b5db66fb0fd25dc492db1f25aa87c8e11 jdk7u241-b00 b317ddb0ec6dc011d1a64dcf2090465c70d57c14 jdk7u241-b01 +77f4748f44065feae19e1fc17c8c69349b1bdc8d icedtea-2.6.20 +480d3ab02c3659dc724a16bdf06d52c715fce27b icedtea-2.6.21pre00 b317ddb0ec6dc011d1a64dcf2090465c70d57c14 jdk7u241-ga b317ddb0ec6dc011d1a64dcf2090465c70d57c14 jdk7u251-b00 2e0245ff42c1702fd083ada5387ac98d41307feb jdk7u251-b01
--- a/.jcheck/conf Wed Feb 05 07:28:48 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,2 +0,0 @@ -project=jdk7 -bugids=dup
--- a/make/Makefile Wed Feb 05 07:28:48 2020 +0000 +++ b/make/Makefile Thu Feb 06 01:43:02 2020 +0000 @@ -111,6 +111,10 @@ ANT_OPTIONS += -Ddebug.classfiles=true endif +ifeq ($(JAVAC_WARNINGS_FATAL), false) + ANT_OPTIONS += -Djavac.warnings.fatal= +endif + # Note: jdk/make/common/Defs.gmk uses LANGUAGE_VERSION (-source NN) # and the somewhat misnamed CLASS_VERSION (-target NN) ifdef TARGET_CLASS_VERSION
--- a/make/build.properties Wed Feb 05 07:28:48 2020 +0000 +++ b/make/build.properties Thu Feb 06 01:43:02 2020 +0000 @@ -68,7 +68,8 @@ # set the following to -version to verify the versions of javac being used javac.version.opt = # in time, there should be no exceptions to -Xlint:all -javac.lint.opts = -Xlint:all,-deprecation -Werror +javac.warnings.fatal = -Werror +javac.lint.opts = -Xlint:all,-deprecation ${javac.warnings.fatal} # options for the <javadoc> task for javac #javadoc.jls3.url=http://java.sun.com/docs/books/jls/
--- a/make/build.xml Wed Feb 05 07:28:48 2020 +0000 +++ b/make/build.xml Thu Feb 06 01:43:02 2020 +0000 @@ -877,7 +877,7 @@ </javac> <taskdef name="genstubs" classname="GenStubs$$Ant" - classpath="${build.toolclasses.dir}/"/> + classpath="${build.toolclasses.dir}:${build.bootstrap.dir}/classes:${ant.home}/lib/ant.jar"/> </target> <target name="-def-javadoc-tool" depends="-check-target.java.home">
--- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Wed Feb 05 07:28:48 2020 +0000 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Thu Feb 06 01:43:02 2020 +0000 @@ -1962,10 +1962,15 @@ } else { JCExpression t = term(EXPR | TYPE); if ((lastmode & TYPE) != 0 && - (S.token() == IDENTIFIER || S.token() == ASSERT || S.token() == ENUM)) - return variableDeclarators(modifiersOpt(), t, stats).toList(); - else + (S.token() == IDENTIFIER || S.token() == ASSERT || + S.token() == ENUM)) { + return variableDeclarators(mods(pos, 0, List.<JCAnnotation>nil()), t, stats).toList(); + } else if ((lastmode & TYPE) != 0 && S.token() == COLON) { + error(pos, "bad.initializer", "for-loop"); + return List.of((JCStatement)F.at(pos).VarDef(null, null, t, null)); + } else { return moreStatementExpressions(pos, t, stats).toList(); + } } } @@ -2057,16 +2062,20 @@ default: break; } - /* A modifiers tree with no modifier tokens or annotations - * has no text position. */ - if ((flags & (Flags.ModifierFlags | Flags.ANNOTATION)) == 0 && annotations.isEmpty()) - pos = Position.NOPOS; + return mods(pos, flags, annotations.toList()); + } + //where + JCModifiers mods(int pos, long flags, List<JCAnnotation> annotations) { + /* A modifiers tree with no modifier tokens or annotations + * has no text position. */ + if ((flags & (Flags.ModifierFlags | Flags.ANNOTATION)) == 0 && annotations.isEmpty()) + pos = Position.NOPOS; - JCModifiers mods = F.at(pos).Modifiers(flags, annotations.toList()); - if (pos != Position.NOPOS) - storeEnd(mods, S.prevEndPos()); - return mods; - } + JCModifiers mods = F.at(pos).Modifiers(flags, annotations); + if (pos != Position.NOPOS) + storeEnd(mods, S.prevEndPos()); + return mods; + } /** Annotation = "@" Qualident [ "(" AnnotationFieldValues ")" ] * @param pos position of "@" token
--- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties Wed Feb 05 07:28:48 2020 +0000 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties Thu Feb 06 01:43:02 2020 +0000 @@ -143,6 +143,10 @@ compiler.err.attribute.value.must.be.constant=\ attribute value must be constant +# 0: statement type +compiler.err.bad.initializer=\ + bad initializer for {0} + compiler.err.break.outside.switch.loop=\ break outside switch or loop
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/diags/examples/ForeachBadInitialization.java Thu Feb 06 01:43:02 2020 +0000 @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +// key: compiler.err.bad.initializer +import java.util.List; +class ForeachBadInitialization { + void m() { + List<String> s = null; + for (a : s) {} + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/enum/8069181/T8069181.java Thu Feb 06 01:43:02 2020 +0000 @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 8069181 + * @summary java.lang.AssertionError when compiling JDK 1.4 code in JDK 8 + * + * @compile -source 1.4 T8069181.java + */ +import java.util.Enumeration; +import java.util.Hashtable; +class T8069181 { + void test() { + Hashtable hTable = new Hashtable(); + hTable.put("hello", "value"); + for (Enumeration enum = hTable.keys();;){ + if(!enum.hasMoreElements()) + break; + enum.nextElement(); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/parser/JavacParserTest.java Thu Feb 06 01:43:02 2020 +0000 @@ -0,0 +1,886 @@ +/* + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @bug 7073631 7159445 + * @summary tests error and diagnostics positions + * @author Jan Lahoda + */ + +import com.sun.source.tree.BinaryTree; +import com.sun.source.tree.BlockTree; +import com.sun.source.tree.ClassTree; +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.ErroneousTree; +import com.sun.source.tree.ExpressionStatementTree; +import com.sun.source.tree.ExpressionTree; +import com.sun.source.tree.MethodInvocationTree; +import com.sun.source.tree.MethodTree; +import com.sun.source.tree.ModifiersTree; +import com.sun.source.tree.StatementTree; +import com.sun.source.tree.Tree; +import com.sun.source.tree.Tree.Kind; +import com.sun.source.tree.VariableTree; +import com.sun.source.tree.WhileLoopTree; +import com.sun.source.util.SourcePositions; +import com.sun.source.util.TreeScanner; +import com.sun.source.util.Trees; +import com.sun.tools.javac.api.JavacTaskImpl; +import com.sun.tools.javac.tree.JCTree; +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import javax.tools.Diagnostic; +import javax.tools.DiagnosticCollector; +import javax.tools.DiagnosticListener; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.SimpleJavaFileObject; +import javax.tools.ToolProvider; + +public class JavacParserTest extends TestCase { + final JavaCompiler tool; + public JavacParserTest(String testName) { + tool = ToolProvider.getSystemJavaCompiler(); + System.out.println("java.home=" + System.getProperty("java.home")); + } + + static class MyFileObject extends SimpleJavaFileObject { + + private String text; + + public MyFileObject(String text) { + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); + this.text = text; + } + + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) { + return text; + } + } + /* + * converts Windows to Unix style LFs for comparing strings + */ + private String normalize(String in) { + return in.replace(System.getProperty("line.separator"), "\n"); + } + + public CompilationUnitTree getCompilationUnitTree(String code) throws IOException { + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, + null, Arrays.asList(new MyFileObject(code))); + CompilationUnitTree cut = ct.parse().iterator().next(); + return cut; + } + + public List<String> getErroneousTreeValues(ErroneousTree node) { + + List<String> values = new ArrayList<>(); + if (node.getErrorTrees() != null) { + for (Tree t : node.getErrorTrees()) { + values.add(t.toString()); + } + } else { + throw new RuntimeException("ERROR: No Erroneous tree " + + "has been created."); + } + return values; + } + + public void testPositionForSuperConstructorCalls() throws IOException { + assert tool != null; + + String code = "package test; public class Test {public Test() {super();}}"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, + null, Arrays.asList(new MyFileObject(code))); + CompilationUnitTree cut = ct.parse().iterator().next(); + SourcePositions pos = Trees.instance(ct).getSourcePositions(); + + MethodTree method = + (MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); + ExpressionStatementTree es = + (ExpressionStatementTree) method.getBody().getStatements().get(0); + + final int esStartPos = code.indexOf(es.toString()); + final int esEndPos = esStartPos + es.toString().length(); + assertEquals("testPositionForSuperConstructorCalls", + esStartPos, pos.getStartPosition(cut, es)); + assertEquals("testPositionForSuperConstructorCalls", + esEndPos, pos.getEndPosition(cut, es)); + + MethodInvocationTree mit = (MethodInvocationTree) es.getExpression(); + + final int mitStartPos = code.indexOf(mit.toString()); + final int mitEndPos = mitStartPos + mit.toString().length(); + assertEquals("testPositionForSuperConstructorCalls", + mitStartPos, pos.getStartPosition(cut, mit)); + assertEquals("testPositionForSuperConstructorCalls", + mitEndPos, pos.getEndPosition(cut, mit)); + + final int methodStartPos = mitStartPos; + final int methodEndPos = methodStartPos + mit.getMethodSelect().toString().length(); + assertEquals("testPositionForSuperConstructorCalls", + methodStartPos, pos.getStartPosition(cut, mit.getMethodSelect())); + assertEquals("testPositionForSuperConstructorCalls", + methodEndPos, pos.getEndPosition(cut, mit.getMethodSelect())); + + } + + public void testPositionForEnumModifiers() throws IOException { + + String code = "package test; public enum Test {A;}"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, + null, Arrays.asList(new MyFileObject(code))); + CompilationUnitTree cut = ct.parse().iterator().next(); + SourcePositions pos = Trees.instance(ct).getSourcePositions(); + + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + ModifiersTree mt = clazz.getModifiers(); + + assertEquals("testPositionForEnumModifiers", + 38 - 24, pos.getStartPosition(cut, mt)); + assertEquals("testPositionForEnumModifiers", + 44 - 24, pos.getEndPosition(cut, mt)); + } + + public void testNewClassWithEnclosing() throws IOException { + + + String code = "package test; class Test { " + + "class d {} private void method() { " + + "Object o = Test.this.new d(); } }"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, + null, Arrays.asList(new MyFileObject(code))); + CompilationUnitTree cut = ct.parse().iterator().next(); + SourcePositions pos = Trees.instance(ct).getSourcePositions(); + + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + ExpressionTree est = + ((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer(); + + assertEquals("testNewClassWithEnclosing", + 97 - 24, pos.getStartPosition(cut, est)); + assertEquals("testNewClassWithEnclosing", + 114 - 24, pos.getEndPosition(cut, est)); + } + + public void testPreferredPositionForBinaryOp() throws IOException { + + String code = "package test; public class Test {" + + "private void test() {" + + "Object o = null; boolean b = o != null && o instanceof String;" + + "} private Test() {}}"; + + CompilationUnitTree cut = getCompilationUnitTree(code); + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + MethodTree method = (MethodTree) clazz.getMembers().get(0); + VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1); + BinaryTree cond = (BinaryTree) condSt.getInitializer(); + + JCTree condJC = (JCTree) cond; + int condStartPos = code.indexOf("&&"); + assertEquals("testPreferredPositionForBinaryOp", + condStartPos, condJC.pos); + } + + public void testPositionBrokenSource126732a() throws IOException { + String[] commands = new String[]{ + "return Runnable()", + "do { } while (true)", + "throw UnsupportedOperationException()", + "assert true", + "1 + 1",}; + + for (String command : commands) { + + String code = "package test;\n" + + "public class Test {\n" + + " public static void test() {\n" + + " " + command + " {\n" + + " new Runnable() {\n" + + " };\n" + + " }\n" + + "}"; + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, + null, null, Arrays.asList(new MyFileObject(code))); + CompilationUnitTree cut = ct.parse().iterator().next(); + + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + MethodTree method = (MethodTree) clazz.getMembers().get(0); + List<? extends StatementTree> statements = + method.getBody().getStatements(); + + StatementTree ret = statements.get(0); + StatementTree block = statements.get(1); + + Trees t = Trees.instance(ct); + int len = code.indexOf(command + " {") + (command + " ").length(); + assertEquals(command, len, + t.getSourcePositions().getEndPosition(cut, ret)); + assertEquals(command, len, + t.getSourcePositions().getStartPosition(cut, block)); + } + } + + public void testPositionBrokenSource126732b() throws IOException { + String[] commands = new String[]{ + "break", + "break A", + "continue ", + "continue A",}; + + for (String command : commands) { + + String code = "package test;\n" + + "public class Test {\n" + + " public static void test() {\n" + + " while (true) {\n" + + " " + command + " {\n" + + " new Runnable() {\n" + + " };\n" + + " }\n" + + " }\n" + + "}"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, + null, null, Arrays.asList(new MyFileObject(code))); + CompilationUnitTree cut = ct.parse().iterator().next(); + + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + MethodTree method = (MethodTree) clazz.getMembers().get(0); + List<? extends StatementTree> statements = + ((BlockTree) ((WhileLoopTree) method.getBody().getStatements().get(0)).getStatement()).getStatements(); + + StatementTree ret = statements.get(0); + StatementTree block = statements.get(1); + + Trees t = Trees.instance(ct); + int len = code.indexOf(command + " {") + (command + " ").length(); + assertEquals(command, len, + t.getSourcePositions().getEndPosition(cut, ret)); + assertEquals(command, len, + t.getSourcePositions().getStartPosition(cut, block)); + } + } + + public void testErrorRecoveryForEnhancedForLoop142381() throws IOException { + + String code = "package test; class Test { " + + "private void method() { " + + "java.util.Set<String> s = null; for (a : s) {} } }"; + + final List<Diagnostic<? extends JavaFileObject>> errors = + new LinkedList<Diagnostic<? extends JavaFileObject>>(); + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, + new DiagnosticListener<JavaFileObject>() { + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { + errors.add(diagnostic); + } + }, null, null, Arrays.asList(new MyFileObject(code))); + + CompilationUnitTree cut = ct.parse().iterator().next(); + + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + StatementTree forStatement = + ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1); + + assertEquals("testErrorRecoveryForEnhancedForLoop142381", + Kind.ENHANCED_FOR_LOOP, forStatement.getKind()); + assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty()); + } + + public void testPositionAnnotationNoPackage187551() throws IOException { + + String code = "\n@interface Test {}"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, + null, Arrays.asList(new MyFileObject(code))); + + CompilationUnitTree cut = ct.parse().iterator().next(); + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + Trees t = Trees.instance(ct); + + assertEquals("testPositionAnnotationNoPackage187551", + 1, t.getSourcePositions().getStartPosition(cut, clazz)); + } + + public void testPositionsSane() throws IOException { + performPositionsSanityTest("package test; class Test { " + + "private void method() { " + + "java.util.List<? extends java.util.List<? extends String>> l; " + + "} }"); + performPositionsSanityTest("package test; class Test { " + + "private void method() { " + + "java.util.List<? super java.util.List<? super String>> l; " + + "} }"); + performPositionsSanityTest("package test; class Test { " + + "private void method() { " + + "java.util.List<? super java.util.List<?>> l; } }"); + } + + private void performPositionsSanityTest(String code) throws IOException { + + final List<Diagnostic<? extends JavaFileObject>> errors = + new LinkedList<Diagnostic<? extends JavaFileObject>>(); + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, + new DiagnosticListener<JavaFileObject>() { + + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { + errors.add(diagnostic); + } + }, null, null, Arrays.asList(new MyFileObject(code))); + + final CompilationUnitTree cut = ct.parse().iterator().next(); + final Trees trees = Trees.instance(ct); + + new TreeScanner<Void, Void>() { + + private long parentStart = 0; + private long parentEnd = Integer.MAX_VALUE; + + @Override + public Void scan(Tree node, Void p) { + if (node == null) { + return null; + } + + long start = trees.getSourcePositions().getStartPosition(cut, node); + + if (start == (-1)) { + return null; //synthetic tree + } + assertTrue(node.toString() + ":" + start + "/" + parentStart, + parentStart <= start); + + long prevParentStart = parentStart; + + parentStart = start; + + long end = trees.getSourcePositions().getEndPosition(cut, node); + + assertTrue(node.toString() + ":" + end + "/" + parentEnd, + end <= parentEnd); + + long prevParentEnd = parentEnd; + + parentEnd = end; + + super.scan(node, p); + + parentStart = prevParentStart; + parentEnd = prevParentEnd; + + return null; + } + + private void assertTrue(String message, boolean b) { + if (!b) fail(message); + } + }.scan(cut, null); + } + + public void testCorrectWilcardPositions() throws IOException { + performWildcardPositionsTest("package test; import java.util.List; " + + "class Test { private void method() { List<? extends List<? extends String>> l; } }", + + Arrays.asList("List<? extends List<? extends String>> l;", + "List<? extends List<? extends String>>", + "List", + "? extends List<? extends String>", + "List<? extends String>", + "List", + "? extends String", + "String")); + performWildcardPositionsTest("package test; import java.util.List; " + + "class Test { private void method() { List<? super List<? super String>> l; } }", + + Arrays.asList("List<? super List<? super String>> l;", + "List<? super List<? super String>>", + "List", + "? super List<? super String>", + "List<? super String>", + "List", + "? super String", + "String")); + performWildcardPositionsTest("package test; import java.util.List; " + + "class Test { private void method() { List<? super List<?>> l; } }", + + Arrays.asList("List<? super List<?>> l;", + "List<? super List<?>>", + "List", + "? super List<?>", + "List<?>", + "List", + "?")); + performWildcardPositionsTest("package test; import java.util.List; " + + "class Test { private void method() { " + + "List<? extends List<? extends List<? extends String>>> l; } }", + + Arrays.asList("List<? extends List<? extends List<? extends String>>> l;", + "List<? extends List<? extends List<? extends String>>>", + "List", + "? extends List<? extends List<? extends String>>", + "List<? extends List<? extends String>>", + "List", + "? extends List<? extends String>", + "List<? extends String>", + "List", + "? extends String", + "String")); + performWildcardPositionsTest("package test; import java.util.List; " + + "class Test { private void method() { " + + "List<? extends List<? extends List<? extends String >>> l; } }", + Arrays.asList("List<? extends List<? extends List<? extends String >>> l;", + "List<? extends List<? extends List<? extends String >>>", + "List", + "? extends List<? extends List<? extends String >>", + "List<? extends List<? extends String >>", + "List", + "? extends List<? extends String >", + "List<? extends String >", + "List", + "? extends String", + "String")); + } + + public void performWildcardPositionsTest(final String code, + List<String> golden) throws IOException { + + final List<Diagnostic<? extends JavaFileObject>> errors = + new LinkedList<Diagnostic<? extends JavaFileObject>>(); + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, + new DiagnosticListener<JavaFileObject>() { + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { + errors.add(diagnostic); + } + }, null, null, Arrays.asList(new MyFileObject(code))); + + final CompilationUnitTree cut = ct.parse().iterator().next(); + final List<String> content = new LinkedList<String>(); + final Trees trees = Trees.instance(ct); + + new TreeScanner<Void, Void>() { + @Override + public Void scan(Tree node, Void p) { + if (node == null) { + return null; + } + long start = trees.getSourcePositions().getStartPosition(cut, node); + + if (start == (-1)) { + return null; //synthetic tree + } + long end = trees.getSourcePositions().getEndPosition(cut, node); + String s = code.substring((int) start, (int) end); + content.add(s); + + return super.scan(node, p); + } + }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null); + + assertEquals("performWildcardPositionsTest",golden.toString(), + content.toString()); + } + + public void testStartPositionForMethodWithoutModifiers() throws IOException { + + String code = "package t; class Test { <T> void t() {} }"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, + null, Arrays.asList(new MyFileObject(code))); + CompilationUnitTree cut = ct.parse().iterator().next(); + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + MethodTree mt = (MethodTree) clazz.getMembers().get(0); + Trees t = Trees.instance(ct); + int start = (int) t.getSourcePositions().getStartPosition(cut, mt); + int end = (int) t.getSourcePositions().getEndPosition(cut, mt); + + assertEquals("testStartPositionForMethodWithoutModifiers", + "<T> void t() {}", code.substring(start, end)); + } + + public void testStartPositionEnumConstantInit() throws IOException { + + String code = "package t; enum Test { AAA; }"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, + null, Arrays.asList(new MyFileObject(code))); + CompilationUnitTree cut = ct.parse().iterator().next(); + ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); + VariableTree enumAAA = (VariableTree) clazz.getMembers().get(0); + Trees t = Trees.instance(ct); + int start = (int) t.getSourcePositions().getStartPosition(cut, + enumAAA.getInitializer()); + + assertEquals("testStartPositionEnumConstantInit", -1, start); + } + + public void testVariableInIfThen1() throws IOException { + + String code = "package t; class Test { " + + "private static void t(String name) { " + + "if (name != null) String nn = name.trim(); } }"; + + DiagnosticCollector<JavaFileObject> coll = + new DiagnosticCollector<JavaFileObject>(); + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, + null, Arrays.asList(new MyFileObject(code))); + + ct.parse(); + + List<String> codes = new LinkedList<String>(); + + for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { + codes.add(d.getCode()); + } + + assertEquals("testVariableInIfThen1", + Arrays.<String>asList("compiler.err.variable.not.allowed"), + codes); + } + + public void testVariableInIfThen2() throws IOException { + + String code = "package t; class Test { " + + "private static void t(String name) { " + + "if (name != null) class X {} } }"; + DiagnosticCollector<JavaFileObject> coll = + new DiagnosticCollector<JavaFileObject>(); + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, + null, Arrays.asList(new MyFileObject(code))); + + ct.parse(); + + List<String> codes = new LinkedList<String>(); + + for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { + codes.add(d.getCode()); + } + + assertEquals("testVariableInIfThen2", + Arrays.<String>asList("compiler.err.class.not.allowed"), codes); + } + + public void testVariableInIfThen3() throws IOException { + + String code = "package t; class Test { "+ + "private static void t(String name) { " + + "if (name != null) abstract } }"; + DiagnosticCollector<JavaFileObject> coll = + new DiagnosticCollector<JavaFileObject>(); + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, + null, Arrays.asList(new MyFileObject(code))); + + ct.parse(); + + List<String> codes = new LinkedList<String>(); + + for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { + codes.add(d.getCode()); + } + + assertEquals("testVariableInIfThen3", + Arrays.<String>asList("compiler.err.illegal.start.of.expr"), + codes); + } + + //see javac bug #6882235, NB bug #98234: + public void testMissingExponent() throws IOException { + + String code = "\nclass Test { { System.err.println(0e); } }"; + + JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, + null, Arrays.asList(new MyFileObject(code))); + + assertNotNull(ct.parse().iterator().next()); + } + + public void testTryResourcePos() throws IOException { + + final String code = "package t; class Test { " + + "{ try (java.io.InputStream in = null) { } } }"; + + CompilationUnitTree cut = getCompilationUnitTree(code); + + new TreeScanner<Void, Void>() { + @Override + public Void visitVariable(VariableTree node, Void p) { + if ("in".contentEquals(node.getName())) { + JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; + System.out.println(node.getName() + "," + var.pos); + assertEquals("testTryResourcePos", "in = null) { } } }", + code.substring(var.pos)); + } + return super.visitVariable(node, p); + } + }.scan(cut, null); + } + + public void testVarPos() throws IOException { + + final String code = "package t; class Test { " + + "{ java.io.InputStream in = null; } }"; + + CompilationUnitTree cut = getCompilationUnitTree(code); + + new TreeScanner<Void, Void>() { + + @Override + public Void visitVariable(VariableTree node, Void p) { + if ("in".contentEquals(node.getName())) { + JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; + assertEquals("testVarPos","in = null; } }", + code.substring(var.pos)); + } + return super.visitVariable(node, p); + } + }.scan(cut, null); + } + + // expected erroneous tree: int x = y;(ERROR); + public void testOperatorMissingError() throws IOException { + + String code = "package test; public class ErrorTest { " + + "void method() { int x = y z } }"; + CompilationUnitTree cut = getCompilationUnitTree(code); + final List<String> values = new ArrayList<>(); + final List<String> expectedValues = + new ArrayList<>(Arrays.asList("[z]")); + + new TreeScanner<Void, Void>() { + + @Override + public Void visitErroneous(ErroneousTree node, Void p) { + + values.add(getErroneousTreeValues(node).toString()); + return null; + + } + }.scan(cut, null); + + assertEquals("testSwitchError: The Erroneous tree " + + "error values: " + values + + " do not match expected error values: " + + expectedValues, values, expectedValues); + } + + //expected erroneous tree: String s = (ERROR); + public void testMissingParenthesisError() throws IOException { + + String code = "package test; public class ErrorTest { " + + "void f() {String s = new String; } }"; + CompilationUnitTree cut = getCompilationUnitTree(code); + final List<String> values = new ArrayList<>(); + final List<String> expectedValues = + new ArrayList<>(Arrays.asList("[new String()]")); + + new TreeScanner<Void, Void>() { + + @Override + public Void visitErroneous(ErroneousTree node, Void p) { + + values.add(getErroneousTreeValues(node).toString()); + return null; + } + }.scan(cut, null); + + assertEquals("testSwitchError: The Erroneous tree " + + "error values: " + values + + " do not match expected error values: " + + expectedValues, values, expectedValues); + } + + //expected erroneous tree: package test; (ERROR)(ERROR) + public void testMissingClassError() throws IOException { + + String code = "package Test; clas ErrorTest { " + + "void f() {String s = new String(); } }"; + CompilationUnitTree cut = getCompilationUnitTree(code); + final List<String> values = new ArrayList<>(); + final List<String> expectedValues = + new ArrayList<>(Arrays.asList("[, clas]", "[]")); + + new TreeScanner<Void, Void>() { + + @Override + public Void visitErroneous(ErroneousTree node, Void p) { + + values.add(getErroneousTreeValues(node).toString()); + return null; + } + }.scan(cut, null); + + assertEquals("testSwitchError: The Erroneous tree " + + "error values: " + values + + " do not match expected error values: " + + expectedValues, values, expectedValues); + } + + //expected erroneous tree: void m1(int i) {(ERROR);{(ERROR);} + public void testSwitchError() throws IOException { + + String code = "package test; public class ErrorTest { " + + "int numDays; void m1(int i) { switchh {i} { case 1: " + + "numDays = 31; break; } } }"; + CompilationUnitTree cut = getCompilationUnitTree(code); + final List<String> values = new ArrayList<>(); + final List<String> expectedValues = + new ArrayList<>(Arrays.asList("[switchh]", "[i]")); + + new TreeScanner<Void, Void>() { + + @Override + public Void visitErroneous(ErroneousTree node, Void p) { + + values.add(getErroneousTreeValues(node).toString()); + return null; + } + }.scan(cut, null); + + assertEquals("testSwitchError: The Erroneous tree " + + "error values: " + values + + " do not match expected error values: " + + expectedValues, values, expectedValues); + } + + //expected erroneous tree: class ErrorTest {(ERROR) + public void testMethodError() throws IOException { + + String code = "package Test; class ErrorTest { " + + "static final void f) {String s = new String(); } }"; + CompilationUnitTree cut = getCompilationUnitTree(code); + final List<String> values = new ArrayList<>(); + final List<String> expectedValues = + new ArrayList<>(Arrays.asList("[\nstatic final void f();]")); + + new TreeScanner<Void, Void>() { + + @Override + public Void visitErroneous(ErroneousTree node, Void p) { + + values.add(normalize(getErroneousTreeValues(node).toString())); + return null; + } + }.scan(cut, null); + + assertEquals("testMethodError: The Erroneous tree " + + "error value: " + values + + " does not match expected error values: " + + expectedValues, values, expectedValues); + } + + void testsNotWorking() throws IOException { + + // Fails with nb-javac, needs further investigation + testPositionBrokenSource126732a(); + testPositionBrokenSource126732b(); + + // Fails, these tests yet to be addressed + testVariableInIfThen1(); + testVariableInIfThen2(); + testPositionForEnumModifiers(); + testStartPositionEnumConstantInit(); + } + void testPositions() throws IOException { + testPositionsSane(); + testCorrectWilcardPositions(); + testPositionAnnotationNoPackage187551(); + testPositionForSuperConstructorCalls(); + testPreferredPositionForBinaryOp(); + testStartPositionForMethodWithoutModifiers(); + testVarPos(); + testVariableInIfThen3(); + testMissingExponent(); + testTryResourcePos(); + testOperatorMissingError(); + testMissingParenthesisError(); + testMissingClassError(); + testSwitchError(); + testMethodError(); + testErrorRecoveryForEnhancedForLoop142381(); + } + + public static void main(String... args) throws IOException { + JavacParserTest jpt = new JavacParserTest("JavacParserTest"); + jpt.testPositions(); + System.out.println("PASS"); + } +} + +abstract class TestCase { + + void assertEquals(String message, int i, int pos) { + if (i != pos) { + fail(message); + } + } + + void assertFalse(String message, boolean bvalue) { + if (bvalue == true) { + fail(message); + } + } + + void assertEquals(String message, int i, long l) { + if (i != l) { + fail(message + ":" + i + ":" + l); + } + } + + void assertEquals(String message, Object o1, Object o2) { + System.out.println(o1); + System.out.println(o2); + if (o1 != null && o2 != null && !o1.equals(o2)) { + fail(message); + } + if (o1 == null && o2 != null) { + fail(message); + } + } + + void assertNotNull(Object o) { + if (o == null) { + fail(); + } + } + + void fail() { + fail("test failed"); + } + + void fail(String message) { + throw new RuntimeException(message); + } +}
--- a/test/tools/javac/parser/netbeans/JavacParserTest.java Wed Feb 05 07:28:48 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,716 +0,0 @@ -/* - * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * @test - * @bug 7073631 - * @summary tests error and diagnostics positions - * @author jan.lahoda@oracle.com - */ - -import com.sun.source.tree.BinaryTree; -import com.sun.source.tree.BlockTree; -import com.sun.source.tree.ClassTree; -import com.sun.source.tree.CompilationUnitTree; -import com.sun.source.tree.ExpressionStatementTree; -import com.sun.source.tree.ExpressionTree; -import com.sun.source.tree.MethodInvocationTree; -import com.sun.source.tree.MethodTree; -import com.sun.source.tree.ModifiersTree; -import com.sun.source.tree.StatementTree; -import com.sun.source.tree.Tree; -import com.sun.source.tree.Tree.Kind; -import com.sun.source.tree.VariableTree; -import com.sun.source.tree.WhileLoopTree; -import com.sun.source.util.SourcePositions; -import com.sun.source.util.TreeScanner; -import com.sun.source.util.Trees; -import com.sun.tools.javac.api.JavacTaskImpl; -import com.sun.tools.javac.tree.JCTree; -import java.io.IOException; -import java.net.URI; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; -import javax.tools.Diagnostic; -import javax.tools.DiagnosticCollector; -import javax.tools.DiagnosticListener; -import javax.tools.JavaCompiler; -import javax.tools.JavaFileObject; -import javax.tools.SimpleJavaFileObject; -import javax.tools.ToolProvider; - -public class JavacParserTest extends TestCase { - final JavaCompiler tool; - public JavacParserTest(String testName) { - tool = ToolProvider.getSystemJavaCompiler(); - System.out.println("java.home=" + System.getProperty("java.home")); - } - - static class MyFileObject extends SimpleJavaFileObject { - - private String text; - - public MyFileObject(String text) { - super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); - this.text = text; - } - - @Override - public CharSequence getCharContent(boolean ignoreEncodingErrors) { - return text; - } - } - - public void testPositionForSuperConstructorCalls() throws IOException { - assert tool != null; - - String code = "package test; public class Test {public Test() {super();}}"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - CompilationUnitTree cut = ct.parse().iterator().next(); - SourcePositions pos = Trees.instance(ct).getSourcePositions(); - - MethodTree method = - (MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0); - ExpressionStatementTree es = - (ExpressionStatementTree) method.getBody().getStatements().get(0); - - assertEquals("testPositionForSuperConstructorCalls", - 72 - 24, pos.getStartPosition(cut, es)); - assertEquals("testPositionForSuperConstructorCalls", - 80 - 24, pos.getEndPosition(cut, es)); - - MethodInvocationTree mit = (MethodInvocationTree) es.getExpression(); - - assertEquals("testPositionForSuperConstructorCalls", - 72 - 24, pos.getStartPosition(cut, mit)); - assertEquals("testPositionForSuperConstructorCalls", - 79 - 24, pos.getEndPosition(cut, mit)); - - assertEquals("testPositionForSuperConstructorCalls", - 72 - 24, pos.getStartPosition(cut, mit.getMethodSelect())); - assertEquals("testPositionForSuperConstructorCalls", - 77 - 24, pos.getEndPosition(cut, mit.getMethodSelect())); - - } - - public void testPositionForEnumModifiers() throws IOException { - - String code = "package test; public enum Test {A;}"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - CompilationUnitTree cut = ct.parse().iterator().next(); - SourcePositions pos = Trees.instance(ct).getSourcePositions(); - - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - ModifiersTree mt = clazz.getModifiers(); - - assertEquals("testPositionForEnumModifiers", - 38 - 24, pos.getStartPosition(cut, mt)); - assertEquals("testPositionForEnumModifiers", - 44 - 24, pos.getEndPosition(cut, mt)); - } - - public void testNewClassWithEnclosing() throws IOException { - - - String code = "package test; class Test { " + - "class d {} private void method() { " + - "Object o = Test.this.new d(); } }"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - CompilationUnitTree cut = ct.parse().iterator().next(); - SourcePositions pos = Trees.instance(ct).getSourcePositions(); - - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - ExpressionTree est = - ((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer(); - - assertEquals("testNewClassWithEnclosing", - 97 - 24, pos.getStartPosition(cut, est)); - assertEquals("testNewClassWithEnclosing", - 114 - 24, pos.getEndPosition(cut, est)); - } - - public void testPreferredPositionForBinaryOp() throws IOException { - - String code = "package test; public class Test {" + - "private void test() {" + - "Object o = null; boolean b = o != null && o instanceof String;" + - "} private Test() {}}"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - CompilationUnitTree cut = ct.parse().iterator().next(); - - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - MethodTree method = (MethodTree) clazz.getMembers().get(0); - VariableTree condSt = (VariableTree) method.getBody().getStatements().get(1); - BinaryTree cond = (BinaryTree) condSt.getInitializer(); - - JCTree condJC = (JCTree) cond; - - assertEquals("testNewClassWithEnclosing", - 117 - 24, condJC.pos); - } - - public void testPositionBrokenSource126732a() throws IOException { - String[] commands = new String[]{ - "return Runnable()", - "do { } while (true)", - "throw UnsupportedOperationException()", - "assert true", - "1 + 1",}; - - for (String command : commands) { - - String code = "package test;\n" - + "public class Test {\n" - + " public static void test() {\n" - + " " + command + " {\n" - + " new Runnable() {\n" - + " };\n" - + " }\n" - + "}"; - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, - null, null, Arrays.asList(new MyFileObject(code))); - CompilationUnitTree cut = ct.parse().iterator().next(); - - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - MethodTree method = (MethodTree) clazz.getMembers().get(0); - List<? extends StatementTree> statements = - method.getBody().getStatements(); - - StatementTree ret = statements.get(0); - StatementTree block = statements.get(1); - - Trees t = Trees.instance(ct); - int len = code.indexOf(command + " {") + (command + " ").length(); - assertEquals(command, len, - t.getSourcePositions().getEndPosition(cut, ret)); - assertEquals(command, len, - t.getSourcePositions().getStartPosition(cut, block)); - } - } - - public void testPositionBrokenSource126732b() throws IOException { - String[] commands = new String[]{ - "break", - "break A", - "continue ", - "continue A",}; - - for (String command : commands) { - - String code = "package test;\n" - + "public class Test {\n" - + " public static void test() {\n" - + " while (true) {\n" - + " " + command + " {\n" - + " new Runnable() {\n" - + " };\n" - + " }\n" - + " }\n" - + "}"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, - null, null, Arrays.asList(new MyFileObject(code))); - CompilationUnitTree cut = ct.parse().iterator().next(); - - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - MethodTree method = (MethodTree) clazz.getMembers().get(0); - List<? extends StatementTree> statements = - ((BlockTree) ((WhileLoopTree) method.getBody().getStatements().get(0)).getStatement()).getStatements(); - - StatementTree ret = statements.get(0); - StatementTree block = statements.get(1); - - Trees t = Trees.instance(ct); - int len = code.indexOf(command + " {") + (command + " ").length(); - assertEquals(command, len, - t.getSourcePositions().getEndPosition(cut, ret)); - assertEquals(command, len, - t.getSourcePositions().getStartPosition(cut, block)); - } - } - - public void testErrorRecoveryForEnhancedForLoop142381() throws IOException { - - String code = "package test; class Test { " + - "private void method() { " + - "java.util.Set<String> s = null; for (a : s) {} } }"; - - final List<Diagnostic<? extends JavaFileObject>> errors = - new LinkedList<Diagnostic<? extends JavaFileObject>>(); - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, - new DiagnosticListener<JavaFileObject>() { - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { - errors.add(diagnostic); - } - }, null, null, Arrays.asList(new MyFileObject(code))); - - CompilationUnitTree cut = ct.parse().iterator().next(); - - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - StatementTree forStatement = - ((MethodTree) clazz.getMembers().get(0)).getBody().getStatements().get(1); - - assertEquals("testErrorRecoveryForEnhancedForLoop142381", - Kind.ENHANCED_FOR_LOOP, forStatement.getKind()); - assertFalse("testErrorRecoveryForEnhancedForLoop142381", errors.isEmpty()); - } - - public void testPositionAnnotationNoPackage187551() throws IOException { - - String code = "\n@interface Test {}"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - - CompilationUnitTree cut = ct.parse().iterator().next(); - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - Trees t = Trees.instance(ct); - - assertEquals("testPositionAnnotationNoPackage187551", - 1, t.getSourcePositions().getStartPosition(cut, clazz)); - } - - public void testPositionsSane() throws IOException { - performPositionsSanityTest("package test; class Test { " + - "private void method() { " + - "java.util.List<? extends java.util.List<? extends String>> l; " + - "} }"); - performPositionsSanityTest("package test; class Test { " + - "private void method() { " + - "java.util.List<? super java.util.List<? super String>> l; " + - "} }"); - performPositionsSanityTest("package test; class Test { " + - "private void method() { " + - "java.util.List<? super java.util.List<?>> l; } }"); - } - - private void performPositionsSanityTest(String code) throws IOException { - - final List<Diagnostic<? extends JavaFileObject>> errors = - new LinkedList<Diagnostic<? extends JavaFileObject>>(); - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, - new DiagnosticListener<JavaFileObject>() { - - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { - errors.add(diagnostic); - } - }, null, null, Arrays.asList(new MyFileObject(code))); - - final CompilationUnitTree cut = ct.parse().iterator().next(); - final Trees trees = Trees.instance(ct); - - new TreeScanner<Void, Void>() { - - private long parentStart = 0; - private long parentEnd = Integer.MAX_VALUE; - - @Override - public Void scan(Tree node, Void p) { - if (node == null) { - return null; - } - - long start = trees.getSourcePositions().getStartPosition(cut, node); - - if (start == (-1)) { - return null; //synthetic tree - } - assertTrue(node.toString() + ":" + start + "/" + parentStart, - parentStart <= start); - - long prevParentStart = parentStart; - - parentStart = start; - - long end = trees.getSourcePositions().getEndPosition(cut, node); - - assertTrue(node.toString() + ":" + end + "/" + parentEnd, - end <= parentEnd); - - long prevParentEnd = parentEnd; - - parentEnd = end; - - super.scan(node, p); - - parentStart = prevParentStart; - parentEnd = prevParentEnd; - - return null; - } - - private void assertTrue(String message, boolean b) { - if (!b) fail(message); - } - }.scan(cut, null); - } - - public void testCorrectWilcardPositions() throws IOException { - performWildcardPositionsTest("package test; import java.util.List; " + - "class Test { private void method() { List<? extends List<? extends String>> l; } }", - - Arrays.asList("List<? extends List<? extends String>> l;", - "List<? extends List<? extends String>>", - "List", - "? extends List<? extends String>", - "List<? extends String>", - "List", - "? extends String", - "String")); - performWildcardPositionsTest("package test; import java.util.List; " + - "class Test { private void method() { List<? super List<? super String>> l; } }", - - Arrays.asList("List<? super List<? super String>> l;", - "List<? super List<? super String>>", - "List", - "? super List<? super String>", - "List<? super String>", - "List", - "? super String", - "String")); - performWildcardPositionsTest("package test; import java.util.List; " + - "class Test { private void method() { List<? super List<?>> l; } }", - - Arrays.asList("List<? super List<?>> l;", - "List<? super List<?>>", - "List", - "? super List<?>", - "List<?>", - "List", - "?")); - performWildcardPositionsTest("package test; import java.util.List; " + - "class Test { private void method() { " + - "List<? extends List<? extends List<? extends String>>> l; } }", - - Arrays.asList("List<? extends List<? extends List<? extends String>>> l;", - "List<? extends List<? extends List<? extends String>>>", - "List", - "? extends List<? extends List<? extends String>>", - "List<? extends List<? extends String>>", - "List", - "? extends List<? extends String>", - "List<? extends String>", - "List", - "? extends String", - "String")); - performWildcardPositionsTest("package test; import java.util.List; " + - "class Test { private void method() { " + - "List<? extends List<? extends List<? extends String >>> l; } }", - Arrays.asList("List<? extends List<? extends List<? extends String >>> l;", - "List<? extends List<? extends List<? extends String >>>", - "List", - "? extends List<? extends List<? extends String >>", - "List<? extends List<? extends String >>", - "List", - "? extends List<? extends String >", - "List<? extends String >", - "List", - "? extends String", - "String")); - } - - public void performWildcardPositionsTest(final String code, - List<String> golden) throws IOException { - - final List<Diagnostic<? extends JavaFileObject>> errors = - new LinkedList<Diagnostic<? extends JavaFileObject>>(); - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, - new DiagnosticListener<JavaFileObject>() { - public void report(Diagnostic<? extends JavaFileObject> diagnostic) { - errors.add(diagnostic); - } - }, null, null, Arrays.asList(new MyFileObject(code))); - - final CompilationUnitTree cut = ct.parse().iterator().next(); - final List<String> content = new LinkedList<String>(); - final Trees trees = Trees.instance(ct); - - new TreeScanner<Void, Void>() { - @Override - public Void scan(Tree node, Void p) { - if (node == null) { - return null; - } - long start = trees.getSourcePositions().getStartPosition(cut, node); - - if (start == (-1)) { - return null; //synthetic tree - } - long end = trees.getSourcePositions().getEndPosition(cut, node); - String s = code.substring((int) start, (int) end); - content.add(s); - - return super.scan(node, p); - } - }.scan(((MethodTree) ((ClassTree) cut.getTypeDecls().get(0)).getMembers().get(0)).getBody().getStatements().get(0), null); - - assertEquals("performWildcardPositionsTest",golden.toString(), - content.toString()); - } - - public void testStartPositionForMethodWithoutModifiers() throws IOException { - - String code = "package t; class Test { <T> void t() {} }"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - CompilationUnitTree cut = ct.parse().iterator().next(); - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - MethodTree mt = (MethodTree) clazz.getMembers().get(0); - Trees t = Trees.instance(ct); - int start = (int) t.getSourcePositions().getStartPosition(cut, mt); - int end = (int) t.getSourcePositions().getEndPosition(cut, mt); - - assertEquals("testStartPositionForMethodWithoutModifiers", - "<T> void t() {}", code.substring(start, end)); - } - - public void testStartPositionEnumConstantInit() throws IOException { - - String code = "package t; enum Test { AAA; }"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - CompilationUnitTree cut = ct.parse().iterator().next(); - ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); - VariableTree enumAAA = (VariableTree) clazz.getMembers().get(0); - Trees t = Trees.instance(ct); - int start = (int) t.getSourcePositions().getStartPosition(cut, - enumAAA.getInitializer()); - - assertEquals("testStartPositionEnumConstantInit", -1, start); - } - - public void testVariableInIfThen1() throws IOException { - - String code = "package t; class Test { " + - "private static void t(String name) { " + - "if (name != null) String nn = name.trim(); } }"; - - DiagnosticCollector<JavaFileObject> coll = - new DiagnosticCollector<JavaFileObject>(); - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, - null, Arrays.asList(new MyFileObject(code))); - - ct.parse(); - - List<String> codes = new LinkedList<String>(); - - for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { - codes.add(d.getCode()); - } - - assertEquals("testVariableInIfThen1", - Arrays.<String>asList("compiler.err.variable.not.allowed"), - codes); - } - - public void testVariableInIfThen2() throws IOException { - - String code = "package t; class Test { " + - "private static void t(String name) { " + - "if (name != null) class X {} } }"; - DiagnosticCollector<JavaFileObject> coll = - new DiagnosticCollector<JavaFileObject>(); - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, - null, Arrays.asList(new MyFileObject(code))); - - ct.parse(); - - List<String> codes = new LinkedList<String>(); - - for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { - codes.add(d.getCode()); - } - - assertEquals("testVariableInIfThen2", - Arrays.<String>asList("compiler.err.class.not.allowed"), codes); - } - - public void testVariableInIfThen3() throws IOException { - - String code = "package t; class Test { "+ - "private static void t(String name) { " + - "if (name != null) abstract } }"; - DiagnosticCollector<JavaFileObject> coll = - new DiagnosticCollector<JavaFileObject>(); - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, coll, null, - null, Arrays.asList(new MyFileObject(code))); - - ct.parse(); - - List<String> codes = new LinkedList<String>(); - - for (Diagnostic<? extends JavaFileObject> d : coll.getDiagnostics()) { - codes.add(d.getCode()); - } - - assertEquals("testVariableInIfThen3", - Arrays.<String>asList("compiler.err.illegal.start.of.expr"), - codes); - } - - //see javac bug #6882235, NB bug #98234: - public void testMissingExponent() throws IOException { - - String code = "\nclass Test { { System.err.println(0e); } }"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - - assertNotNull(ct.parse().iterator().next()); - } - - public void testTryResourcePos() throws IOException { - - final String code = "package t; class Test { " + - "{ try (java.io.InputStream in = null) { } } }"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - CompilationUnitTree cut = ct.parse().iterator().next(); - - new TreeScanner<Void, Void>() { - @Override - public Void visitVariable(VariableTree node, Void p) { - if ("in".contentEquals(node.getName())) { - JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; - System.out.println(node.getName() + "," + var.pos); - assertEquals("testTryResourcePos", "in = null) { } } }", - code.substring(var.pos)); - } - return super.visitVariable(node, p); - } - }.scan(cut, null); - } - - public void testVarPos() throws IOException { - - final String code = "package t; class Test { " + - "{ java.io.InputStream in = null; } }"; - - JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, null, null, null, - null, Arrays.asList(new MyFileObject(code))); - CompilationUnitTree cut = ct.parse().iterator().next(); - - new TreeScanner<Void, Void>() { - - @Override - public Void visitVariable(VariableTree node, Void p) { - if ("in".contentEquals(node.getName())) { - JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) node; - assertEquals("testVarPos","in = null; } }", - code.substring(var.pos)); - } - return super.visitVariable(node, p); - } - }.scan(cut, null); - } - - void testsNotWorking() throws IOException { - - // Fails with nb-javac, needs further investigation - testPositionBrokenSource126732a(); - testPositionBrokenSource126732b(); - - // Fails, these tests yet to be addressed - testVariableInIfThen1(); - testVariableInIfThen2(); - testPositionForEnumModifiers(); - testStartPositionEnumConstantInit(); - } - void testPositions() throws IOException { - testPositionsSane(); - testCorrectWilcardPositions(); - testPositionAnnotationNoPackage187551(); - testPositionForSuperConstructorCalls(); - testPreferredPositionForBinaryOp(); - testStartPositionForMethodWithoutModifiers(); - testVarPos(); - testVariableInIfThen3(); - testTryResourcePos(); - } - - public static void main(String... args) throws IOException { - JavacParserTest jpt = new JavacParserTest("JavacParserTest"); - jpt.testPositions(); - System.out.println("PASS"); - } -} - -abstract class TestCase { - - void assertEquals(String message, int i, int pos) { - if (i != pos) { - fail(message); - } - } - - void assertFalse(String message, boolean empty) { - throw new UnsupportedOperationException("Not yet implemented"); - } - - void assertEquals(String message, int i, long l) { - if (i != l) { - fail(message + ":" + i + ":" + l); - } - } - - void assertEquals(String message, Object o1, Object o2) { - System.out.println(o1); - System.out.println(o2); - if (o1 != null && o2 != null && !o1.equals(o2)) { - fail(message); - } - if (o1 == null && o2 != null) { - fail(message); - } - } - - void assertNotNull(Object o) { - if (o == null) { - fail(); - } - } - - void fail() { - fail("test failed"); - } - - void fail(String message) { - throw new RuntimeException(message); - } -}