001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package org.terracotta.dso;
005:
006: import org.eclipse.core.runtime.IPath;
007: import org.eclipse.jdt.core.IClassFile;
008: import org.eclipse.jdt.core.ICompilationUnit;
009: import org.eclipse.jdt.core.IType;
010:
011: import com.tc.object.LiteralValues;
012: import com.tc.object.tools.BootJar;
013: import com.tc.object.tools.BootJarSignature;
014:
015: import java.io.File;
016: import java.util.HashSet;
017: import java.util.Iterator;
018: import java.util.Set;
019:
020: /**
021: * Utility for determining if a type is a pre-instrumented class in the bootjar.
022: */
023:
024: public class BootClassHelper {
025: private static BootClassHelper m_helper;
026: private Set<String> m_bootClasses;
027: private static LiteralValues m_literals = new LiteralValues();
028:
029: public static BootClassHelper getHelper() {
030: return m_helper;
031: }
032:
033: static BootClassHelper initialize() throws Exception {
034: return m_helper = new BootClassHelper();
035: }
036:
037: /*
038: * Initialize from the boot-jar that would be used by the VM
039: * Eclipse is running in. Eclipse can be running in one VM version
040: * while the user can specify that internally it use another
041: * version. If that is the case, BootJar.getBootJarForReading will
042: * fail complaining that we're trying to read an incompatible
043: * version.
044: */
045: public BootClassHelper() throws Exception {
046: TcPlugin plugin = TcPlugin.getDefault();
047: IPath libDirPath = plugin.getLibDirPath();
048:
049: if (libDirPath.append("tc.jar").toFile().exists()) {
050: String bootJarName = BootJarSignature
051: .getBootJarNameForThisVM();
052: IPath bootJarPath = libDirPath.append("dso-boot").append(
053: bootJarName);
054: File bootJarFile = bootJarPath.toFile();
055: BootJar bootJar = BootJar.getBootJarForReading(bootJarFile);
056:
057: m_bootClasses = new HashSet<String>();
058: Set classes = bootJar.getAllPreInstrumentedClasses();
059: Iterator iter = classes.iterator();
060: while (iter.hasNext()) {
061: m_bootClasses.add(iter.next().toString());
062: }
063: }
064:
065: if (m_bootClasses == null) {
066: m_bootClasses = new HashSet<String>();
067: }
068:
069: m_bootClasses.add("java.lang.Integer");
070: m_bootClasses.add("java.lang.String");
071: m_bootClasses.add("java.lang.Double");
072: m_bootClasses.add("java.lang.Boolean");
073: m_bootClasses.add("java.lang.Character");
074: m_bootClasses.add("java.lang.Float");
075: m_bootClasses.add("java.util.HashMap");
076: m_bootClasses.add("java.util.ArrayList");
077: }
078:
079: public boolean isAdaptable(final ICompilationUnit module) {
080: return module != null ? isAdaptable(module.findPrimaryType())
081: : false;
082: }
083:
084: public boolean isAdaptable(final IClassFile classFile) {
085: try {
086: return classFile != null ? isAdaptable(classFile.getType())
087: : false;
088: } catch (Exception e) {
089: return false;
090: }
091: }
092:
093: public boolean isAdaptable(final IType type) {
094: if (type != null) {
095: return isAdaptable(PatternHelper
096: .getFullyQualifiedName(type));
097: }
098: return false;
099: }
100:
101: public boolean isAdaptable(String fullName) {
102: return m_literals.isLiteral(fullName)
103: || (m_bootClasses != null && m_bootClasses
104: .contains(fullName));
105: }
106: }
|