wildcatsの日記

赤羽在住でIT関係の会社の社長やってます。

とりあえず

これで要件は事足りたので良しとしてみる。
インタフェイスの継承とかされてreturnTypeが親インタフェイスで実装クラスにimplementsするインタフェイスが子インタフェイスとかだったらダメですけど。


diff -r -u D:\Tool\java\janino-2.5.5\src/org/codehaus/janino/ClassBodyEvaluator.java D:\Tool\java\janino-2.5.5-改造\src/org/codehaus/janino/ClassBodyEvaluator.java
--- D:\Tool\java\janino-2.5.5\src/org/codehaus/janino/ClassBodyEvaluator.java Wed Jan 17 00:17:06 2007
+++ D:\Tool\java\janino-2.5.5-改造\src/org/codehaus/janino/ClassBodyEvaluator.java Fri Feb 23 00:36:07 2007
@@ -309,7 +309,7 @@
this.result = this.compileToClass(
compilationUnit, // compilationUnit
DebuggingInformation.ALL, // debuggingInformation
- this.className
+ this.className,null
);
}

@@ -390,11 +390,12 @@
protected Class compileToClass(
Java.CompilationUnit compilationUnit,
EnumeratorSet debuggingInformation,
- String newClassName
+ String newClassName,
+ Class returnType
) throws CompileException {

// Compile and load the compilation unit.
- ClassLoader cl = this.compileToClassLoader(compilationUnit, debuggingInformation);
+ ClassLoader cl = this.compileToClassLoader(compilationUnit, debuggingInformation,returnType);

// Find the generated class by name.
try {
diff -r -u D:\Tool\java\janino-2.5.5\src/org/codehaus/janino/ScriptEvaluator.java D:\Tool\java\janino-2.5.5-改造\src/org/codehaus/janino/ScriptEvaluator.java
--- D:\Tool\java\janino-2.5.5\src/org/codehaus/janino/ScriptEvaluator.java Tue Sep 19 22:46:34 2006
+++ D:\Tool\java\janino-2.5.5-改造\src/org/codehaus/janino/ScriptEvaluator.java Fri Feb 23 00:35:44 2007
@@ -567,11 +567,12 @@
Class[][] parameterTypes = this.optionalParameterTypes == null ? new Class[count][0] : this.optionalParameterTypes;

// Create methods with one block each.
+ Class returnType = null;
for (int i = 0; i < count; ++i) {
Scanner s = scanners[i];

boolean staticMethod = this.optionalStaticMethod == null ? true : this.optionalStaticMethod[i];
- Class returnType = this.optionalReturnTypes == null ? this.getDefaultReturnType() : this.optionalReturnTypes[i];
+ returnType = this.optionalReturnTypes == null ? this.getDefaultReturnType() : this.optionalReturnTypes[i];
String[] parameterNames = this.optionalParameterNames == null ? new String[0] : this.optionalParameterNames[i];
Class[] thrownExceptions = this.optionalThrownExceptions == null ? new Class[0] : this.optionalThrownExceptions[i];

@@ -594,7 +595,7 @@
Class c = this.compileToClass(
compilationUnit, // compilationUnit
DebuggingInformation.DEFAULT_DEBUGGING_INFORMATION, // debuggingInformation
- this.className
+ this.className,returnType
);

// Find the script method by name.
@@ -704,7 +705,7 @@
Class c = this.compileToClass(
compilationUnit, // compilationUnit
DebuggingInformation.DEFAULT_DEBUGGING_INFORMATION, // debuggingInformation
- this.className
+ this.className,null
);

// Find the script method by name.
diff -r -u D:\Tool\java\janino-2.5.5\src/org/codehaus/janino/SimpleCompiler.java D:\Tool\java\janino-2.5.5-改造\src/org/codehaus/janino/SimpleCompiler.java
--- D:\Tool\java\janino-2.5.5\src/org/codehaus/janino/SimpleCompiler.java Sun Jan 14 17:15:58 2007
+++ D:\Tool\java\janino-2.5.5-改造\src/org/codehaus/janino/SimpleCompiler.java Fri Feb 23 00:41:17 2007
@@ -229,7 +229,7 @@
// Compile the classes and load them.
this.compileToClassLoader(
compilationUnit,
- DebuggingInformation.DEFAULT_DEBUGGING_INFORMATION
+ DebuggingInformation.DEFAULT_DEBUGGING_INFORMATION,null
);
}

@@ -389,7 +389,8 @@
*/
protected ClassLoader compileToClassLoader(
Java.CompilationUnit compilationUnit,
- EnumeratorSet debuggingInformation
+ EnumeratorSet debuggingInformation,
+ Class returnType
) throws CompileException {
if (SimpleCompiler.DEBUG) {
UnparseVisitor.unparse(compilationUnit, new OutputStreamWriter(System.out));
@@ -398,7 +399,8 @@
// Compile compilation unit to class files.
ClassFile[] classFiles = new UnitCompiler(
compilationUnit,
- this.iClassLoader
+ this.iClassLoader,
+ returnType
).compileUnit(debuggingInformation);

// Convert the class files to bytes and store them in a Map.
diff -r -u D:\Tool\java\janino-2.5.5\src/org/codehaus/janino/UnitCompiler.java D:\Tool\java\janino-2.5.5-改造\src/org/codehaus/janino/UnitCompiler.java
--- D:\Tool\java\janino-2.5.5\src/org/codehaus/janino/UnitCompiler.java Sat Feb 03 22:23:58 2007
+++ D:\Tool\java\janino-2.5.5-改造\src/org/codehaus/janino/UnitCompiler.java Fri Feb 23 00:55:39 2007
@@ -70,13 +70,25 @@
*/
private static final int STRING_CONCAT_LIMIT = 3;

+ public UnitCompiler(Java.CompilationUnit compilationUnit,
+ IClassLoader iClassLoader)
+ throws CompileException {
+ this(compilationUnit,iClassLoader,null);
+ }
+
public UnitCompiler(
Java.CompilationUnit compilationUnit,
- IClassLoader iClassLoader
+ IClassLoader iClassLoader,
+ Class returnType
) throws CompileException {
this.compilationUnit = compilationUnit;
this.iClassLoader = iClassLoader;
-
+ try {
+ this.returnType = iClassLoader.loadIClass(Descriptor.fromClassName(returnType.getName()));
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ }
+
try {
if (iClassLoader.loadIClass(Descriptor.STRING_BUILDER) != null) {
this.isStringBuilderAvailable = true;
@@ -2542,7 +2554,7 @@
rhsType = this.compileGetValue(ce.rhs);
}

- IClass expressionType;
+ IClass expressionType = null;
if (mhsType == rhsType) {

// JLS 15.25.1.1
@@ -2581,8 +2593,35 @@
if (rhsType.isAssignableFrom(mhsType)) {
expressionType = rhsType;
} else {
- this.compileError("Reference types \"" + mhsType + "\" and \"" + rhsType + "\" don't match", ce.getLocation());
- return this.iClassLoader.OBJECT;
+ List mhsInterfaces = new LinkedList();
+ mhsInterfaces.addAll(Arrays.asList(mhsType.getInterfaces()));
+ IClass mhsSuperType = mhsType;
+ do {
+ mhsInterfaces.addAll(Arrays.asList(mhsSuperType.getInterfaces()));
+ mhsSuperType = mhsSuperType.getSuperclass();
+ } while (mhsSuperType != null);
+
+ List rhsInterfaces = new LinkedList();
+ IClass rhsSuperType = rhsType;
+ do {
+ rhsInterfaces.addAll(Arrays.asList(rhsSuperType.getInterfaces()));
+ rhsSuperType = rhsSuperType.getSuperclass();
+ } while (rhsSuperType != null);
+
+ for (IClass mhsInterface : mhsInterfaces) {
+ for (IClass rhsInterface : rhsInterfaces) {
+ if (mhsInterface.equals(rhsInterface)) {
+ if (this.returnType.equals(mhsInterface)) {
+ expressionType = mhsInterface;
+ }
+ }
+ }
+ }
+
+ if (expressionType == null) {
+ this.compileError("Reference types \"" + mhsType + "\" and \"" + rhsType + "\" don't match", ce.getLocation());
+ return this.iClassLoader.OBJECT;
+ }
}
} else
{
@@ -2593,6 +2632,7 @@

return expressionType;
}
+
private IClass compileGet2(Java.Crement c) throws CompileException {

// Optimized crement of integer local variable.
@@ -7844,4 +7884,5 @@
private final Collection typeImportsOnDemand = new ArrayList(); // String[] package
private final Map singleStaticImports = new HashMap(); // String staticMemberName => IField, List of IMethod, or IClass
private final Collection staticImportsOnDemand = new ArrayList(); // IClass
+ private final IClass returnType;
}