test/tools/javac/lambdaShapes/org/openjdk/tests/separate/ClassToInterfaceConverter.java

Thu, 24 Oct 2013 16:52:27 -0700

author
rfield
date
Thu, 24 Oct 2013 16:52:27 -0700
changeset 2170
860f1d21763a
parent 1448
7d34e91f66bb
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8027220: DefaultMethodsTest: Change test to match spec
Reviewed-by: ksrini

rfield@1422 1 /*
katleman@1448 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
rfield@1422 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
rfield@1422 4 *
rfield@1422 5 * This code is free software; you can redistribute it and/or modify it
rfield@1422 6 * under the terms of the GNU General Public License version 2 only, as
rfield@1422 7 * published by the Free Software Foundation. Oracle designates this
rfield@1422 8 * particular file as subject to the "Classpath" exception as provided
rfield@1422 9 * by Oracle in the LICENSE file that accompanied this code.
rfield@1422 10 *
rfield@1422 11 * This code is distributed in the hope that it will be useful, but WITHOUT
rfield@1422 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
rfield@1422 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
rfield@1422 14 * version 2 for more details (a copy is included in the LICENSE file that
rfield@1422 15 * accompanied this code).
rfield@1422 16 *
rfield@1422 17 * You should have received a copy of the GNU General Public License version
rfield@1422 18 * 2 along with this work; if not, write to the Free Software Foundation,
rfield@1422 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
rfield@1422 20 *
rfield@1422 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
rfield@1422 22 * or visit www.oracle.com if you need additional information or have any
rfield@1422 23 * questions.
rfield@1422 24 */
rfield@1422 25
rfield@1422 26 package org.openjdk.tests.separate;
rfield@1422 27
rfield@1422 28 import java.io.*;
rfield@1422 29 import java.util.*;
rfield@1422 30
rfield@1422 31 public class ClassToInterfaceConverter implements ClassFilePreprocessor {
rfield@1422 32
rfield@1422 33 private String whichClass;
rfield@1422 34
rfield@1422 35 public ClassToInterfaceConverter(String className) {
rfield@1422 36 this.whichClass = className;
rfield@1422 37 }
rfield@1422 38
rfield@1422 39 private boolean utf8Matches(ClassFile.CpEntry entry, String v) {
rfield@1422 40 if (!(entry instanceof ClassFile.CpUtf8)) {
rfield@1422 41 return false;
rfield@1422 42 }
rfield@1422 43 ClassFile.CpUtf8 utf8 = (ClassFile.CpUtf8)entry;
rfield@1422 44 if (v.length() != utf8.bytes.length) {
rfield@1422 45 return false;
rfield@1422 46 }
rfield@1422 47 for (int i = 0; i < v.length(); ++i) {
rfield@1422 48 if (v.charAt(i) != utf8.bytes[i]) {
rfield@1422 49 return false;
rfield@1422 50 }
rfield@1422 51 }
rfield@1422 52 return true;
rfield@1422 53 }
rfield@1422 54
rfield@1422 55 private void convertToInterface(ClassFile cf) {
rfield@1422 56 cf.access_flags = 0x0601; // ACC_INTERFACE | ACC_ABSTRACT | ACC_PUBLIC
rfield@1422 57 ArrayList<ClassFile.Method> new_methods = new ArrayList<>();
rfield@1422 58 // Find <init> method and delete it
rfield@1422 59 for (int i = 0; i < cf.methods.size(); ++i) {
rfield@1422 60 ClassFile.Method method = cf.methods.get(i);
rfield@1422 61 ClassFile.CpEntry name = cf.constant_pool.get(method.name_index);
rfield@1422 62 if (!utf8Matches(name, "<init>")) {
rfield@1422 63 new_methods.add(method);
rfield@1422 64 }
rfield@1422 65 }
rfield@1422 66 cf.methods = new_methods;
rfield@1422 67 }
rfield@1422 68
rfield@1422 69 public byte[] preprocess(String classname, byte[] bytes) {
rfield@1422 70 ClassFile cf = new ClassFile(bytes);
rfield@1422 71
rfield@1422 72 ClassFile.CpEntry entry = cf.constant_pool.get(cf.this_class);
rfield@1422 73 ClassFile.CpEntry name = cf.constant_pool.get(
rfield@1422 74 ((ClassFile.CpClass)entry).name_index);
rfield@1422 75 if (utf8Matches(name, whichClass)) {
rfield@1422 76 convertToInterface(cf);
rfield@1422 77 return cf.toByteArray();
rfield@1422 78 } else {
rfield@1422 79 return bytes; // unmodified
rfield@1422 80 }
rfield@1422 81 }
rfield@1422 82
rfield@1422 83 /*
rfield@1422 84 public static void main(String argv[]) throws Exception {
rfield@1422 85 File input = new File(argv[0]);
rfield@1422 86 byte[] buffer = new byte[(int)input.length()];
rfield@1422 87 new FileInputStream(input).read(buffer);
rfield@1422 88
rfield@1422 89 ClassFilePreprocessor cfp = new ClassToInterfaceConverter("Hello");
rfield@1422 90 byte[] cf = cfp.preprocess(argv[0], buffer);
rfield@1422 91 new FileOutputStream(argv[0] + ".mod").write(cf);
rfield@1422 92 }
rfield@1422 93 */
rfield@1422 94 }

mercurial