1 | /* |
---|---|
2 | * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser. |
3 | * Copyright (C) 2011, 2013-2020 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 | |
22 | package com.github.javaparser.ast.nodeTypes; |
23 | |
24 | import com.github.javaparser.ast.Node; |
25 | import com.github.javaparser.ast.NodeList; |
26 | import com.github.javaparser.ast.type.ClassOrInterfaceType; |
27 | |
28 | import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType; |
29 | |
30 | /** |
31 | * A node that explicitly extends other types, using the {@code extends} keyword. |
32 | */ |
33 | public interface NodeWithExtends<N extends Node> { |
34 | |
35 | /** |
36 | * @return All extended types that have been explicitly added (thus exist within the AST). |
37 | * Note that this can contain more than one item if this is an interface. |
38 | * Note that this will not include {@code java.lang.Object} unless it is explicitly added (e.g. {@code class X extends Object {}}) |
39 | * If you want the implicitly extended types, you will need a resolved reference. |
40 | */ |
41 | NodeList<ClassOrInterfaceType> getExtendedTypes(); |
42 | |
43 | void tryAddImportToParentCompilationUnit(Class<?> clazz); |
44 | |
45 | default ClassOrInterfaceType getExtendedTypes(int i) { |
46 | return getExtendedTypes().get(i); |
47 | } |
48 | |
49 | N setExtendedTypes(NodeList<ClassOrInterfaceType> extendsList); |
50 | |
51 | @SuppressWarnings("unchecked") |
52 | default N setExtendedType(int i, ClassOrInterfaceType extend) { |
53 | getExtendedTypes().set(i, extend); |
54 | return (N) this; |
55 | } |
56 | |
57 | @SuppressWarnings("unchecked") |
58 | default N addExtendedType(ClassOrInterfaceType extend) { |
59 | getExtendedTypes().add(extend); |
60 | return (N) this; |
61 | } |
62 | |
63 | /** |
64 | * @deprecated use addExtendedType |
65 | */ |
66 | @Deprecated |
67 | default N addExtends(Class<?> clazz) { |
68 | return addExtendedType(clazz); |
69 | } |
70 | |
71 | /** |
72 | * @deprecated use addExtendedType |
73 | */ |
74 | @Deprecated |
75 | default N addExtends(String name) { |
76 | return addExtendedType(name); |
77 | } |
78 | |
79 | /** |
80 | * Add an "extends" to this and automatically add the import |
81 | * |
82 | * @param clazz the class to extend from |
83 | * @return this |
84 | */ |
85 | default N addExtendedType(Class<?> clazz) { |
86 | tryAddImportToParentCompilationUnit(clazz); |
87 | return addExtendedType(clazz.getSimpleName()); |
88 | } |
89 | |
90 | /** |
91 | * Add an "extends" to this |
92 | * |
93 | * @param name the name of the type to extends from |
94 | * @return this |
95 | */ |
96 | @SuppressWarnings("unchecked") |
97 | default N addExtendedType(String name) { |
98 | getExtendedTypes().add(parseClassOrInterfaceType(name)); |
99 | return (N) this; |
100 | } |
101 | |
102 | } |
103 |
Members