< prev index next >

test/jdk/java/lang/Class/getSimpleName/GetSimpleNameTest.java

Print this page

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /* @test
 25  * @bug 8057919

 26  * @summary Class.getSimpleName() should work for non-JLS compliant class names
 27  */
 28 
 29 import java.lang.classfile.ClassBuilder;
 30 import java.lang.classfile.ClassFile;
 31 import java.lang.classfile.CodeBuilder;
 32 import java.lang.classfile.attribute.EnclosingMethodAttribute;
 33 import java.lang.classfile.attribute.InnerClassInfo;
 34 import java.lang.classfile.attribute.InnerClassesAttribute;
 35 import java.lang.constant.ClassDesc;
 36 import java.lang.reflect.AccessFlag;
 37 import java.util.Optional;
 38 
 39 import static java.lang.classfile.ClassFile.ACC_PUBLIC;
 40 import static java.lang.classfile.ClassFile.ACC_STATIC;
 41 import static java.lang.constant.ConstantDescs.CD_Object;
 42 import static java.lang.constant.ConstantDescs.INIT_NAME;
 43 import static java.lang.constant.ConstantDescs.MTD_void;
 44 
 45 public class GetSimpleNameTest {

149                 cb.aload(0);
150                 cb.invokespecial(CD_Object, INIT_NAME, MTD_void);
151                 cb.return_();
152             });
153         }
154 
155         void makeCtxk(ClassBuilder clb, boolean isInner) {
156             if (isInner) {
157                 clb.with(EnclosingMethodAttribute.of(outerName,
158                         Optional.of("f"), Optional.of(MTD_void)));
159             } else {
160                 clb.withMethodBody("f", MTD_void, ACC_PUBLIC | ACC_STATIC,
161                         CodeBuilder::return_);
162             }
163         }
164 
165         byte[] getNestedClasses(boolean isInner) {
166             var name = (isInner ? innerName : outerName);
167             return ClassFile.of().build(name, clb -> {
168                 clb.withSuperclass(CD_Object);
169                 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
170                 clb.with(InnerClassesAttribute.of(
171                         InnerClassInfo.of(innerName,
172                                 Optional.of(outerName),
173                                 Optional.of(simpleName))));
174                 makeDefaultCtor(clb);
175             });
176         }
177 
178         byte[] getInnerClasses(boolean isInner) {
179             var name = (isInner ? innerName : outerName);
180             return ClassFile.of().build(name, clb -> {
181                 clb.withSuperclass(CD_Object);
182                 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
183                 clb.with(InnerClassesAttribute.of(
184                         InnerClassInfo.of(innerName,
185                                 Optional.of(outerName),
186                                 Optional.of(simpleName),
187                                 AccessFlag.PUBLIC)));
188                 makeDefaultCtor(clb);
189             });
190         }
191 
192         byte[] getLocalClasses(boolean isInner) {
193             var name = (isInner ? innerName : outerName);
194             return ClassFile.of().build(name, clb -> {
195                 clb.withSuperclass(CD_Object);
196                 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
197                 clb.with(InnerClassesAttribute.of(
198                         InnerClassInfo.of(innerName,
199                                 Optional.empty(),
200                                 Optional.of(simpleName),
201                                 AccessFlag.PUBLIC, AccessFlag.STATIC)));
202                 makeDefaultCtor(clb);
203                 makeCtxk(clb, isInner);
204             });
205         }
206 
207         byte[] getAnonymousClasses(boolean isInner) {
208             var name = (isInner ? innerName : outerName);
209             return ClassFile.of().build(name, clb -> {
210                 clb.withSuperclass(CD_Object);
211                 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.SUPER);
212                 clb.with(InnerClassesAttribute.of(
213                         InnerClassInfo.of(innerName,
214                                 Optional.empty(),
215                                 Optional.empty(),
216                                 AccessFlag.PUBLIC, AccessFlag.STATIC)));
217                 makeDefaultCtor(clb);
218                 makeCtxk(clb, isInner);
219             });
220         }
221     }
222 }

  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /* @test
 25  * @bug 8057919
 26  * @enablePreview
 27  * @summary Class.getSimpleName() should work for non-JLS compliant class names
 28  */
 29 
 30 import java.lang.classfile.ClassBuilder;
 31 import java.lang.classfile.ClassFile;
 32 import java.lang.classfile.CodeBuilder;
 33 import java.lang.classfile.attribute.EnclosingMethodAttribute;
 34 import java.lang.classfile.attribute.InnerClassInfo;
 35 import java.lang.classfile.attribute.InnerClassesAttribute;
 36 import java.lang.constant.ClassDesc;
 37 import java.lang.reflect.AccessFlag;
 38 import java.util.Optional;
 39 
 40 import static java.lang.classfile.ClassFile.ACC_PUBLIC;
 41 import static java.lang.classfile.ClassFile.ACC_STATIC;
 42 import static java.lang.constant.ConstantDescs.CD_Object;
 43 import static java.lang.constant.ConstantDescs.INIT_NAME;
 44 import static java.lang.constant.ConstantDescs.MTD_void;
 45 
 46 public class GetSimpleNameTest {

150                 cb.aload(0);
151                 cb.invokespecial(CD_Object, INIT_NAME, MTD_void);
152                 cb.return_();
153             });
154         }
155 
156         void makeCtxk(ClassBuilder clb, boolean isInner) {
157             if (isInner) {
158                 clb.with(EnclosingMethodAttribute.of(outerName,
159                         Optional.of("f"), Optional.of(MTD_void)));
160             } else {
161                 clb.withMethodBody("f", MTD_void, ACC_PUBLIC | ACC_STATIC,
162                         CodeBuilder::return_);
163             }
164         }
165 
166         byte[] getNestedClasses(boolean isInner) {
167             var name = (isInner ? innerName : outerName);
168             return ClassFile.of().build(name, clb -> {
169                 clb.withSuperclass(CD_Object);
170                 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
171                 clb.with(InnerClassesAttribute.of(
172                         InnerClassInfo.of(innerName,
173                                 Optional.of(outerName),
174                                 Optional.of(simpleName))));
175                 makeDefaultCtor(clb);
176             });
177         }
178 
179         byte[] getInnerClasses(boolean isInner) {
180             var name = (isInner ? innerName : outerName);
181             return ClassFile.of().build(name, clb -> {
182                 clb.withSuperclass(CD_Object);
183                 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
184                 clb.with(InnerClassesAttribute.of(
185                         InnerClassInfo.of(innerName,
186                                 Optional.of(outerName),
187                                 Optional.of(simpleName),
188                                 AccessFlag.PUBLIC)));
189                 makeDefaultCtor(clb);
190             });
191         }
192 
193         byte[] getLocalClasses(boolean isInner) {
194             var name = (isInner ? innerName : outerName);
195             return ClassFile.of().build(name, clb -> {
196                 clb.withSuperclass(CD_Object);
197                 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
198                 clb.with(InnerClassesAttribute.of(
199                         InnerClassInfo.of(innerName,
200                                 Optional.empty(),
201                                 Optional.of(simpleName),
202                                 AccessFlag.PUBLIC, AccessFlag.STATIC)));
203                 makeDefaultCtor(clb);
204                 makeCtxk(clb, isInner);
205             });
206         }
207 
208         byte[] getAnonymousClasses(boolean isInner) {
209             var name = (isInner ? innerName : outerName);
210             return ClassFile.of().build(name, clb -> {
211                 clb.withSuperclass(CD_Object);
212                 clb.withFlags(AccessFlag.PUBLIC, AccessFlag.IDENTITY);
213                 clb.with(InnerClassesAttribute.of(
214                         InnerClassInfo.of(innerName,
215                                 Optional.empty(),
216                                 Optional.empty(),
217                                 AccessFlag.PUBLIC, AccessFlag.STATIC)));
218                 makeDefaultCtor(clb);
219                 makeCtxk(clb, isInner);
220             });
221         }
222     }
223 }
< prev index next >