|
Groovy example source code file (GeneratedMetaMethod.java)
The Groovy GeneratedMetaMethod.java source code
/*
* Copyright 2003-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.reflection;
import groovy.lang.GroovyRuntimeException;
import groovy.lang.MetaMethod;
import java.lang.reflect.Modifier;
import java.lang.reflect.Constructor;
import java.io.*;
import java.util.*;
public abstract class GeneratedMetaMethod extends MetaMethod {
private final String name;
private final CachedClass declaringClass;
private final Class returnType;
public GeneratedMetaMethod(String name, CachedClass declaringClass, Class returnType, Class [] parameters) {
this.name = name;
this.declaringClass = declaringClass;
this.returnType = returnType;
nativeParamTypes = parameters;
}
public int getModifiers() {
return Modifier.PUBLIC;
}
public String getName() {
return name;
}
public Class getReturnType() {
return returnType;
}
public CachedClass getDeclaringClass() {
return declaringClass;
}
public static class Proxy extends GeneratedMetaMethod {
private volatile MetaMethod proxy;
private final String className;
public Proxy(String className, String name, CachedClass declaringClass, Class returnType, Class[] parameters) {
super(name, declaringClass, returnType, parameters);
this.className = className;
}
@Override
public boolean isValidMethod(Class[] arguments) {
return proxy().isValidMethod(arguments);
}
@Override
public Object doMethodInvoke(Object object, Object[] argumentArray) {
return proxy().doMethodInvoke(object, argumentArray);
}
public Object invoke(Object object, Object[] arguments) {
return proxy().invoke(object, arguments);
}
public final synchronized MetaMethod proxy() {
if (proxy == null) {
createProxy();
}
return proxy;
}
private void createProxy() {
try {
Class<?> aClass = getClass().getClassLoader().loadClass(className.replace('/','.'));
Constructor<?> constructor = aClass.getConstructor(String.class, CachedClass.class, Class.class, Class[].class);
proxy = (MetaMethod) constructor.newInstance(getName(), getDeclaringClass(), getReturnType(), getNativeParameterTypes());
}
catch (Throwable t) {
t.printStackTrace();
throw new GroovyRuntimeException("Failed to create DGM method proxy : " + t, t);
}
}
}
public static class DgmMethodRecord implements Serializable {
public String className;
public String methodName;
public Class returnType;
public Class[] parameters;
private static final Class[] PRIMITIVE_CLASSES = {
Boolean.TYPE, Character.TYPE, Byte.TYPE, Short.TYPE,
Integer.TYPE, Long.TYPE, Double.TYPE, Float.TYPE, Void.TYPE,
boolean [].class, char [].class, byte [].class, short [].class,
int [].class, long [].class, double[].class, float [].class,
Object [].class, String [].class, Class [].class, Byte[].class
};
public static void saveDgmInfo(List<DgmMethodRecord> records, String file) throws IOException {
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
Map<String,Integer> classes = new LinkedHashMap
Other Groovy examples (source code examples)Here is a short list of links related to this Groovy GeneratedMetaMethod.java source code file: |
Other websites by Alvin Alexander:
Life/living in Alaska (OneMansAlaska.com)
How I Sold My Business (HowISoldMyBusiness.com)
Copyright 1998-2011 Alvin Alexander, devdaily.com
All Rights Reserved.