1 /* 2 * Copyright (C) 2007-2010 Jlio Vilmar Gesser. 3 * Copyright (C) 2011, 2013-2016 The JavaParser Team. 4 * 5 * This file is part of JavaParser. 6 * 7 * JavaParser can be used either under the terms of 8 * a) the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * b) the terms of the Apache License 12 * 13 * You should have received a copy of both licenses in LICENCE.LGPL and 14 * LICENCE.APACHE. Please refer to those files for details. 15 * 16 * JavaParser is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU Lesser General Public License for more details. 20 */ 21 package com.github.javaparser.ast.stmt; 22 23 import com.github.javaparser.ast.AllFieldsConstructor; 24 import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; 25 import com.github.javaparser.ast.observer.ObservableProperty; 26 import com.github.javaparser.ast.visitor.GenericVisitor; 27 import com.github.javaparser.ast.visitor.VoidVisitor; 28 import static com.github.javaparser.utils.Utils.assertNotNull; 29 import com.github.javaparser.ast.Node; 30 import com.github.javaparser.ast.visitor.CloneVisitor; 31 import com.github.javaparser.metamodel.LocalClassDeclarationStmtMetaModel; 32 import com.github.javaparser.metamodel.JavaParserMetaModel; 33 import javax.annotation.Generated; 34 import com.github.javaparser.TokenRange; 35 import java.util.function.Consumer; 36 import java.util.Optional; 37 38 /** 39 * <h1>A class declaration inside a method.</h1> 40 * <h2>Java 1.0</h2> 41 * Not available. 42 * <h2>Java 1.1+</h2> 43 * A statement consisting of a class declaration. 44 * <br/><code>class X { void m() { <b>class Y { }</b> } }</code> 45 * 46 * @see ClassOrInterfaceDeclaration 47 * @author Julio Vilmar Gesser 48 */ 49 public final class LocalClassDeclarationStmt extends Statement { 50 51 private ClassOrInterfaceDeclaration classDeclaration; 52 53 public LocalClassDeclarationStmt() { 54 this(null, new ClassOrInterfaceDeclaration()); 55 } 56 57 @AllFieldsConstructor 58 public LocalClassDeclarationStmt(final ClassOrInterfaceDeclaration classDeclaration) { 59 this(null, classDeclaration); 60 } 61 62 /** 63 * This constructor is used by the parser and is considered private. 64 */ 65 @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") 66 public LocalClassDeclarationStmt(TokenRange tokenRange, ClassOrInterfaceDeclaration classDeclaration) { 67 super(tokenRange); 68 setClassDeclaration(classDeclaration); 69 customInitialization(); 70 } 71 72 @Override 73 @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") 74 public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { 75 return v.visit(this, arg); 76 } 77 78 @Override 79 @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") 80 public <A> void accept(final VoidVisitor<A> v, final A arg) { 81 v.visit(this, arg); 82 } 83 84 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") 85 public ClassOrInterfaceDeclaration getClassDeclaration() { 86 return classDeclaration; 87 } 88 89 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") 90 public LocalClassDeclarationStmt setClassDeclaration(final ClassOrInterfaceDeclaration classDeclaration) { 91 assertNotNull(classDeclaration); 92 if (classDeclaration == this.classDeclaration) { 93 return (LocalClassDeclarationStmt) this; 94 } 95 notifyPropertyChange(ObservableProperty.CLASS_DECLARATION, this.classDeclaration, classDeclaration); 96 if (this.classDeclaration != null) 97 this.classDeclaration.setParentNode(null); 98 this.classDeclaration = classDeclaration; 99 setAsParentNodeOf(classDeclaration); 100 return this; 101 } 102 103 @Override 104 @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") 105 public boolean remove(Node node) { 106 if (node == null) 107 return false; 108 return super.remove(node); 109 } 110 111 @Override 112 @Generated("com.github.javaparser.generator.core.node.CloneGenerator") 113 public LocalClassDeclarationStmt clone() { 114 return (LocalClassDeclarationStmt) accept(new CloneVisitor(), null); 115 } 116 117 @Override 118 @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") 119 public LocalClassDeclarationStmtMetaModel getMetaModel() { 120 return JavaParserMetaModel.localClassDeclarationStmtMetaModel; 121 } 122 123 @Override 124 @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") 125 public boolean replace(Node node, Node replacementNode) { 126 if (node == null) 127 return false; 128 if (node == classDeclaration) { 129 setClassDeclaration((ClassOrInterfaceDeclaration) replacementNode); 130 return true; 131 } 132 return super.replace(node, replacementNode); 133 } 134 135 @Override 136 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 137 public boolean isLocalClassDeclarationStmt() { 138 return true; 139 } 140 141 @Override 142 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 143 public LocalClassDeclarationStmt asLocalClassDeclarationStmt() { 144 return this; 145 } 146 147 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 148 public void ifLocalClassDeclarationStmt(Consumer<LocalClassDeclarationStmt> action) { 149 action.accept(this); 150 } 151 152 @Override 153 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 154 public Optional<LocalClassDeclarationStmt> toLocalClassDeclarationStmt() { 155 return Optional.of(this); 156 } 157 } 158