/*
* Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
* Copyright (C) 2011, 2013-2016 The JavaParser Team.
*
* This file is part of JavaParser.
*
* JavaParser can be used either under the terms of
* a) the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* b) the terms of the Apache License
*
* You should have received a copy of both licenses in LICENCE.LGPL and
* LICENCE.APACHE. Please refer to those files for details.
*
* JavaParser is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*/
package com.github.javaparser.ast.type;
import com.github.javaparser.Range;
import com.github.javaparser.ast.AllFieldsConstructor;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.ast.expr.SimpleName;
import com.github.javaparser.ast.nodeTypes.NodeWithAnnotations;
import com.github.javaparser.ast.nodeTypes.NodeWithSimpleName;
import com.github.javaparser.ast.nodeTypes.NodeWithTypeArguments;
import com.github.javaparser.ast.observer.ObservableProperty;
import com.github.javaparser.ast.visitor.CloneVisitor;
import com.github.javaparser.ast.visitor.GenericVisitor;
import com.github.javaparser.ast.visitor.VoidVisitor;
import com.github.javaparser.metamodel.ClassOrInterfaceTypeMetaModel;
import com.github.javaparser.metamodel.JavaParserMetaModel;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static com.github.javaparser.utils.Utils.assertNotNull;
import static java.util.stream.Collectors.joining;
import javax.annotation.Generated;
/**
* A class or an interface type.
Object
HashMap<String, String>
*
java.util.Punchcard
*
*
Note that the syntax is ambiguous here, and JavaParser does not know what is to the left of the class. It assumes
* cases like Map.Entry
where Map is the scope of Entry. In java.util.Punchcard
, it will not
* recognize that java and util are parts of the package name. Instead, it will set util as the scope of Punchcard, as a
* ClassOrInterfaceType (which it is not.) In turn, util will have java as its scope, also as a
* ClassOrInterfaceType
*
* @author Julio Vilmar Gesser
*/
public final class ClassOrInterfaceType extends ReferenceType implements NodeWithSimpleName, NodeWithAnnotations, NodeWithTypeArguments {
private ClassOrInterfaceType scope;
private SimpleName name;
private NodeList typeArguments;
public ClassOrInterfaceType() {
this(null, null, new SimpleName(), null, new NodeList<>());
}
/**
* @deprecated use JavaParser.parseClassOrInterfaceType instead. This constructor does not understand generics.
*/
public ClassOrInterfaceType(final String name) {
this(null, null, new SimpleName(name), null, new NodeList<>());
}
public ClassOrInterfaceType(final ClassOrInterfaceType scope, final String name) {
this(null, scope, new SimpleName(name), null, new NodeList<>());
}
public ClassOrInterfaceType(final ClassOrInterfaceType scope, final SimpleName name, final NodeList typeArguments) {
this(null, scope, name, typeArguments, new NodeList<>());
}
@AllFieldsConstructor
public ClassOrInterfaceType(final ClassOrInterfaceType scope, final SimpleName name, final NodeList typeArguments, final NodeList annotations) {
this(null, scope, name, typeArguments, annotations);
}
/**This constructor is used by the parser and is considered private.*/
@Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator")
public ClassOrInterfaceType(Range range, ClassOrInterfaceType scope, SimpleName name, NodeList typeArguments, NodeList annotations) {
super(range, annotations);
setScope(scope);
setName(name);
setTypeArguments(typeArguments);
customInitialization();
}
@Override
public R accept(final GenericVisitor v, final A arg) {
return v.visit(this, arg);
}
@Override
public void accept(final VoidVisitor v, final A arg) {
v.visit(this, arg);
}
@Override
public SimpleName getName() {
return name;
}
public Optional getScope() {
return Optional.ofNullable(scope);
}
public boolean isBoxedType() {
return PrimitiveType.unboxMap.containsKey(name.getIdentifier());
}
public PrimitiveType toUnboxedType() throws UnsupportedOperationException {
if (!isBoxedType()) {
throw new UnsupportedOperationException(name + " isn't a boxed type.");
}
return new PrimitiveType(PrimitiveType.unboxMap.get(name.getIdentifier()));
}
@Override
public ClassOrInterfaceType setName(final SimpleName name) {
assertNotNull(name);
if (name == this.name) {
return (ClassOrInterfaceType) this;
}
notifyPropertyChange(ObservableProperty.NAME, this.name, name);
if (this.name != null)
this.name.setParentNode(null);
this.name = name;
setAsParentNodeOf(name);
return this;
}
/**
* Sets the scope
*
* @param scope the scope, can be null
* @return this, the ClassOrInterfaceType
*/
public ClassOrInterfaceType setScope(final ClassOrInterfaceType scope) {
if (scope == this.scope) {
return (ClassOrInterfaceType) this;
}
notifyPropertyChange(ObservableProperty.SCOPE, this.scope, scope);
if (this.scope != null)
this.scope.setParentNode(null);
this.scope = scope;
setAsParentNodeOf(scope);
return this;
}
@Override
public Optional> getTypeArguments() {
return Optional.ofNullable(typeArguments);
}
/**
* Sets the typeArguments
*
* @param typeArguments the typeArguments, can be null
* @return this, the ClassOrInterfaceType
*/
@Override
public ClassOrInterfaceType setTypeArguments(final NodeList typeArguments) {
if (typeArguments == this.typeArguments) {
return (ClassOrInterfaceType) this;
}
notifyPropertyChange(ObservableProperty.TYPE_ARGUMENTS, this.typeArguments, typeArguments);
if (this.typeArguments != null)
this.typeArguments.setParentNode(null);
this.typeArguments = typeArguments;
setAsParentNodeOf(typeArguments);
return this;
}
@Override
public ClassOrInterfaceType setAnnotations(NodeList annotations) {
return (ClassOrInterfaceType) super.setAnnotations(annotations);
}
@Override
@Generated("com.github.javaparser.generator.core.node.GetNodeListsGenerator")
public List> getNodeLists() {
return Arrays.asList(getTypeArguments().orElse(null), getAnnotations());
}
@Override
public boolean remove(Node node) {
if (node == null)
return false;
if (scope != null) {
if (node == scope) {
removeScope();
return true;
}
}
if (typeArguments != null) {
for (int i = 0; i < typeArguments.size(); i++) {
if (typeArguments.get(i) == node) {
typeArguments.remove(i);
return true;
}
}
}
return super.remove(node);
}
@Override
public String asString() {
StringBuilder str = new StringBuilder();
getScope().ifPresent(s -> str.append(s.asString()).append("."));
str.append(name.asString());
getTypeArguments().ifPresent(ta -> str.append(ta.stream().map(Type::asString).collect(joining(",", "<", ">"))));
return str.toString();
}
public ClassOrInterfaceType removeScope() {
return setScope((ClassOrInterfaceType) null);
}
@Override
@Generated("com.github.javaparser.generator.core.node.CloneGenerator")
public ClassOrInterfaceType clone() {
return (ClassOrInterfaceType) accept(new CloneVisitor(), null);
}
@Override
@Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator")
public ClassOrInterfaceTypeMetaModel getMetaModel() {
return JavaParserMetaModel.classOrInterfaceTypeMetaModel;
}
}