1 16 package com.google.gwt.dev.jdt; 17 18 import com.google.gwt.core.ext.TreeLogger; 19 import com.google.gwt.core.ext.UnableToCompleteException; 20 21 import org.eclipse.jdt.core.compiler.CharOperation; 22 import org.eclipse.jdt.internal.compiler.ClassFile; 23 import org.eclipse.jdt.internal.compiler.CompilationResult; 24 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; 25 26 30 public class ByteCodeCompiler extends AbstractCompiler { 31 32 private final CacheManager cacheManager; 33 34 40 public ByteCodeCompiler(SourceOracle sourceOracle) { 41 super(sourceOracle, true); 42 this.cacheManager = new CacheManager(); 43 } 44 45 56 public ByteCodeCompiler(SourceOracle sourceOracle, CacheManager cacheManager) { 57 super(sourceOracle, true); 58 this.cacheManager = cacheManager; 59 } 60 61 66 public byte[] getClassBytes(TreeLogger logger, String binaryTypeName) 67 throws UnableToCompleteException { 68 69 String msg = "Getting bytecode for '" + binaryTypeName + "'"; 73 logger = logger.branch(TreeLogger.SPAM, msg, null); 74 setLogger(logger); 75 76 ByteCode byteCode = doGetByteCodeFromCache(logger, binaryTypeName); 79 if (byteCode != null) { 80 return byteCode.getBytes(); 83 } 84 85 ICompilationUnit start = getCompilationUnitForType(logger, binaryTypeName); 90 compile(logger, new ICompilationUnit[] {start}); 91 92 byteCode = doGetByteCodeFromCache(logger, binaryTypeName); 96 if (byteCode != null) { 97 return byteCode.getBytes(); 98 } else { 99 throw new UnableToCompleteException(); 100 } 101 } 102 103 110 public void putClassBytes(TreeLogger logger, String binaryTypeName, 111 byte[] bytes, String location) { 112 113 String pkgName = ""; 117 int lastDot = binaryTypeName.lastIndexOf('.'); 118 if (lastDot != -1) { 119 pkgName = binaryTypeName.substring(0, lastDot); 120 } 121 rememberPackage(pkgName); 122 123 ByteCode byteCode = new ByteCode(binaryTypeName, bytes, location, true); 126 cacheManager.acceptIntoCache(logger, binaryTypeName, byteCode); 127 } 128 129 135 public void removeStaleByteCode(TreeLogger logger) { 136 cacheManager.removeStaleByteCode(logger, this); 137 } 138 139 protected void doAcceptResult(CompilationResult result) { 140 TreeLogger logger = getLogger(); 143 ClassFile[] classFiles = result.getClassFiles(); 144 for (int i = 0; i < classFiles.length; i++) { 145 ClassFile classFile = classFiles[i]; 146 char[][] compoundName = classFile.getCompoundName(); 147 char[] classNameChars = CharOperation.concatWith(compoundName, '.'); 148 String className = String.valueOf(classNameChars); 149 byte bytes[] = classFile.getBytes(); 150 String loc = String.valueOf(result.compilationUnit.getFileName()); 151 boolean isTransient = true; 152 if (result.compilationUnit instanceof ICompilationUnitAdapter) { 153 ICompilationUnitAdapter unit = (ICompilationUnitAdapter) result.compilationUnit; 154 isTransient = unit.getCompilationUnitProvider().isTransient(); 155 } 156 ByteCode byteCode = new ByteCode(className, bytes, loc, isTransient); 157 if (cacheManager.acceptIntoCache(logger, className, byteCode)) { 158 logger.log(TreeLogger.SPAM, "Successfully compiled and cached '" 159 + className + "'", null); 160 } 161 } 162 } 163 164 169 protected ByteCode doGetByteCodeFromCache(TreeLogger logger, 170 String binaryTypeName) { 171 return cacheManager.getByteCode(logger, binaryTypeName); 172 } 173 } 174 | Popular Tags |