зеркало из https://github.com/mozilla/pjs.git
Deprecating ClassOutput in favor of ClassRepository and Context methods to access ClassNameHelper functionality in favor of directly calling ClassNameHelper methods. For that I changed ClassNameHelper from interface to abstract class and added ClassNameHelper.get(Context cx) method to get name helper object that is used for the given Context object.
This commit is contained in:
Родитель
ea5ad64c74
Коммит
251c25a5c3
|
@ -38,23 +38,227 @@
|
||||||
|
|
||||||
package org.mozilla.javascript;
|
package org.mozilla.javascript;
|
||||||
|
|
||||||
public interface ClassNameHelper {
|
import java.io.*;
|
||||||
|
|
||||||
public String getTargetPackage();
|
public abstract class ClassNameHelper {
|
||||||
|
|
||||||
public void setTargetPackage(String targetPackage);
|
public static ClassNameHelper get(Context cx) {
|
||||||
|
ClassNameHelper helper = savedNameHelper;
|
||||||
|
if (helper == null && !helperNotAvailable) {
|
||||||
|
try {
|
||||||
|
Class nameHelperClass = Class.forName(
|
||||||
|
"org.mozilla.javascript.optimizer.OptClassNameHelper");
|
||||||
|
helper = (ClassNameHelper)nameHelperClass.newInstance();
|
||||||
|
} catch (ClassNotFoundException x) {
|
||||||
|
// ...must be running lite, that's ok
|
||||||
|
} catch (IllegalAccessException x) {
|
||||||
|
} catch (InstantiationException x) {
|
||||||
|
}
|
||||||
|
if (helper != null) {
|
||||||
|
savedNameHelper = helper;
|
||||||
|
} else {
|
||||||
|
helperNotAvailable = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return helper;
|
||||||
|
}
|
||||||
|
|
||||||
public void setTargetExtends(Class extendsClass);
|
static void clearCache() {
|
||||||
|
ClassNameHelper helper = savedNameHelper;
|
||||||
|
if (helper != null) {
|
||||||
|
helper.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setTargetImplements(Class[] implementsClasses);
|
/**
|
||||||
|
* Get the current target class file name.
|
||||||
|
* <p>
|
||||||
|
* If nonnull, requests to compile source will result in one or
|
||||||
|
* more class files being generated.
|
||||||
|
*
|
||||||
|
* @since 1.5 Release 4
|
||||||
|
*/
|
||||||
|
public String getTargetClassFileName() {
|
||||||
|
ClassRepository repository = getClassRepository();
|
||||||
|
if (repository instanceof FileClassRepository) {
|
||||||
|
return ((FileClassRepository)repository).
|
||||||
|
getTargetClassFileName(getClassName());
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public ClassRepository getClassRepository();
|
/**
|
||||||
|
* Set the current target class file name.
|
||||||
|
* <p>
|
||||||
|
* If nonnull, requests to compile source will result in one or
|
||||||
|
* more class files being generated. If null, classes will only
|
||||||
|
* be generated in memory.
|
||||||
|
*
|
||||||
|
* @since 1.5 Release 4
|
||||||
|
*/
|
||||||
|
public void setTargetClassFileName(String classFileName) {
|
||||||
|
if (classFileName != null) {
|
||||||
|
setClassRepository(new FileClassRepository(classFileName));
|
||||||
|
} else {
|
||||||
|
setClassName(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setClassRepository(ClassRepository repository);
|
/**
|
||||||
|
* @deprecated Application should use {@link ClassRepository} instead of
|
||||||
|
* {@link ClassOutput}.
|
||||||
|
*
|
||||||
|
* @see #getClassRepository
|
||||||
|
*/
|
||||||
|
public final ClassOutput getClassOutput() {
|
||||||
|
ClassRepository repository = getClassRepository();
|
||||||
|
if (repository instanceof ClassOutputWrapper) {
|
||||||
|
return ((ClassOutputWrapper)repository).classOutput;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public String getClassName();
|
/**
|
||||||
|
* @deprecated Application should use {@link ClassRepository} instead of
|
||||||
|
* {@link ClassOutput}.
|
||||||
|
*
|
||||||
|
* @see #setClassRepository
|
||||||
|
*/
|
||||||
|
public void setClassOutput(ClassOutput classOutput) {
|
||||||
|
if (classOutput != null) {
|
||||||
|
setClassRepository(new ClassOutputWrapper(classOutput));
|
||||||
|
} else {
|
||||||
|
setClassRepository(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setClassName(String initialName);
|
/**
|
||||||
|
* Get the current package to generate classes into.
|
||||||
|
*/
|
||||||
|
public abstract String getTargetPackage();
|
||||||
|
|
||||||
public void reset();
|
/**
|
||||||
|
* Set the package to generate classes into.
|
||||||
|
*/
|
||||||
|
public abstract void setTargetPackage(String targetPackage);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the class that the generated target will extend.
|
||||||
|
*
|
||||||
|
* @param extendsClass the class it extends
|
||||||
|
*/
|
||||||
|
public abstract void setTargetExtends(Class extendsClass);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the interfaces that the generated target will implement.
|
||||||
|
*
|
||||||
|
* @param implementsClasses an array of Class objects, one for each
|
||||||
|
* interface the target will extend
|
||||||
|
*/
|
||||||
|
public abstract void setTargetImplements(Class[] implementsClasses);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current class repository.
|
||||||
|
*
|
||||||
|
* @see ClassRepository
|
||||||
|
* @since 30/10/01 tip + patch (Kemal Bayram)
|
||||||
|
*/
|
||||||
|
public abstract ClassRepository getClassRepository();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the current class repository.
|
||||||
|
*
|
||||||
|
* @see ClassRepository
|
||||||
|
* @since 30/10/01 tip + patch (Kemal Bayram)
|
||||||
|
*/
|
||||||
|
public abstract void setClassRepository(ClassRepository repository);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current class name.
|
||||||
|
*
|
||||||
|
* @since 30/10/01 tip + patch (Kemal Bayram)
|
||||||
|
*/
|
||||||
|
public abstract String getClassName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the current class name.
|
||||||
|
*
|
||||||
|
* @since 30/10/01 tip + patch (Kemal Bayram)
|
||||||
|
*/
|
||||||
|
public abstract void setClassName(String initialName);
|
||||||
|
|
||||||
|
public abstract void reset();
|
||||||
|
|
||||||
|
// Implement class file saving here instead of inside codegen.
|
||||||
|
private class FileClassRepository implements ClassRepository {
|
||||||
|
|
||||||
|
FileClassRepository(String classFileName) {
|
||||||
|
int lastSeparator = classFileName.lastIndexOf(File.separatorChar);
|
||||||
|
String initialName;
|
||||||
|
if (lastSeparator == -1) {
|
||||||
|
generatingDirectory = null;
|
||||||
|
initialName = classFileName;
|
||||||
|
} else {
|
||||||
|
generatingDirectory = classFileName.substring(0, lastSeparator);
|
||||||
|
initialName = classFileName.substring(lastSeparator+1);
|
||||||
|
}
|
||||||
|
if (initialName.endsWith(".class"))
|
||||||
|
initialName = initialName.substring(0, initialName.length()-6);
|
||||||
|
setClassName(initialName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean storeClass(String className, byte[] bytes, boolean tl)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
// no "elegant" way of getting file name from fully
|
||||||
|
// qualified class name.
|
||||||
|
String targetPackage = getTargetPackage();
|
||||||
|
if ((targetPackage != null) && (targetPackage.length()>0) &&
|
||||||
|
className.startsWith(targetPackage+"."))
|
||||||
|
{
|
||||||
|
className = className.substring(targetPackage.length()+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
FileOutputStream out = new FileOutputStream(getTargetClassFileName(className));
|
||||||
|
out.write(bytes);
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
String getTargetClassFileName(String className) {
|
||||||
|
StringBuffer sb = new StringBuffer();
|
||||||
|
if (generatingDirectory != null) {
|
||||||
|
sb.append(generatingDirectory);
|
||||||
|
sb.append(File.separator);
|
||||||
|
}
|
||||||
|
sb.append(className);
|
||||||
|
sb.append(".class");
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
String generatingDirectory;
|
||||||
|
};
|
||||||
|
|
||||||
|
private static class ClassOutputWrapper implements ClassRepository {
|
||||||
|
|
||||||
|
ClassOutputWrapper(ClassOutput classOutput) {
|
||||||
|
this.classOutput = classOutput;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean storeClass(String name, byte[] bytes, boolean tl)
|
||||||
|
throws IOException
|
||||||
|
{
|
||||||
|
OutputStream out = classOutput.getOutputStream(name, tl);
|
||||||
|
out.write(bytes);
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ClassOutput classOutput;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ClassNameHelper savedNameHelper;
|
||||||
|
private static boolean helperNotAvailable;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,57 +0,0 @@
|
||||||
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
||||||
*
|
|
||||||
* The contents of this file are subject to the Netscape Public
|
|
||||||
* License Version 1.1 (the "License"); you may not use this file
|
|
||||||
* except in compliance with the License. You may obtain a copy of
|
|
||||||
* the License at http://www.mozilla.org/NPL/
|
|
||||||
*
|
|
||||||
* Software distributed under the License is distributed on an "AS
|
|
||||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
||||||
* implied. See the License for the specific language governing
|
|
||||||
* rights and limitations under the License.
|
|
||||||
*
|
|
||||||
* The Original Code is Rhino code, released
|
|
||||||
* May 6, 1999.
|
|
||||||
*
|
|
||||||
* The Initial Developer of the Original Code is Netscape
|
|
||||||
* Communications Corporation. Portions created by Netscape are
|
|
||||||
* Copyright (C) 1997-2000 Netscape Communications Corporation. All
|
|
||||||
* Rights Reserved.
|
|
||||||
*
|
|
||||||
* Contributor(s):
|
|
||||||
* Andi Vajda
|
|
||||||
*
|
|
||||||
* Alternatively, the contents of this file may be used under the
|
|
||||||
* terms of the GNU Public License (the "GPL"), in which case the
|
|
||||||
* provisions of the GPL are applicable instead of those above.
|
|
||||||
* If you wish to allow use of your version of this file only
|
|
||||||
* under the terms of the GPL and not to allow others to use your
|
|
||||||
* version of this file under the NPL, indicate your decision by
|
|
||||||
* deleting the provisions above and replace them with the notice
|
|
||||||
* and other provisions required by the GPL. If you do not delete
|
|
||||||
* the provisions above, a recipient may use your version of this
|
|
||||||
* file under either the NPL or the GPL.
|
|
||||||
*/
|
|
||||||
package org.mozilla.javascript;
|
|
||||||
|
|
||||||
// API class
|
|
||||||
|
|
||||||
import java.io.*;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This interface is implemented by classes interested in the bytecode
|
|
||||||
* generated by the rhino compiler for script objects.
|
|
||||||
*
|
|
||||||
* @see Context
|
|
||||||
* @author Andi Vajda
|
|
||||||
*/
|
|
||||||
public interface ClassOutput {
|
|
||||||
/**
|
|
||||||
* @param className the name of the class for which bytecode is ready.
|
|
||||||
* @param isTopLevel if true, represents the top-level script being compiled
|
|
||||||
* @return a stream into which to write bytecode.
|
|
||||||
* @since 1.5 Release 2
|
|
||||||
*/
|
|
||||||
public OutputStream getOutputStream(String className, boolean isTopLevel)
|
|
||||||
throws IOException;
|
|
||||||
}
|
|
|
@ -40,7 +40,7 @@ import java.io.*;
|
||||||
* This interface provides a means to store generated class and to
|
* This interface provides a means to store generated class and to
|
||||||
* allow selective class loading.
|
* allow selective class loading.
|
||||||
*
|
*
|
||||||
* @see Context
|
* @see ClassNameHelper
|
||||||
* @author Kemal Bayram
|
* @author Kemal Bayram
|
||||||
*/
|
*/
|
||||||
public interface ClassRepository {
|
public interface ClassRepository {
|
||||||
|
@ -51,5 +51,5 @@ public interface ClassRepository {
|
||||||
* @return true if the class should be loaded, false otherwise.
|
* @return true if the class should be loaded, false otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean storeClass(String className, byte[] classBytes,
|
public boolean storeClass(String className, byte[] classBytes,
|
||||||
boolean isTopLevel) throws IOException;
|
boolean isTopLevel) throws IOException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -169,7 +169,7 @@ public class Context {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (cx.enterCount != 0) {
|
if (cx.enterCount != 0) {
|
||||||
// The suplied context must be the context for
|
// The suplied context must be the context for
|
||||||
// the current thread if it is already entered
|
// the current thread if it is already entered
|
||||||
if (cx != old) {
|
if (cx != old) {
|
||||||
throw new RuntimeException
|
throw new RuntimeException
|
||||||
|
@ -1344,142 +1344,111 @@ public class Context {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current class name.
|
* @deprecated Use <tt>ClassNameHelper.get(cx).getClassName()</tt> instead.
|
||||||
*
|
* @see ClassNameHelper#getClassName
|
||||||
* @since 30/10/01 tip + patch (Kemal Bayram)
|
|
||||||
*/
|
*/
|
||||||
public String getClassName() {
|
public String getClassName() {
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
return nameHelper != null ? nameHelper.getClassName() : null;
|
return nameHelper != null ? nameHelper.getClassName() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the current class name.
|
* @deprecated Use <tt>ClassNameHelper.get(cx).setClassName(className)</tt> instead.
|
||||||
*
|
* @see ClassNameHelper#setClassName
|
||||||
* @since 30/10/01 tip + patch (Kemal Bayram)
|
|
||||||
*/
|
*/
|
||||||
public void setClassName(String className) {
|
public void setClassName(String className) {
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
if (nameHelper != null)
|
if (nameHelper != null)
|
||||||
nameHelper.setClassName(className);
|
nameHelper.setClassName(className);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the current target class file name.
|
* @deprecated Use
|
||||||
* <p>
|
* <tt>ClassNameHelper.get(cx).getTargetClassFileName()</tt> instead.
|
||||||
* If nonnull, requests to compile source will result in one or
|
* @see ClassNameHelper#getTargetClassFileName
|
||||||
* more class files being generated.
|
|
||||||
* @since 1.3
|
|
||||||
*/
|
*/
|
||||||
public String getTargetClassFileName() {
|
public String getTargetClassFileName() {
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
if (nameHelper != null) {
|
if (nameHelper != null) {
|
||||||
ClassRepository repository = nameHelper.getClassRepository();
|
return nameHelper.getTargetClassFileName();
|
||||||
if (repository instanceof FileClassRepository)
|
|
||||||
return ((FileClassRepository)repository).getTargetClassFileName(nameHelper.getClassName());
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the current target class file name.
|
|
||||||
* <p>
|
|
||||||
* If nonnull, requests to compile source will result in one or
|
|
||||||
* more class files being generated. If null, classes will only
|
|
||||||
* be generated in memory.
|
|
||||||
*
|
|
||||||
* @since 1.3
|
|
||||||
*/
|
|
||||||
public void setTargetClassFileName(String classFileName) {
|
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
|
||||||
if (nameHelper != null) {
|
|
||||||
if (classFileName != null)
|
|
||||||
nameHelper.setClassRepository(
|
|
||||||
new FileClassRepository(classFileName));
|
|
||||||
else
|
|
||||||
nameHelper.setClassName(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current package to generate classes into.
|
|
||||||
*
|
|
||||||
* @since 1.3
|
|
||||||
*/
|
|
||||||
public String getTargetPackage() {
|
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
|
||||||
return nameHelper != null ? nameHelper.getTargetPackage() : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the package to generate classes into.
|
|
||||||
*
|
|
||||||
* @since 1.3
|
|
||||||
*/
|
|
||||||
public void setTargetPackage(String targetPackage) {
|
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
|
||||||
if (nameHelper != null)
|
|
||||||
nameHelper.setTargetPackage(targetPackage);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current class repository.
|
|
||||||
*
|
|
||||||
* @see ClassRepository
|
|
||||||
* @since 30/10/01 tip + patch (Kemal Bayram)
|
|
||||||
*/
|
|
||||||
public ClassRepository getClassRepository() {
|
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
|
||||||
return nameHelper != null ? nameHelper.getClassRepository() : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set the current class repository.
|
|
||||||
*
|
|
||||||
* @see ClassRepository
|
|
||||||
* @since 30/10/01 tip + patch (Kemal Bayram)
|
|
||||||
*/
|
|
||||||
public void setClassRepository(ClassRepository classRepository) {
|
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
|
||||||
if (nameHelper != null)
|
|
||||||
nameHelper.setClassRepository(classRepository);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current interface to write class bytes into.
|
|
||||||
*
|
|
||||||
* @see ClassOutput
|
|
||||||
* @since 1.5 Release 2
|
|
||||||
*/
|
|
||||||
public ClassOutput getClassOutput() {
|
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
|
||||||
if (nameHelper != null) {
|
|
||||||
ClassRepository repository = nameHelper.getClassRepository();
|
|
||||||
if ((repository != null) &&
|
|
||||||
(repository instanceof ClassOutputWrapper))
|
|
||||||
{
|
|
||||||
return ((ClassOutputWrapper)repository).classOutput;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the interface to write class bytes into.
|
* @deprecated Use
|
||||||
* Unless setTargetClassFileName() has been called classOutput will be
|
* <tt>ClassNameHelper.get(cx).setTargetClassFileName(classFileName)</tt> instead.
|
||||||
* used each time the javascript compiler has generated the bytecode for a
|
* @see ClassNameHelper#setTargetClassFileName
|
||||||
* script class.
|
*/
|
||||||
*
|
public void setTargetClassFileName(String classFileName) {
|
||||||
* @see ClassOutput
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
* @since 1.5 Release 2
|
if (nameHelper != null) {
|
||||||
|
nameHelper.setTargetClassFileName(classFileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use <tt>ClassNameHelper.get(cx).getTargetPackage()</tt> instead.
|
||||||
|
* @see ClassNameHelper#getTargetPackage
|
||||||
|
*/
|
||||||
|
public String getTargetPackage() {
|
||||||
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
|
return nameHelper != null ? nameHelper.getTargetPackage() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use <tt>ClassNameHelper.get(cx).setTargetPackage(targetPackage)</tt>
|
||||||
|
* instead.
|
||||||
|
* @see ClassNameHelper#setTargetPackage
|
||||||
|
*/
|
||||||
|
public void setTargetPackage(String targetPackage) {
|
||||||
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
|
if (nameHelper != null)
|
||||||
|
nameHelper.setTargetPackage(targetPackage);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use <tt>ClassNameHelper.get(cx).getClassRepository()</tt> instead.
|
||||||
|
* @see ClassNameHelper#getClassRepository
|
||||||
|
*/
|
||||||
|
public ClassRepository getClassRepository() {
|
||||||
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
|
return nameHelper != null ? nameHelper.getClassRepository() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use
|
||||||
|
* <tt>ClassNameHelper.get(cx).setClassRepository(classRepository)</tt> instead.
|
||||||
|
* @see ClassNameHelper#setClassRepository
|
||||||
|
*/
|
||||||
|
public void setClassRepository(ClassRepository classRepository) {
|
||||||
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
|
if (nameHelper != null)
|
||||||
|
nameHelper.setClassRepository(classRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use
|
||||||
|
* <tt>ClassNameHelper.get(cx).getClassOutput()</tt> instead.
|
||||||
|
* @see ClassNameHelper#getClassOutput
|
||||||
|
*/
|
||||||
|
public ClassOutput getClassOutput() {
|
||||||
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
|
if (nameHelper != null) {
|
||||||
|
return nameHelper.getClassOutput();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use
|
||||||
|
* <tt>ClassNameHelper.get(cx).setClassOutput(classOutput)</tt> instead.
|
||||||
|
* @see ClassNameHelper#setClassOutput
|
||||||
*/
|
*/
|
||||||
public void setClassOutput(ClassOutput classOutput) {
|
public void setClassOutput(ClassOutput classOutput) {
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
if (nameHelper != null) {
|
if (nameHelper != null) {
|
||||||
if (classOutput != null)
|
nameHelper.setClassOutput(classOutput);
|
||||||
nameHelper.setClassRepository(new ClassOutputWrapper(classOutput));
|
|
||||||
else
|
|
||||||
nameHelper.setClassRepository(null);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1524,30 +1493,30 @@ public class Context {
|
||||||
classShutter = shutter;
|
classShutter = shutter;
|
||||||
}
|
}
|
||||||
|
|
||||||
ClassShutter getClassShutter() {
|
final ClassShutter getClassShutter() {
|
||||||
return classShutter;
|
return classShutter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the class that the generated target will extend.
|
* @deprecated Use
|
||||||
*
|
* <tt>ClassNameHelper.get(cx).setTargetExtends(extendsClass)</tt> instead.
|
||||||
* @param extendsClass the class it extends
|
* @see ClassNameHelper#setTargetExtends
|
||||||
*/
|
*/
|
||||||
public void setTargetExtends(Class extendsClass) {
|
public void setTargetExtends(Class extendsClass) {
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
if (nameHelper != null) {
|
if (nameHelper != null) {
|
||||||
nameHelper.setTargetExtends(extendsClass);
|
nameHelper.setTargetExtends(extendsClass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the interfaces that the generated target will implement.
|
* @deprecated Use
|
||||||
*
|
* <tt>ClassNameHelper.get(cx).setTargetImplements(implementsClasses)</tt>
|
||||||
* @param implementsClasses an array of Class objects, one for each
|
* instead.
|
||||||
* interface the target will extend
|
* @see ClassNameHelper#setTargetImplements
|
||||||
*/
|
*/
|
||||||
public void setTargetImplements(Class[] implementsClasses) {
|
public void setTargetImplements(Class[] implementsClasses) {
|
||||||
ClassNameHelper nameHelper = getNameHelper();
|
ClassNameHelper nameHelper = ClassNameHelper.get(this);
|
||||||
if (nameHelper != null) {
|
if (nameHelper != null) {
|
||||||
nameHelper.setTargetImplements(implementsClasses);
|
nameHelper.setTargetImplements(implementsClasses);
|
||||||
}
|
}
|
||||||
|
@ -1568,7 +1537,7 @@ public class Context {
|
||||||
* @param key the key used to lookup the value
|
* @param key the key used to lookup the value
|
||||||
* @return a value previously stored using putThreadLocal.
|
* @return a value previously stored using putThreadLocal.
|
||||||
*/
|
*/
|
||||||
public Object getThreadLocal(Object key) {
|
public final Object getThreadLocal(Object key) {
|
||||||
if (hashtable == null)
|
if (hashtable == null)
|
||||||
return null;
|
return null;
|
||||||
return hashtable.get(key);
|
return hashtable.get(key);
|
||||||
|
@ -1606,7 +1575,7 @@ public class Context {
|
||||||
* This is useful for sharing functions across multiple scopes.
|
* This is useful for sharing functions across multiple scopes.
|
||||||
* @since 1.5 Release 1
|
* @since 1.5 Release 1
|
||||||
*/
|
*/
|
||||||
public boolean hasCompileFunctionsWithDynamicScope() {
|
public final boolean hasCompileFunctionsWithDynamicScope() {
|
||||||
return compileFunctionsWithDynamicScopeFlag;
|
return compileFunctionsWithDynamicScopeFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1644,8 +1613,7 @@ public class Context {
|
||||||
if (isCachingEnabled && !cachingEnabled) {
|
if (isCachingEnabled && !cachingEnabled) {
|
||||||
// Caching is being turned off. Empty caches.
|
// Caching is being turned off. Empty caches.
|
||||||
JavaMembers.classTable = new Hashtable();
|
JavaMembers.classTable = new Hashtable();
|
||||||
if (savedNameHelper != null)
|
ClassNameHelper.clearCache();
|
||||||
savedNameHelper.reset();
|
|
||||||
}
|
}
|
||||||
isCachingEnabled = cachingEnabled;
|
isCachingEnabled = cachingEnabled;
|
||||||
FunctionObject.setCachingEnabled(cachingEnabled);
|
FunctionObject.setCachingEnabled(cachingEnabled);
|
||||||
|
@ -1727,7 +1695,7 @@ public class Context {
|
||||||
* @see org.mozilla.javascript.WrapHandler
|
* @see org.mozilla.javascript.WrapHandler
|
||||||
* @since 1.5 Release 4
|
* @since 1.5 Release 4
|
||||||
*/
|
*/
|
||||||
public WrapFactory getWrapFactory() {
|
public final WrapFactory getWrapFactory() {
|
||||||
if (wrapFactory == null) {
|
if (wrapFactory == null) {
|
||||||
wrapFactory = new WrapFactory();
|
wrapFactory = new WrapFactory();
|
||||||
}
|
}
|
||||||
|
@ -1738,7 +1706,7 @@ public class Context {
|
||||||
* Return the current debugger.
|
* Return the current debugger.
|
||||||
* @return the debugger, or null if none is attached.
|
* @return the debugger, or null if none is attached.
|
||||||
*/
|
*/
|
||||||
public Debugger getDebugger() {
|
public final Debugger getDebugger() {
|
||||||
return debugger;
|
return debugger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1746,7 +1714,7 @@ public class Context {
|
||||||
* Return the debugger context data associated with current context.
|
* Return the debugger context data associated with current context.
|
||||||
* @return the debugger data, or null if debugger is not attached
|
* @return the debugger data, or null if debugger is not attached
|
||||||
*/
|
*/
|
||||||
public Object getDebuggerContextData() {
|
public final Object getDebuggerContextData() {
|
||||||
return debuggerData;
|
return debuggerData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1981,7 +1949,6 @@ public class Context {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Class codegenClass;
|
private static Class codegenClass;
|
||||||
private static ClassNameHelper savedNameHelper;
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
codegenClass = Class.forName(
|
codegenClass = Class.forName(
|
||||||
|
@ -1992,26 +1959,6 @@ public class Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ClassNameHelper getNameHelper() {
|
|
||||||
if (savedNameHelper != null)
|
|
||||||
return savedNameHelper;
|
|
||||||
if (codegenClass == null)
|
|
||||||
return null;
|
|
||||||
try {
|
|
||||||
Class nameHelperClass = Class.forName(
|
|
||||||
"org.mozilla.javascript.optimizer.OptClassNameHelper");
|
|
||||||
savedNameHelper = (ClassNameHelper)nameHelperClass.newInstance();
|
|
||||||
return savedNameHelper;
|
|
||||||
} catch (ClassNotFoundException x) {
|
|
||||||
// ...must be running lite, that's ok
|
|
||||||
return null;
|
|
||||||
} catch (IllegalAccessException x) {
|
|
||||||
return null;
|
|
||||||
} catch (InstantiationException x) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Interpreter getCompiler() {
|
private Interpreter getCompiler() {
|
||||||
if (codegenClass != null) {
|
if (codegenClass != null) {
|
||||||
try {
|
try {
|
||||||
|
@ -2040,7 +1987,7 @@ public class Context {
|
||||||
: getCompiler();
|
: getCompiler();
|
||||||
ClassNameHelper nameHelper = optimizationLevel == -1
|
ClassNameHelper nameHelper = optimizationLevel == -1
|
||||||
? null
|
? null
|
||||||
: getNameHelper();
|
: ClassNameHelper.get(this);
|
||||||
|
|
||||||
errorCount = 0;
|
errorCount = 0;
|
||||||
IRFactory irf = compiler.createIRFactory(ts, nameHelper, scope);
|
IRFactory irf = compiler.createIRFactory(ts, nameHelper, scope);
|
||||||
|
@ -2270,74 +2217,4 @@ public class Context {
|
||||||
// For instruction counting (interpreter only)
|
// For instruction counting (interpreter only)
|
||||||
int instructionCount;
|
int instructionCount;
|
||||||
int instructionThreshold;
|
int instructionThreshold;
|
||||||
|
|
||||||
// Implement class file saving here instead of inside codegen.
|
|
||||||
private class FileClassRepository implements ClassRepository {
|
|
||||||
|
|
||||||
FileClassRepository(String classFileName) {
|
|
||||||
int lastSeparator = classFileName.lastIndexOf(File.separatorChar);
|
|
||||||
String initialName;
|
|
||||||
if (lastSeparator == -1) {
|
|
||||||
generatingDirectory = null;
|
|
||||||
initialName = classFileName;
|
|
||||||
} else {
|
|
||||||
generatingDirectory = classFileName.substring(0, lastSeparator);
|
|
||||||
initialName = classFileName.substring(lastSeparator+1);
|
|
||||||
}
|
|
||||||
if (initialName.endsWith(".class"))
|
|
||||||
initialName = initialName.substring(0, initialName.length()-6);
|
|
||||||
getNameHelper().setClassName(initialName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean storeClass(String className, byte[] bytes, boolean tl)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
// no "elegant" way of getting file name from fully
|
|
||||||
// qualified class name.
|
|
||||||
String targetPackage = getNameHelper().getTargetPackage();
|
|
||||||
if ((targetPackage != null) && (targetPackage.length()>0) &&
|
|
||||||
className.startsWith(targetPackage+"."))
|
|
||||||
{
|
|
||||||
className = className.substring(targetPackage.length()+1);
|
|
||||||
}
|
|
||||||
|
|
||||||
FileOutputStream out = new FileOutputStream(getTargetClassFileName(className));
|
|
||||||
out.write(bytes);
|
|
||||||
out.close();
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
String getTargetClassFileName(String className) {
|
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
if (generatingDirectory != null) {
|
|
||||||
sb.append(generatingDirectory);
|
|
||||||
sb.append(File.separator);
|
|
||||||
}
|
|
||||||
sb.append(className);
|
|
||||||
sb.append(".class");
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
String generatingDirectory;
|
|
||||||
};
|
|
||||||
|
|
||||||
private static class ClassOutputWrapper implements ClassRepository {
|
|
||||||
|
|
||||||
ClassOutputWrapper(ClassOutput classOutput) {
|
|
||||||
this.classOutput = classOutput;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean storeClass(String name, byte[] bytes, boolean tl)
|
|
||||||
throws IOException
|
|
||||||
{
|
|
||||||
OutputStream out = classOutput.getOutputStream(name, tl);
|
|
||||||
out.write(bytes);
|
|
||||||
out.close();
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
ClassOutput classOutput;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ import org.mozilla.javascript.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
|
|
||||||
public class OptClassNameHelper implements ClassNameHelper {
|
public class OptClassNameHelper extends ClassNameHelper {
|
||||||
|
|
||||||
public OptClassNameHelper() {
|
public OptClassNameHelper() {
|
||||||
setClassName(null);
|
setClassName(null);
|
||||||
|
|
|
@ -76,7 +76,8 @@ public class Main {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static String[] processOptions(Context cx, String args[]) {
|
public static String[] processOptions(Context cx, String args[]) {
|
||||||
cx.setTargetPackage(""); // default to no package
|
ClassNameHelper nameHelper = ClassNameHelper.get(cx);
|
||||||
|
nameHelper.setTargetPackage(""); // default to no package
|
||||||
cx.setGeneratingDebug(false); // default to no symbols
|
cx.setGeneratingDebug(false); // default to no symbols
|
||||||
for (int i=0; i < args.length; i++) {
|
for (int i=0; i < args.length; i++) {
|
||||||
String arg = args[i];
|
String arg = args[i];
|
||||||
|
@ -132,7 +133,7 @@ public class Main {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cx.setTargetClassFileName(outFile);
|
nameHelper.setTargetClassFileName(outFile);
|
||||||
hasOutOption = true;
|
hasOutOption = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -163,13 +164,13 @@ public class Main {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cx.setTargetPackage(targetPackage);
|
nameHelper.setTargetPackage(targetPackage);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (arg.equals("-extends") && ++i < args.length) {
|
if (arg.equals("-extends") && ++i < args.length) {
|
||||||
String targetExtends = args[i];
|
String targetExtends = args[i];
|
||||||
try {
|
try {
|
||||||
cx.setTargetExtends(Class.forName(targetExtends));
|
nameHelper.setTargetExtends(Class.forName(targetExtends));
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
throw new Error(e.toString()); // TODO: better error
|
throw new Error(e.toString()); // TODO: better error
|
||||||
}
|
}
|
||||||
|
@ -191,7 +192,7 @@ public class Main {
|
||||||
}
|
}
|
||||||
Class[] implementsClasses = new Class[v.size()];
|
Class[] implementsClasses = new Class[v.size()];
|
||||||
v.copyInto(implementsClasses);
|
v.copyInto(implementsClasses);
|
||||||
cx.setTargetImplements(implementsClasses);
|
nameHelper.setTargetImplements(implementsClasses);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
usage(arg);
|
usage(arg);
|
||||||
|
@ -214,10 +215,11 @@ public class Main {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public static void processSource(Context cx, String[] filenames) {
|
public static void processSource(Context cx, String[] filenames) {
|
||||||
|
ClassNameHelper nameHelper = ClassNameHelper.get(cx);
|
||||||
if (hasOutOption && filenames.length > 1) {
|
if (hasOutOption && filenames.length > 1) {
|
||||||
cx.reportError(ToolErrorReporter.getMessage(
|
cx.reportError(ToolErrorReporter.getMessage(
|
||||||
"msg.multiple.js.to.file",
|
"msg.multiple.js.to.file",
|
||||||
cx.getTargetClassFileName()));
|
nameHelper.getTargetClassFileName()));
|
||||||
}
|
}
|
||||||
for (int i=0; i < filenames.length; i++) {
|
for (int i=0; i < filenames.length; i++) {
|
||||||
String filename = filenames[i];
|
String filename = filenames[i];
|
||||||
|
@ -241,9 +243,9 @@ public class Main {
|
||||||
String className = getClassName(nojs) + ".class";
|
String className = getClassName(nojs) + ".class";
|
||||||
String out = f.getParent() == null ? className : f.getParent() +
|
String out = f.getParent() == null ? className : f.getParent() +
|
||||||
File.separator + className;
|
File.separator + className;
|
||||||
cx.setTargetClassFileName(out);
|
nameHelper.setTargetClassFileName(out);
|
||||||
}
|
}
|
||||||
if (cx.getTargetClassFileName() == null) {
|
if (nameHelper.getTargetClassFileName() == null) {
|
||||||
cx.reportError(ToolErrorReporter.getMessage("msg.no-opt"));
|
cx.reportError(ToolErrorReporter.getMessage("msg.no-opt"));
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
|
Загрузка…
Ссылка в новой задаче