All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.xmlbeans.impl.jam.internal.classrefs.UnqualifiedJClassRef Maven / Gradle / Ivy

/*   Copyright 2004 The Apache Software Foundation
 *
 *   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.apache.xmlbeans.impl.jam.internal.classrefs;

import org.apache.xmlbeans.impl.jam.JClass;

import java.io.StringWriter;

/**
 *
 * @author Patrick Calahan <email: pcal-at-bea-dot-com>
 */
public class UnqualifiedJClassRef implements JClassRef {

  // ========================================================================
  // Constants

  private static final boolean VERBOSE = false;
  private static final String PREFIX = "[UnqualifiedJClassRef]";

  // ========================================================================
  // Variables

  private String mUnqualifiedClassname;
  private String mQualifiedClassname = null;
  private JClassRefContext mContext;

  // ========================================================================
  // Factory

  /**
   * Creates a new JClassRef for a qualified class or type name.
   */
  public static JClassRef create(String qualifiedClassname,
                                 JClassRefContext ctx) {
    throw new IllegalStateException("Unqualified names currently disabled.");
    //return new UnqualifiedJClassRef(qualifiedClassname,ctx);
  }

  // ========================================================================
  // Constructors

  private UnqualifiedJClassRef(String ucname,
                               JClassRefContext ctx)
  {
    if (ctx == null) throw new IllegalArgumentException("null ctx");
    if (ucname == null) throw new IllegalArgumentException("null ucname");
    mContext = ctx;
    mUnqualifiedClassname = ucname;
    if (VERBOSE) System.out.println("[UnqualifiedJClassRef] created for '"+
                                    ucname+"'");
  }

  // ========================================================================
  // JClassRef implementation

  public JClass getRefClass() {
    //FIXME this needs optimization, keep it simple and lazy for now
    return mContext.getClassLoader().loadClass(getQualifiedName());
  }

  public String getQualifiedName() {
    if (mQualifiedClassname != null) return mQualifiedClassname;
    // ok, check to see if it's an array type.  if so, we want to strip
    // away all the brackets and so we can try to load just the component
    // type.
    String candidateName;
    int arrayDimensions = 0;
    int bracket = mUnqualifiedClassname.indexOf('[');
    if (bracket != -1) {
      candidateName = mUnqualifiedClassname.substring(0,bracket);
      do {
        arrayDimensions++;
        bracket = mUnqualifiedClassname.indexOf('[',bracket+1);
      } while(bracket != -1);
    } else {
      candidateName = mUnqualifiedClassname;
    }
    // ok, try to get the class that they are talking about
    String name = qualifyName(candidateName);
    if (name == null) {
      throw new IllegalStateException("unable to handle unqualified java type "+
                                      "reference '"+candidateName+" ["+
                                      mUnqualifiedClassname+"]'. "+
                                      "This is still partially NYI.");
    }
    // now if it was an array, we need to convert it into a corresponding
    // field descriptor
    if (arrayDimensions > 0) {
      StringWriter out = new StringWriter();
      for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy