/******************************************************************************* * Copyright (c) 2005, 2010 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. * * Contributors: * Oracle - initial API and implementation ******************************************************************************/ package org.eclipse.jpt.common.utility.internal; import java.io.PrintWriter; import java.io.Serializable; import org.eclipse.jpt.common.utility.JavaType; /** * Straightforward implementation of the {@link JavaType} interface. */ public final class SimpleJavaType implements JavaType, Cloneable, Serializable { /** * store the type as a name, so we can reference classes * that are not loaded */ private final String elementTypeName; /** * non-array types have an array depth of zero */ private final int arrayDepth; private static final String BRACKETS = "[]"; //$NON-NLS-1$ private static final long serialVersionUID = 1L; // ********** constructors ********** /** * Construct a Java type with the specified element type and array depth. */ public SimpleJavaType(String elementTypeName, int arrayDepth) { super(); if ((elementTypeName == null) || (elementTypeName.length() == 0)) { throw new IllegalArgumentException("The element type name is required."); //$NON-NLS-1$ } if (ClassName.getArrayDepth(elementTypeName) != 0) { // e.g. "[Ljava.lang.Object;" throw new IllegalArgumentException("The element type must not be an array: " + elementTypeName + '.'); //$NON-NLS-1$ } if (arrayDepth < 0) { throw new IllegalArgumentException("The array depth must be greater than or equal to zero: " + arrayDepth + '.'); //$NON-NLS-1$ } if (elementTypeName.equals(void.class.getName()) && (arrayDepth != 0)) { throw new IllegalArgumentException("'void' must have an array depth of zero: " + arrayDepth + '.'); //$NON-NLS-1$ } this.elementTypeName = elementTypeName; this.arrayDepth = arrayDepth; } /** * Construct a Java type for the specified class. * The class name can be in one of the following forms:
* - java.lang.Object
*
- int
*
- java.util.Map$Entry
*
- [Ljava.lang.Object;
*
- [I
*
- [Ljava.util.Map$Entry;
*
'$'
version of the name is used in {@link Class#forName(String)},
* but the '.'
version of the name is used in source code.
* Very irritating....
*/
private String getElementTypeNameDeclaration() {
return this.elementTypeName.replace('$', '.');
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.getClass().getSimpleName());
sb.append('(');
this.appendDeclarationTo(sb);
sb.append(')');
return sb.toString();
}
// ********** cloning **********
@Override
public Object clone() {
try {
return super.clone();
} catch (CloneNotSupportedException ex) {
throw new InternalError();
}
}
}