1 /* 2 * Copyright (C) 2009 The Android Open Source Project 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 */ 16 17 package dex.reader.util; 18 19 import java.io.ByteArrayInputStream; 20 import java.io.InputStream; 21 import java.io.OutputStream; 22 import java.net.URI; 23 24 import javax.tools.SimpleJavaFileObject; 25 26 /** 27 * {@code JavaSource} represents an in-memory java source. 28 */ 29 public class JavaSource extends SimpleJavaFileObject { 30 private String src; 31 private final String sourceName; 32 JavaSource(String sourceName, String sourceCode)33 public JavaSource(String sourceName, String sourceCode) { 34 super(URI.create("string:///" + sourceName.replace(".", "/") + ".java"), 35 Kind.SOURCE); 36 this.sourceName = sourceName; 37 this.src = sourceCode; 38 } 39 40 @Override getName()41 public String getName() { 42 return sourceName; 43 } 44 getCharContent(boolean ignoreEncodingErrors)45 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 46 return src; 47 } 48 openOutputStream()49 public OutputStream openOutputStream() { 50 throw new IllegalStateException(); 51 } 52 openInputStream()53 public InputStream openInputStream() { 54 return new ByteArrayInputStream(src.getBytes()); 55 } 56 }