Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2003-2007 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.ast;
 
 import groovy.lang.*;
 import  org.objectweb.asm.Opcodes;
 
 import java.util.List;
 import java.util.Map;
This class is a Helper for ClassNode and classes handling ClassNodes. It does contain a set of predefined ClassNodes for the most used types and some code for cached ClassNode creation and basic ClassNode handling

Author(s):
Jochen Theodorou
 
 public class ClassHelper {
 
     private static final Class[] classes = new Class[] {
         Object.class....,
         .....,
         Closure.classGString.classList.classMap.classRange.class,
         Pattern.classScript.classString.class,  Boolean.class
         Character.classByte.classShort.classInteger.classLong.class,
         Double.classFloat.classBigDecimal.classBigInteger.classVoid.class,
         Reference.classClass.classMetaClass.class,     
     };
 
     private static final String[] primitiveClassNames = new String[] {
         """boolean""char""byte""short"
         "int""long""double""float""void"
     };
     
     public static final ClassNode 
         DYNAMIC_TYPE = new ClassNode(Object.class),  OBJECT_TYPE = ,
         VOID_TYPE = new ClassNode(.),        CLOSURE_TYPE = new ClassNode(Closure.class),
         GSTRING_TYPE = new ClassNode(GString.class), LIST_TYPE = makeWithoutCaching(List.class),
         MAP_TYPE = new ClassNode(Map.class),         RANGE_TYPE = new ClassNode(Range.class),
         PATTERN_TYPE = new ClassNode(Pattern.class), STRING_TYPE = new ClassNode(String.class),
         SCRIPT_TYPE = new ClassNode(Script.class),   REFERENCE_TYPE = makeWithoutCaching(Reference.class),
         
         boolean_TYPE = new ClassNode(boolean.class),     char_TYPE = new ClassNode(char.class),
         byte_TYPE = new ClassNode(byte.class),           int_TYPE = new ClassNode(int.class),
         long_TYPE = new ClassNode(long.class),           short_TYPE = new ClassNode(short.class),
         double_TYPE = new ClassNode(double.class),       float_TYPE = new ClassNode(float.class),
         Byte_TYPE = new ClassNode(Byte.class),           Short_TYPE = new ClassNode(Short.class),
         Integer_TYPE = new ClassNode(Integer.class),     Long_TYPE = new ClassNode(Long.class),
         Character_TYPE = new ClassNode(Character.class), Float_TYPE = new ClassNode(Float.class),
         Double_TYPE = new ClassNode(Double.class),       Boolean_TYPE = new ClassNode(Boolean.class),
         BigInteger_TYPE =  new ClassNode(java.math.BigInteger.class),
         BigDecimal_TYPE = new ClassNode(java.math.BigDecimal.class),
         void_WRAPPER_TYPE = new ClassNode(Void.class),   
         
         CLASS_Type = new ClassNode(Class.class),        METACLASS_TYPE = new ClassNode(MetaClass.class),
         GENERATED_CLOSURE_Type = new ClassNode(GeneratedClosure.class),
         Enum_Type = new ClassNode("java.lang.Enum",0,),
         Annotation_TYPE = new ClassNode("java.lang.annotation.Annotation",0,);
         
     static {
         . = false;
         . = false;
     }
     
     private static ClassNode[] types = new ClassNode[] {
         ,
         ,
         ,
         ,
         ,
         ,
     };
 
     
     private static ClassNode[] numbers = new ClassNode[] {
        
    };
    protected static final ClassNode[] EMPTY_TYPE_ARRAY = {};
    
    public static final String OBJECT = "java.lang.Object";    
    
    
    
Creates an array of ClassNodes using an array of classes. For each of the given classes a new ClassNode will be created

Parameters:
classes an array of classes used to create the ClassNodes
Returns:
an array of ClassNodes
See also:
make(Class)
    public static ClassNode[] make(Class[] classes) {
    	ClassNode[] cns = new ClassNode[classes.length];
    	for (int i=0; i<cns.lengthi++) {
    		cns[i] = make(classes[i]);
    	}
    	
    	return cns;
    }
    
    
Creates a ClassNode using a given class. A new ClassNode object is only created if the class is not one of the predefined ones

Parameters:
c class used to created the ClassNode
Returns:
ClassNode instance created from the given class
    public static ClassNode make(Class c) {
        return make(c,true);
    }
    
    public static ClassNode make(Class cboolean includeGenerics) {
        for (int i=0; i<.i++) {
            if (c==[i]) return [i];
        }
        if (c.isArray()) {
            ClassNode cn = make(c.getComponentType(),includeGenerics);
            return cn.makeArray();
        }
        return makeWithoutCaching(c,includeGenerics);
    }
    
    public static ClassNode makeWithoutCaching(Class c){
        return makeWithoutCaching(c,true);
    }
    
    public static ClassNode makeWithoutCaching(Class cboolean includeGenerics){
        ClassNode t = new ClassNode(c);
        if (includeGenerics) VMPluginFactory.getPlugin().setAdditionalClassInformation(t);
        return t;
    }
    
    
    
Creates a ClassNode using a given class. Unlike make(String) this method will not use the cache to create the ClassNode. This means the ClassNode created from this method using the same name will have a different reference

Parameters:
name of the class the ClassNode is representing
See also:
make(String)
    public static ClassNode makeWithoutCaching(String name) { 
        ClassNode cn = new ClassNode(name,Opcodes.ACC_PUBLIC,);
        cn.isPrimaryNode = false;
        return cn;
    }
    
    
Creates a ClassNode using a given class. If the name is one of the predefined ClassNodes then the corresponding ClassNode instance will be returned. If the name is null or of length 0 the dynamic type is returned

Parameters:
name of the class the ClassNode is representing
    public static ClassNode make(String name) {
        if (name == null || name.length() == 0) return ;
        
        for (int i=0; i<.i++) {
            if ([i].equals(name)) return [i];
        }
        
        for (int i=0; i<.i++) {
            String cname = [i].getName();
            if (name.equals(cname)) return [i];
        }        
        return makeWithoutCaching(name);
    }
    
    
Creates a ClassNode containing the wrapper of a ClassNode of primitive type. Any ClassNode representing a primitive type should be created using the predefined types used in class. The method will check the parameter for known references of ClassNode representing a primitive type. If Reference is found, then a ClassNode will be contained that represents the wrapper class. For exmaple for boolean, the wrapper class is java.lang.Boolean. If the parameter is no primitve type, the redirected ClassNode will be returned

Parameters:
cn the ClassNode containing a possible primitive type
See also:
make(Class)
make(String)
    public static ClassNode getWrapper(ClassNode cn) {
        cn = cn.redirect();
        if (!isPrimitiveType(cn)) return cn;
        if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
        	return ;
        }
        else {
            return cn;
        }
    }
    public static ClassNode getUnwrapper(ClassNode cn) {
        cn = cn.redirect();
        if (isPrimitiveType(cn)) return cn;
        if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        } else if (cn==) {
            return ;
        }
        else {
            return cn;
        }
    }


    
Test to determine if a ClasNode is a primitve type. Note: this only works for ClassNodes created using a predefined ClassNode

Parameters:
cn the ClassNode containing a possible primitive type
Returns:
true if the ClassNode is a primitve type
See also:
make(Class)
make(String)
    public static boolean isPrimitiveType(ClassNode cn) {
        return  cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn == ;
    }
    public static boolean isNumberType(ClassNode cn) {
        return  cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn ==  ||
                cn == ;
    }
    public static ClassNode makeReference() {
        return make(Reference.class);
    }
    public static boolean isCachedType(ClassNode type) {
        for (int i=0;i<.i++) {
            if ([i] == typereturn true;
        }
        return false;
    }
New to GrepCode? Check out our FAQ X