FileUtils.java revision 40bb73886c61d4b2e96bab98b50c1cbfcb54d9a3
1/* 2 * Copyright 2010 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.google.android.testing.mocking; 17 18import java.io.File; 19import java.io.FileNotFoundException; 20import java.io.FileOutputStream; 21import java.io.IOException; 22 23/** 24 * @author swoodward@google.com (Stephen Woodward) 25 */ 26public class FileUtils { 27 28 /** 29 * @param clazz 30 * @param sdkVersion 31 * @return the appropriate interface name for the interface mock support file. 32 */ 33 static String getInterfaceNameFor(Class<?> clazz, SdkVersion sdkVersion) { 34 return sdkVersion.getPackagePrefix() + "genmocks." + clazz.getName() + "DelegateInterface"; 35 } 36 /** 37 * @param clazz 38 * @param sdkVersion 39 * @return the appropriate subclass name for the subclass mock support file. 40 */ 41 static String getSubclassNameFor(Class<?> clazz, SdkVersion sdkVersion) { 42 return sdkVersion.getPackagePrefix() + "genmocks." + clazz.getName() + "DelegateSubclass"; 43 } 44 45 /** 46 * Converts a class name into the a .class filename. 47 * 48 * @param className 49 * @return the file name for the specified class name. 50 */ 51 static String getFilenameFor(String className) { 52 return className.replace('.', File.separatorChar) + ".class"; 53 } 54 55 /** 56 * Converts a filename into a class name. 57 * 58 * @param filename 59 * @return the class name for the specified file name. 60 */ 61 static String getClassNameFor(String filename) { 62 if (!filename.endsWith(".class")) { 63 throw new IllegalArgumentException("Argument provided is not a class filename: " + filename); 64 } 65 // On non-Linux, files use the native separator, but jar entries use /... sigh 66 return filename.replace(File.separatorChar, '.').replace('/', '.') 67 .substring(0, filename.length() - 6); 68 } 69 70 static void saveClassToFolder(GeneratedClassFile clazz, String outputFolderName) 71 throws FileNotFoundException, IOException { 72 File classFolder = new File(outputFolderName); 73 File targetFile = new File(classFolder, getFilenameFor(clazz.getClassName())); 74 targetFile.getParentFile().mkdirs(); 75 FileOutputStream outputStream = new FileOutputStream(targetFile); 76 outputStream.write(clazz.getContents()); 77 outputStream.close(); 78 } 79} 80