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