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.AccessSpecifier; |
25 | import com.github.javaparser.ast.Modifier; |
26 | import com.github.javaparser.ast.Node; |
27 | import com.github.javaparser.ast.NodeList; |
28 | import com.github.javaparser.resolution.declarations.HasAccessSpecifier; |
29 | |
30 | import java.util.Arrays; |
31 | import java.util.List; |
32 | import java.util.function.Supplier; |
33 | import java.util.stream.Collectors; |
34 | |
35 | import static com.github.javaparser.ast.NodeList.toNodeList; |
36 | |
37 | /** |
38 | * A Node with Modifiers. |
39 | * Note that not all modifiers may be valid for this node. |
40 | */ |
41 | public interface NodeWithModifiers<N extends Node> { |
42 | /** |
43 | * Return the modifiers of this variable declaration. |
44 | * Warning: modifying the returned set will not trigger observers, |
45 | * you have to use setModifiers for that. |
46 | * |
47 | * @return modifiers |
48 | * @see Modifier |
49 | */ |
50 | NodeList<Modifier> getModifiers(); |
51 | |
52 | N setModifiers(NodeList<Modifier> modifiers); |
53 | |
54 | @SuppressWarnings("unchecked") |
55 | default N addModifier(Modifier.Keyword... newModifiers) { |
56 | NodeList<Modifier> existingModifiers = new NodeList<>(getModifiers()); |
57 | for (Modifier.Keyword newModifier : newModifiers) { |
58 | boolean alreadyPresent = existingModifiers.stream().anyMatch(m -> m.getKeyword() == newModifier); |
59 | if (!alreadyPresent) { |
60 | existingModifiers.add(new Modifier(newModifier)); |
61 | } |
62 | } |
63 | setModifiers(existingModifiers); |
64 | return (N) this; |
65 | } |
66 | |
67 | @SuppressWarnings("unchecked") |
68 | default N removeModifier(Modifier.Keyword... modifiersToRemove) { |
69 | List<Modifier.Keyword> modifiersToRemoveAsList = Arrays.asList(modifiersToRemove); |
70 | NodeList<Modifier> remaining = getModifiers().stream() |
71 | .filter(existingModifier -> !modifiersToRemoveAsList.contains(existingModifier.getKeyword())) |
72 | .collect(toNodeList()); |
73 | setModifiers(remaining); |
74 | return (N) this; |
75 | } |
76 | |
77 | default N setModifier(Modifier.Keyword m, boolean set) { |
78 | if (set) { |
79 | return addModifier(m); |
80 | } else { |
81 | return removeModifier(m); |
82 | } |
83 | } |
84 | |
85 | default boolean hasModifier(Modifier.Keyword modifier) { |
86 | for (Modifier m : getModifiers()) { |
87 | if (m.getKeyword() == modifier) { |
88 | return true; |
89 | } |
90 | } |
91 | return false; |
92 | } |
93 | |
94 | /** |
95 | * Creates a list of modifier nodes corresponding to the keywords passed, and set it. |
96 | */ |
97 | default N setModifiers(final Modifier.Keyword... modifiers) { |
98 | return setModifiers(Arrays.stream(modifiers).map(Modifier::new).collect(toNodeList())); |
99 | } |
100 | |
101 | /** |
102 | * @return the access specifier as far as it can be derived from the modifiers. |
103 | * Does not take anything else into account (like "interface methods are implicitly public") |
104 | */ |
105 | default AccessSpecifier getAccessSpecifier() { |
106 | for (Modifier modifier : getModifiers()) { |
107 | switch (modifier.getKeyword()) { |
108 | case PUBLIC: |
109 | return AccessSpecifier.PUBLIC; |
110 | case PROTECTED: |
111 | return AccessSpecifier.PROTECTED; |
112 | case PRIVATE: |
113 | return AccessSpecifier.PRIVATE; |
114 | } |
115 | } |
116 | return AccessSpecifier.PACKAGE_PRIVATE; |
117 | } |
118 | } |
119 |
Members