home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (HandleMetaClass.java)

This example Groovy source code file (HandleMetaClass.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

delegatingmetaclass, expandometaclass, expandometaclass, groovyobject, handlemetaclass, handlemetaclass, metaclass, metamethod, none, none, object, object, reflection, unsupportedoperationexception

The Groovy HandleMetaClass.java source code

 * Copyright 2003-2009 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,
 * See the License for the specific language governing permissions and
 * limitations under the License.
package org.codehaus.groovy.runtime;

import groovy.lang.*;

import java.lang.reflect.Method;

public class HandleMetaClass extends DelegatingMetaClass {
    private Object object;
    private static MetaClass myMetaClass;
    private static final Object NONE = new Object();

    public HandleMetaClass(MetaClass mc) {
        this(mc, null);

    public HandleMetaClass(MetaClass mc, Object obj) {
        if (obj != null) {
            if (InvokerHelper.getMetaClass(obj.getClass()) == mc || !(mc instanceof ExpandoMetaClass))
              object = obj; // object has default meta class, so we need to replace it on demand
              object = NONE; // object already has per instance meta class

        if (myMetaClass == null)
          myMetaClass = InvokerHelper.getMetaClass(getClass());

    public void initialize() {

    public GroovyObject replaceDelegate() {
        if (object == null) {
            if (!(delegate instanceof ExpandoMetaClass)) {
              delegate = new ExpandoMetaClass(delegate.getTheClass(), true, true);
            DefaultGroovyMethods.setMetaClass(delegate.getTheClass(), delegate);
        else {
          if (object != NONE) {
              final MetaClass metaClass = delegate;
              delegate = new ExpandoMetaClass(delegate.getTheClass(), false, true);
              if (metaClass instanceof ExpandoMetaClass) {
                  ExpandoMetaClass emc = (ExpandoMetaClass) metaClass;
                  for (MetaMethod method : emc.getExpandoMethods())
              DefaultGroovyMethods.setMetaClass(object, delegate);
              object = NONE;
        return (GroovyObject)delegate;

    public Object invokeMethod(String name, Object args) {
        return replaceDelegate().invokeMethod(name, args);

    // this method mimics EMC behavior
    public Object getProperty(String property) {
        if(ExpandoMetaClass.isValidExpandoProperty(property)) {
            if(property.equals(ExpandoMetaClass.STATIC_QUALIFIER) ||
               property.equals(ExpandoMetaClass.CONSTRUCTOR) ||
               myMetaClass.hasProperty(this, property) == null) {
                  return replaceDelegate().getProperty(property);
        return myMetaClass.getProperty(this, property);

    public void setProperty(String property, Object newValue) {
        replaceDelegate().setProperty(property, newValue);

    public void addNewInstanceMethod(Method method) {
        throw new UnsupportedOperationException();

    public void addNewStaticMethod(Method method) {
        throw new UnsupportedOperationException();

    public void addMetaMethod(MetaMethod metaMethod) {
        throw new UnsupportedOperationException();

    public void addMetaBeanProperty(MetaBeanProperty metaBeanProperty) {
        throw new UnsupportedOperationException();

    public boolean equals(Object obj) {
        return super.equals(obj) || getAdaptee().equals(obj) || (obj instanceof HandleMetaClass && equals(((HandleMetaClass)obj).getAdaptee()));

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy HandleMetaClass.java source code file:

new blog posts


Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.