com.sun.codemodel.JNarrowedClass Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of codemodel Show documentation
Show all versions of codemodel Show documentation
The core functionality of the CodeModel java source code generation library
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.codemodel;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
/**
* Represents X<Y>.
*
* TODO: consider separating the decl and the use.
*
* @author
* Kohsuke Kawaguchi ([email protected])
*/
class JNarrowedClass extends JClass {
/**
* A generic class with type parameters.
*/
final JClass basis;
/**
* Arguments to those parameters.
*/
private final List args;
JNarrowedClass(JClass basis, JClass arg) {
this(basis,Collections.singletonList(arg));
}
JNarrowedClass(JClass basis, List args) {
super(basis.owner());
this.basis = basis;
assert !(basis instanceof JNarrowedClass);
this.args = args;
}
public JClass narrow( JClass clazz ) {
List newArgs = new ArrayList(args);
newArgs.add(clazz);
return new JNarrowedClass(basis,newArgs);
}
public JClass narrow( JClass... clazz ) {
List newArgs = new ArrayList(args);
for (JClass c : clazz)
newArgs.add(c);
return new JNarrowedClass(basis,newArgs);
}
public String name() {
StringBuffer buf = new StringBuffer();
buf.append(basis.name());
buf.append('<');
boolean first = true;
for (JClass c : args) {
if(first)
first = false;
else
buf.append(',');
buf.append(c.name());
}
buf.append('>');
return buf.toString();
}
public String fullName() {
StringBuilder buf = new StringBuilder();
buf.append(basis.fullName());
buf.append('<');
boolean first = true;
for (JClass c : args) {
if(first)
first = false;
else
buf.append(',');
buf.append(c.fullName());
}
buf.append('>');
return buf.toString();
}
public String binaryName() {
StringBuilder buf = new StringBuilder();
buf.append(basis.binaryName());
buf.append('<');
boolean first = true;
for (JClass c : args) {
if(first)
first = false;
else
buf.append(',');
buf.append(c.binaryName());
}
buf.append('>');
return buf.toString();
}
public void generate(JFormatter f) {
f.t(basis).p('<').g(args).p(JFormatter.CLOSE_TYPE_ARGS);
}
@Override
void printLink(JFormatter f) {
basis.printLink(f);
f.p("{@code <}");
boolean first = true;
for( JClass c : args ) {
if(first)
first = false;
else
f.p(',');
c.printLink(f);
}
f.p("{@code >}");
}
public JPackage _package() {
return basis._package();
}
public JClass _extends() {
JClass base = basis._extends();
if(base==null) return base;
return base.substituteParams(basis.typeParams(),args);
}
public Iterator _implements() {
return new Iterator() {
private final Iterator core = basis._implements();
public void remove() {
core.remove();
}
public JClass next() {
return core.next().substituteParams(basis.typeParams(),args);
}
public boolean hasNext() {
return core.hasNext();
}
};
}
public JClass erasure() {
return basis;
}
public boolean isInterface() {
return basis.isInterface();
}
public boolean isAbstract() {
return basis.isAbstract();
}
public boolean isArray() {
return false;
}
//
// Equality is based on value
//
public boolean equals(Object obj) {
if(!(obj instanceof JNarrowedClass)) return false;
return fullName().equals(((JClass)obj).fullName());
}
public int hashCode() {
return fullName().hashCode();
}
protected JClass substituteParams(JTypeVar[] variables, List bindings) {
JClass b = basis.substituteParams(variables,bindings);
boolean different = b!=basis;
List clazz = new ArrayList(args.size());
for( int i=0; i getTypeParameters() {
return args;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy