View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.bcel.verifier.tests;
18  
19  import java.io.File;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.net.URISyntaxException;
24  
25  import org.apache.bcel.classfile.JavaClass;
26  import org.apache.bcel.classfile.Utility;
27  
28  public abstract class TestCreator {
29  
30      // Common package base name for generated test classes
31      protected static final String TEST_PACKAGE = TestCreator.class.getPackage().getName();
32  
33      public void create() throws IOException {
34          final File classFile = new File(getPackageFolder(), getClassName());
35          try (FileOutputStream out = new FileOutputStream(classFile)) {
36              create(out);
37          }
38      }
39  
40      public abstract void create(OutputStream out) throws IOException;
41  
42      private File getClassesFolder() throws IOException {
43          try {
44              return new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
45          } catch (final URISyntaxException e) {
46              throw new IOException(e);
47          }
48      }
49  
50      private String getClassName() {
51          final String name = getClass().getName();
52          return name.substring(name.lastIndexOf('.') + 1).replace("Creator", JavaClass.EXTENSION);
53      }
54  
55      private File getPackageFolder() throws IOException {
56          return new File(getClassesFolder(), getPackageName());
57      }
58  
59      protected String getPackageName() {
60          return Utility.packageToPath(getClass().getPackage().getName());
61      }
62  }