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.expr; 22 23 import com.github.javaparser.ast.AllFieldsConstructor; 24 import com.github.javaparser.ast.body.TypeDeclaration; 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 java.util.Optional; 29 import com.github.javaparser.ast.Node; 30 import com.github.javaparser.ast.visitor.CloneVisitor; 31 import com.github.javaparser.metamodel.OptionalProperty; 32 import com.github.javaparser.metamodel.ThisExprMetaModel; 33 import com.github.javaparser.metamodel.JavaParserMetaModel; 34 import javax.annotation.Generated; 35 import com.github.javaparser.TokenRange; 36 import com.github.javaparser.resolution.Resolvable; 37 import com.github.javaparser.resolution.declarations.ResolvedConstructorDeclaration; 38 import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration; 39 import java.util.function.Consumer; 40 41 /** 42 * An occurrence of the "this" keyword. <br/><code>World.this.greet()</code> is a MethodCallExpr of method name greet, 43 * and scope "World.super" which is a ThisExpr with classExpr "World". <br/><code>this.name</code> is a 44 * FieldAccessExpr of field greet, and a ThisExpr as its scope. The ThisExpr has no classExpr. 45 * 46 * @author Julio Vilmar Gesser 47 * @see com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt 48 * @see ThisExpr 49 */ 50 public final class ThisExpr extends Expression implements Resolvable<ResolvedTypeDeclaration> { 51 52 @OptionalProperty 53 private Expression classExpr; 54 55 public ThisExpr() { 56 this(null, null); 57 } 58 59 @AllFieldsConstructor 60 public ThisExpr(final Expression classExpr) { 61 this(null, classExpr); 62 } 63 64 /** 65 * This constructor is used by the parser and is considered private. 66 */ 67 @Generated("com.github.javaparser.generator.core.node.MainConstructorGenerator") 68 public ThisExpr(TokenRange tokenRange, Expression classExpr) { 69 super(tokenRange); 70 setClassExpr(classExpr); 71 customInitialization(); 72 } 73 74 @Override 75 @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") 76 public <R, A> R accept(final GenericVisitor<R, A> v, final A arg) { 77 return v.visit(this, arg); 78 } 79 80 @Override 81 @Generated("com.github.javaparser.generator.core.node.AcceptGenerator") 82 public <A> void accept(final VoidVisitor<A> v, final A arg) { 83 v.visit(this, arg); 84 } 85 86 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") 87 public Optional<Expression> getClassExpr() { 88 return Optional.ofNullable(classExpr); 89 } 90 91 @Generated("com.github.javaparser.generator.core.node.PropertyGenerator") 92 public ThisExpr setClassExpr(final Expression classExpr) { 93 if (classExpr == this.classExpr) { 94 return (ThisExpr) this; 95 } 96 notifyPropertyChange(ObservableProperty.CLASS_EXPR, this.classExpr, classExpr); 97 if (this.classExpr != null) 98 this.classExpr.setParentNode(null); 99 this.classExpr = classExpr; 100 setAsParentNodeOf(classExpr); 101 return this; 102 } 103 104 @Override 105 @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") 106 public boolean remove(Node node) { 107 if (node == null) 108 return false; 109 if (classExpr != null) { 110 if (node == classExpr) { 111 removeClassExpr(); 112 return true; 113 } 114 } 115 return super.remove(node); 116 } 117 118 @Generated("com.github.javaparser.generator.core.node.RemoveMethodGenerator") 119 public ThisExpr removeClassExpr() { 120 return setClassExpr((Expression) null); 121 } 122 123 @Override 124 @Generated("com.github.javaparser.generator.core.node.CloneGenerator") 125 public ThisExpr clone() { 126 return (ThisExpr) accept(new CloneVisitor(), null); 127 } 128 129 @Override 130 @Generated("com.github.javaparser.generator.core.node.GetMetaModelGenerator") 131 public ThisExprMetaModel getMetaModel() { 132 return JavaParserMetaModel.thisExprMetaModel; 133 } 134 135 @Override 136 @Generated("com.github.javaparser.generator.core.node.ReplaceMethodGenerator") 137 public boolean replace(Node node, Node replacementNode) { 138 if (node == null) 139 return false; 140 if (classExpr != null) { 141 if (node == classExpr) { 142 setClassExpr((Expression) replacementNode); 143 return true; 144 } 145 } 146 return super.replace(node, replacementNode); 147 } 148 149 @Override 150 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 151 public boolean isThisExpr() { 152 return true; 153 } 154 155 @Override 156 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 157 public ThisExpr asThisExpr() { 158 return this; 159 } 160 161 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 162 public void ifThisExpr(Consumer<ThisExpr> action) { 163 action.accept(this); 164 } 165 166 @Override 167 public ResolvedTypeDeclaration resolve() { 168 return getSymbolResolver().resolveDeclaration(this, ResolvedTypeDeclaration.class); 169 } 170 171 @Override 172 @Generated("com.github.javaparser.generator.core.node.TypeCastingGenerator") 173 public Optional<ThisExpr> toThisExpr() { 174 return Optional.of(this); 175 } 176 } 177