| Method from org.codehaus.groovy.ast.ClassNode Detail: |
public void addConstructor(ConstructorNode node) {
node.setDeclaringClass(this);
redirect().getDeclaredConstructorsLazy().add(node);
}
|
public ConstructorNode addConstructor(int modifiers,
Parameter[] parameters,
ClassNode[] exceptions,
Statement code) {
ConstructorNode node = new ConstructorNode(modifiers, parameters, exceptions, code);
addConstructor(node);
return node;
}
|
public void addField(FieldNode node) {
node.setDeclaringClass(redirect());
node.setOwner(redirect());
redirect().getFieldsLazy().add(node);
redirect().getFieldIndexLazy().put(node.getName(), node);
}
|
public FieldNode addField(String name,
int modifiers,
ClassNode type,
Expression initialValue) {
FieldNode node = new FieldNode(name, modifiers, type, redirect(), initialValue);
addField(node);
return node;
}
|
public void addInterface(ClassNode type) {
// lets check if it already implements an interface
boolean skip = false;
ClassNode[] interfaces = redirect().interfaces;
for (int i = 0; i < interfaces.length; i++) {
if (type.equals(interfaces[i])) {
skip = true;
}
}
if (!skip) {
ClassNode[] newInterfaces = new ClassNode[interfaces.length + 1];
System.arraycopy(interfaces, 0, newInterfaces, 0, interfaces.length);
newInterfaces[interfaces.length] = type;
redirect().interfaces = newInterfaces;
}
}
|
public void addMethod(MethodNode node) {
node.setDeclaringClass(this);
redirect().getMethodsListLazy().add(node);
redirect().getMethodsLazy().put(node.getName(), node);
}
|
public MethodNode addMethod(String name,
int modifiers,
ClassNode returnType,
Parameter[] parameters,
ClassNode[] exceptions,
Statement code) {
MethodNode other = getDeclaredMethod(name, parameters);
// let's not add duplicate methods
if (other != null) {
return other;
}
MethodNode node = new MethodNode(name, modifiers, returnType, parameters, exceptions, code);
addMethod(node);
return node;
}
If a method with the given name and parameters is already defined then it is returned
otherwise the given method is added to this node. This method is useful for
default method adding like getProperty() or invokeMethod() where there may already
be a method defined in a class and so the default implementations should not be added
if already present. |
public void addMixin(MixinNode mixin) {
// lets check if it already uses a mixin
MixinNode[] mixins = redirect().mixins;
boolean skip = false;
for (int i = 0; i < mixins.length; i++) {
if (mixin.equals(mixins[i])) {
skip = true;
}
}
if (!skip) {
MixinNode[] newMixins = new MixinNode[mixins.length + 1];
System.arraycopy(mixins, 0, newMixins, 0, mixins.length);
newMixins[mixins.length] = mixin;
redirect().mixins = newMixins;
}
}
|
public void addObjectInitializerStatements(Statement statements) {
if (objectInitializers == null)
objectInitializers = new LinkedList();
objectInitializers.add(statements);
}
|
public void addProperty(PropertyNode node) {
node.setDeclaringClass(redirect());
FieldNode field = node.getField();
addField(field);
redirect().getPropertiesLazy().add(node);
}
|
public PropertyNode addProperty(String name,
int modifiers,
ClassNode type,
Expression initialValueExpression,
Statement getterBlock,
Statement setterBlock) {
for (Object o : getProperties()) {
PropertyNode pn = (PropertyNode) o;
if (pn.getName().equals(name)) {
if (pn.getInitialExpression() == null && initialValueExpression != null)
pn.getField().setInitialValueExpression(initialValueExpression);
if (pn.getGetterBlock() == null && getterBlock != null)
pn.setGetterBlock(getterBlock);
if (pn.getSetterBlock() == null && setterBlock != null)
pn.setSetterBlock(setterBlock);
return pn;
}
}
PropertyNode node =
new PropertyNode(name, modifiers, type, redirect(), initialValueExpression, getterBlock, setterBlock);
addProperty(node);
return node;
}
|
public void addStaticInitializerStatements(List staticStatements,
boolean fieldInit) {
MethodNode method = null;
List declaredMethods = getDeclaredMethods("< clinit >");
if (declaredMethods.isEmpty()) {
method =
addMethod("< clinit >", ACC_STATIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BlockStatement());
method.setSynthetic(true);
}
else {
method = (MethodNode) declaredMethods.get(0);
}
BlockStatement block = null;
Statement statement = method.getCode();
if (statement == null) {
block = new BlockStatement();
}
else if (statement instanceof BlockStatement) {
block = (BlockStatement) statement;
}
else {
block = new BlockStatement();
block.addStatement(statement);
}
// while anything inside a static initializer block is appended
// we don't want to append in the case we have a initialization
// expression of a static field. In that case we want to add
// before the other statements
if (!fieldInit) {
block.addStatements(staticStatements);
} else {
List blockStatements = block.getStatements();
staticStatements.addAll(blockStatements);
blockStatements.clear();
blockStatements.addAll(staticStatements);
}
}
|
public MethodNode addSyntheticMethod(String name,
int modifiers,
ClassNode returnType,
Parameter[] parameters,
ClassNode[] exceptions,
Statement code) {
MethodNode answer = addMethod(name, modifiers|ACC_SYNTHETIC, returnType, parameters, exceptions, code);
answer.setSynthetic(true);
return answer;
}
Adds a synthetic method as part of the compilation process |
public void addTransform(Class transform,
ASTNode node) {
if (transformInstances == null)
getTransformInstancesLazy();
GroovyASTTransformation annotation = transform.getAnnotation(GroovyASTTransformation.class);
Set< ASTNode > nodes = transformInstances.get(annotation.phase()).get(transform);
if (nodes == null) {
nodes = new LinkedHashSet();
transformInstances.get(annotation.phase()).put(transform, nodes);
}
nodes.add(node);
}
|
public boolean declaresInterface(ClassNode classNode) {
ClassNode[] interfaces = redirect().getInterfaces();
if (declaresInterfaceDirect(interfaces, classNode)) return true;
List superInterfaces = Arrays.asList(interfaces);
while (superInterfaces.size() > 0) {
List keep = new ArrayList();
for (int i = 0; i < superInterfaces.size(); i++) {
ClassNode cn = (ClassNode) superInterfaces.get(i);
if (cn.declaresInterface(classNode)) return true;
keep.addAll(Arrays.asList(cn.getInterfaces()));
}
superInterfaces = keep;
}
return false;
}
|
public boolean equals(Object o) {
if (redirect!=null) return redirect().equals(o);
ClassNode cn = (ClassNode) o;
return (cn.getName().equals(getName()));
}
|
public List getAbstractMethods() {
List result = new ArrayList(3);
Map declaredMethods = getDeclaredMethodsMap();
for (Iterator it = declaredMethods.values().iterator(); it.hasNext();) {
MethodNode method = (MethodNode) it.next();
if (method.isAbstract()) {
result.add(method);
}
}
if (result.isEmpty()) {
return null;
} else {
return result;
}
}
Returns a list containing MethodNode objects for
each abstract method in the class represented by
this ClassNode |
public List getAllDeclaredMethods() {
return new ArrayList(getDeclaredMethodsMap().values());
}
|
public Set getAllInterfaces() {
Set res = new HashSet ();
getAllInterfaces(res);
return res;
}
|
public List getAnnotations() {
if (redirect!=null) return redirect.getAnnotations();
lazyClassInit();
return super.getAnnotations();
}
|
public List getAnnotations(ClassNode type) {
if (redirect!=null) return redirect.getAnnotations(type);
lazyClassInit();
return super.getAnnotations(type);
}
|
public CompileUnit getCompileUnit() {
if (redirect!=null) return redirect().getCompileUnit();
if (compileUnit == null && module != null) {
compileUnit = module.getUnit();
}
return compileUnit;
}
|
public ClassNode getComponentType() {
return componentType;
}
|
public List getDeclaredConstructors() {
if (!redirect().lazyInitDone) redirect().lazyClassInit();
return redirect().getDeclaredConstructorsLazy();
}
|
public FieldNode getDeclaredField(String name) {
return (FieldNode) redirect().getFieldIndexLazy().get(name);
}
Finds a field matching the given name in this class. |
public MethodNode getDeclaredMethod(String name,
Parameter[] parameters) {
for (Object o : getDeclaredMethods(name)) {
MethodNode method = (MethodNode) o;
if (parametersEqual(method.getParameters(), parameters)) {
return method;
}
}
return null;
}
Finds a method matching the given name and parameters in this class. |
public List getDeclaredMethods(String name) {
if (!redirect().lazyInitDone) redirect().lazyClassInit();
if (redirect!=null) return redirect().getDeclaredMethods(name);
return getMethodsLazy().getNotNull(name);
}
This methods returns a list of all methods of the given name
defined in the current class |
public Map getDeclaredMethodsMap() {
// Start off with the methods from the superclass.
ClassNode parent = getSuperClass();
Map result = null;
if (parent != null) {
result = parent.getDeclaredMethodsMap();
} else {
result = new HashMap();
}
// add in unimplemented abstract methods from the interfaces
ClassNode[] interfaces = getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
ClassNode iface = interfaces[i];
Map ifaceMethodsMap = iface.getDeclaredMethodsMap();
for (Object o : ifaceMethodsMap.keySet()) {
String methSig = (String) o;
if (!result.containsKey(methSig)) {
MethodNode methNode = (MethodNode) ifaceMethodsMap.get(methSig);
result.put(methSig, methNode);
}
}
}
// And add in the methods implemented in this class.
for (Object o : getMethods()) {
MethodNode method = (MethodNode) o;
String sig = method.getTypeDescriptor();
result.put(sig, method);
}
return result;
}
|
public MethodNode getEnclosingMethod() {
return redirect().enclosingMethod;
}
|
public FieldNode getField(String name) {
ClassNode node = this;
while (node != null) {
FieldNode fn = node.getDeclaredField(name);
if (fn != null) return fn;
node = node.getSuperClass();
}
return null;
}
Finds a field matching the given name in this class or a parent class. |
public List getFields() {
if (!redirect().lazyInitDone) redirect().lazyClassInit();
if (redirect!=null) return redirect().getFields();
return getFieldsLazy();
}
Returns a list containing FieldNode objects for
each field in the class represented by this ClassNode |
public GenericsType[] getGenericsTypes() {
return genericsTypes;
}
|
public MethodNode getGetterMethod(String getterName) {
for (Object o : getDeclaredMethods(getterName)) {
MethodNode method = (MethodNode) o;
if (getterName.equals(method.getName())
&& ClassHelper.VOID_TYPE!=method.getReturnType()
&& method.getParameters().length == 0) {
return method;
}
}
ClassNode parent = getSuperClass();
if (parent!=null) return parent.getGetterMethod(getterName);
return null;
}
|
public ClassNode[] getInterfaces() {
if (!redirect().lazyInitDone) redirect().lazyClassInit();
if (redirect!=null) return redirect().getInterfaces();
return interfaces;
}
Returns an array of ClassNodes representing the
interfaces the class implements |
public MethodNode getMethod(String name,
Parameter[] parameters) {
for (Object o : getMethods(name)) {
MethodNode method = (MethodNode) o;
if (parametersEqual(method.getParameters(), parameters)) {
return method;
}
}
return null;
}
Finds a method matching the given name and parameters in this class
or any parent class. |
public List getMethods() {
if (!redirect().lazyInitDone) redirect().lazyClassInit();
if (redirect!=null) return redirect().getMethods();
return getMethodsListLazy();
}
Returns a list containing MethodNode objects for
each method in the class represented by this ClassNode |
public List getMethods(String name) {
List answer = new ArrayList();
ClassNode node = this;
while (node != null) {
answer.addAll(node.getDeclaredMethods(name));
node = node.getSuperClass();
}
return answer;
}
This methods creates a list of all methods with this name of the
current class and of all super classes |
public MixinNode[] getMixins() {
return redirect().mixins;
}
|
public int getModifiers() {
return redirect().modifiers;
}
|
public ModuleNode getModule() {
return redirect().module;
}
|
public String getName() {
return redirect().name;
}
|
public String getNameWithoutPackage() {
int idx = getName().lastIndexOf('.");
if (idx > 0) {
return getName().substring(idx + 1);
}
return getName();
}
|
public List getObjectInitializerStatements() {
if (objectInitializers == null)
objectInitializers = new LinkedList();
return objectInitializers;
}
|
public ClassNode getOuterClass() {
return null;
}
Helper method to avoid casting to inner class |
public FieldNode getOuterField(String name) {
return null;
}
|
public String getPackageName() {
int idx = getName().lastIndexOf('.");
if (idx > 0) {
return getName().substring(0, idx);
}
return null;
}
|
public ClassNode getPlainNodeReference() {
if (ClassHelper.isPrimitiveType(this)) return this;
ClassNode n = new ClassNode(getName(),getModifiers(),getSuperClass(),null,null);
n.isPrimaryNode = false;
n.setRedirect(this.redirect);
return n;
}
|
public List getProperties() {
return redirect().getPropertiesLazy();
}
|
public PropertyNode getProperty(String name) {
for (Object o : getProperties()) {
PropertyNode pn = (PropertyNode) o;
if (pn.getName().equals(name)) return pn;
}
return null;
}
|
public MethodNode getSetterMethod(String setterName) {
for (Object o : getDeclaredMethods(setterName)) {
MethodNode method = (MethodNode) o;
if (setterName.equals(method.getName())
&& ClassHelper.VOID_TYPE==method.getReturnType()
&& method.getParameters().length == 1) {
return method;
}
}
ClassNode parent = getSuperClass();
if (parent!=null) return parent.getSetterMethod(setterName);
return null;
}
|
public ClassNode getSuperClass() {
if (!lazyInitDone && !isResolved()) {
throw new GroovyBugError("ClassNode#getSuperClass for "+getName()+" called before class resolving");
}
ClassNode sn = redirect().getUnresolvedSuperClass();
if (sn!=null) sn=sn.redirect();
return sn;
}
|
public Map getTransforms(CompilePhase phase) {
if (transformInstances == null)
return Collections.EMPTY_MAP;
return transformInstances.get(phase);
}
|
public Class getTypeClass() {
Class c = redirect().clazz;
if (c!=null) return c;
ClassNode component = redirect().componentType;
if (component!=null && component.isResolved()){
ClassNode cn = component.makeArray();
setRedirect(cn);
return redirect().clazz;
}
throw new GroovyBugError("ClassNode#getTypeClass for "+getName()+" is called before the type class is set ");
}
|
public ClassNode getUnresolvedSuperClass() {
return getUnresolvedSuperClass(true);
}
|
public ClassNode getUnresolvedSuperClass(boolean useRedirect) {
if (!useRedirect) return superClass;
if (!redirect().lazyInitDone) redirect().lazyClassInit();
return redirect().superClass;
}
|
public boolean hasDeclaredMethod(String name,
Parameter[] parameters) {
MethodNode other = getDeclaredMethod(name, parameters);
return other != null;
}
|
public boolean hasMethod(String name,
Parameter[] parameters) {
MethodNode other = getMethod(name, parameters);
return other != null;
}
|
public boolean hasPackageName() {
return redirect().name.indexOf('.") >0;
}
|
public boolean hasPossibleMethod(String name,
Expression arguments) {
int count = 0;
if (arguments instanceof TupleExpression) {
TupleExpression tuple = (TupleExpression) arguments;
// TODO this won't strictly be true when using list expansion in argument calls
count = tuple.getExpressions().size();
}
ClassNode node = this;
do {
for (Object o : getMethods(name)) {
MethodNode method = (MethodNode) o;
if (method.getParameters().length == count) {
return true;
}
}
node = node.getSuperClass();
}
while (node != null);
return false;
}
Returns true if the given method has a possibly matching instance method with the given name and arguments. |
public boolean hasPossibleStaticMethod(String name,
Expression arguments) {
int count = 0;
if (arguments instanceof TupleExpression) {
TupleExpression tuple = (TupleExpression) arguments;
// TODO this won't strictly be true when using list expansion in argument calls
count = tuple.getExpressions().size();
} else if (arguments instanceof MapExpression) {
count = 1;
}
for (Object o : getMethods(name)) {
MethodNode method = (MethodNode) o;
if(method.isStatic()) {
Parameter[] parameters = method.getParameters();
if (parameters.length == count) return true;
// handle varargs case
if (parameters.length > 0 && parameters[parameters.length - 1].getType().isArray()) {
if (count >= parameters.length - 1) return true;
}
// handle parameters with default values
int nonDefaultParameters = 0;
for(int i = 0; i < parameters.length; i++) {
if(parameters[i].hasInitialExpression() == false) {
nonDefaultParameters++;
}
}
if(count < parameters.length && nonDefaultParameters < = count) {
return true;
}
}
}
return false;
}
Returns true if the given method has a possibly matching static method with the given name and arguments. |
public boolean hasProperty(String name) {
return getProperty(name)!=null;
}
|
public int hashCode() {
if (redirect!=null) return redirect().hashCode();
return getName().hashCode();
}
|
public boolean implementsInterface(ClassNode classNode) {
ClassNode node = redirect();
do {
if (node.declaresInterface(classNode)) {
return true;
}
node = node.getSuperClass();
}
while (node != null);
return false;
}
|
public boolean isAnnotated() {
return this.annotated;
}
|
public boolean isAnnotationDefinition() {
return redirect().isPrimaryNode &&
isInterface() &&
(getModifiers() & Opcodes.ACC_ANNOTATION)!=0;
}
|
public boolean isArray() {
return componentType!=null;
}
|
public boolean isDerivedFrom(ClassNode type) {
if (type.equals(ClassHelper.OBJECT_TYPE)) return true;
ClassNode node = this;
while (node != null) {
if (type.equals(node)) {
return true;
}
node = node.getSuperClass();
}
return false;
}
|
public boolean isDerivedFromGroovyObject() {
return implementsInterface(ClassHelper.make(GroovyObject.class));
}
|
public boolean isGenericsPlaceHolder() {
return placeholder;
}
|
public boolean isInterface() {
return (getModifiers() & Opcodes.ACC_INTERFACE) > 0;
}
|
public boolean isPrimaryClassNode() {
return redirect().isPrimaryNode || (componentType!= null && componentType.isPrimaryClassNode());
}
Returns if this instance is a primary ClassNode |
public boolean isResolved() {
return redirect().clazz!=null || (componentType != null && componentType.isResolved());
}
|
public boolean isScript() {
return redirect().script || isDerivedFrom(ClassHelper.SCRIPT_TYPE);
}
|
public boolean isScriptBody() {
return redirect().scriptBody;
}
|
public boolean isStaticClass() {
return redirect().staticClass;
}
Is this class delcared in a static method (such as a closure / inner class declared in a static method) |
public boolean isUsingGenerics() {
return usesGenerics;
}
|
public ClassNode makeArray() {
if (redirect!=null) return redirect().makeArray();
ClassNode cn;
if (clazz!=null) {
Class ret = Array.newInstance(clazz,0).getClass();
// don't use the ClassHelper here!
cn = new ClassNode(ret,this);
} else {
cn = new ClassNode(this);
}
return cn;
}
Returns a ClassNode representing an array of the class
represented by this ClassNode |
protected boolean parametersEqual(Parameter[] a,
Parameter[] b) {
if (a.length == b.length) {
boolean answer = true;
for (int i = 0; i < a.length; i++) {
if (!a[i].getType().equals(b[i].getType())) {
answer = false;
break;
}
}
return answer;
}
return false;
}
|
public ClassNode redirect() {
ClassNode res = this;
while (res.redirect != null) res = res.redirect;
return res;
}
Returns the ClassNode this ClassNode is redirecting to. |
public void renameField(String oldName,
String newName) {
final Map index = redirect().getFieldIndexLazy();
index.put(newName, index.remove(oldName));
}
|
public void setAnnotated(boolean flag) {
this.annotated = flag;
}
Marks if the current class uses annotations or not |
protected void setCompileUnit(CompileUnit cu) {
if (redirect!=null) redirect().setCompileUnit(cu);
if (compileUnit!= null) compileUnit = cu;
}
|
public void setEnclosingMethod(MethodNode enclosingMethod) {
redirect().enclosingMethod = enclosingMethod;
}
|
public void setGenericsPlaceHolder(boolean b) {
usesGenerics = usesGenerics || b;
placeholder = b;
}
|
public void setGenericsTypes(GenericsType[] genericsTypes) {
usesGenerics = usesGenerics || genericsTypes!=null;
this.genericsTypes = genericsTypes;
}
|
public void setInterfaces(ClassNode[] interfaces) {
if (redirect!=null) {
redirect().setInterfaces(interfaces);
} else {
this.interfaces = interfaces;
}
}
|
public void setModule(ModuleNode module) {
redirect().module = module;
if (module != null) {
redirect().compileUnit = module.getUnit();
}
}
|
public String setName(String name) {
return redirect().name=name;
}
|
public void setRedirect(ClassNode cn) {
if (isPrimaryNode) throw new GroovyBugError("tried to set a redirect for a primary ClassNode ("+getName()+"- >"+cn.getName()+").");
if (cn!=null) cn = cn.redirect();
if (cn==this) return;
redirect = cn;
}
Sets this instance as proxy for the given ClassNode. |
public void setScript(boolean script) {
redirect().script = script;
}
|
public void setScriptBody(boolean scriptBody) {
redirect().scriptBody = scriptBody;
}
|
public void setStaticClass(boolean staticClass) {
redirect().staticClass = staticClass;
}
|
public void setSuperClass(ClassNode superClass) {
redirect().superClass = superClass;
}
Sets the superclass of this ClassNode |
public void setUnresolvedSuperClass(ClassNode sn) {
superClass = sn;
}
|
public void setUsingGenerics(boolean b) {
usesGenerics = b;
}
|
public String toString() {
String ret = getName();
if (genericsTypes != null) {
ret += " < ";
for (int i = 0; i < genericsTypes.length; i++) {
if (i != 0) ret += ", ";
ret += genericsTypes[i];
}
ret += " >";
}
if (redirect != null) {
ret += " - > " + redirect().toString();
}
return ret;
}
|
public MethodNode tryFindPossibleMethod(String name,
Expression arguments) {
int count = 0;
if (arguments instanceof TupleExpression) {
TupleExpression tuple = (TupleExpression) arguments;
// TODO this won't strictly be true when using list expansion in argument calls
count = tuple.getExpressions().size();
} else
return null;
MethodNode res = null;
ClassNode node = this;
TupleExpression args = (TupleExpression) arguments;
do {
for (Object o : node.getMethods(name)) {
MethodNode method = (MethodNode) o;
if (method.getParameters().length == count) {
boolean match = true;
for (int i = 0; i != count; ++i)
if (!args.getType().isDerivedFrom(method.getParameters()[i].getType())) {
match = false;
break;
}
if (match) {
if (res == null)
res = method;
else {
if (res.getParameters().length != count)
return null;
if (node.equals(this))
return null;
match = true;
for (int i = 0; i != count; ++i)
if (!res.getParameters()[i].getType().equals(method.getParameters()[i].getType())) {
match = false;
break;
}
if (!match)
return null;
}
}
}
}
node = node.getSuperClass();
}
while (node != null);
return res;
}
|
public void visitContents(GroovyClassVisitor visitor) {
// now let's visit the contents of the class
for (Object o : getProperties()) {
PropertyNode pn = (PropertyNode) o;
visitor.visitProperty(pn);
}
for (Object o : getFields()) {
FieldNode fn = (FieldNode) o;
visitor.visitField(fn);
}
for (Object o : getDeclaredConstructors()) {
ConstructorNode cn = (ConstructorNode) o;
visitor.visitConstructor(cn);
}
for (Object o : getMethods()) {
MethodNode mn = (MethodNode) o;
visitor.visitMethod(mn);
}
}
|