1 | /******************************************************************************* |
---|---|
2 | * Copyright (c) 2018 IBM Corporation and others. |
3 | * |
4 | * This program and the accompanying materials |
5 | * are made available under the terms of the Eclipse Public License 2.0 |
6 | * which accompanies this distribution, and is available at |
7 | * https://www.eclipse.org/legal/epl-2.0/ |
8 | * |
9 | * SPDX-License-Identifier: EPL-2.0 |
10 | * |
11 | * Contributors: |
12 | * IBM Corporation - initial API and implementation |
13 | *******************************************************************************/ |
14 | package org.eclipse.jdt.core.dom; |
15 | |
16 | import org.eclipse.jdt.core.IJavaElement; |
17 | import org.eclipse.jdt.core.compiler.CharOperation; |
18 | import org.eclipse.jdt.internal.compiler.lookup.ModuleBinding; |
19 | import org.eclipse.jdt.internal.compiler.lookup.PackageBinding; |
20 | import org.eclipse.jdt.internal.compiler.util.Util; |
21 | |
22 | /** |
23 | * This class represents the recovered binding for a package |
24 | */ |
25 | class RecoveredPackageBinding implements IPackageBinding { |
26 | |
27 | private static final String[] NO_NAME_COMPONENTS = CharOperation.NO_STRINGS; |
28 | private static final String UNNAMED = Util.EMPTY_STRING; |
29 | private static final char PACKAGE_NAME_SEPARATOR = '.'; |
30 | |
31 | private PackageBinding binding; |
32 | private BindingResolver resolver; |
33 | private String name = null; |
34 | private String[] components = null; |
35 | |
36 | RecoveredPackageBinding(org.eclipse.jdt.internal.compiler.lookup.PackageBinding binding, BindingResolver resolver) { |
37 | this.binding = binding; |
38 | this.resolver = resolver; |
39 | } |
40 | |
41 | @Override |
42 | public IAnnotationBinding[] getAnnotations() { |
43 | return AnnotationBinding.NoAnnotations; |
44 | } |
45 | |
46 | @Override |
47 | public int getKind() { |
48 | return IBinding.PACKAGE; |
49 | } |
50 | |
51 | @Override |
52 | public int getModifiers() { |
53 | return Modifier.NONE; |
54 | } |
55 | |
56 | @Override |
57 | public boolean isDeprecated() { |
58 | return false; |
59 | } |
60 | |
61 | @Override |
62 | public boolean isRecovered() { |
63 | return true; |
64 | } |
65 | |
66 | @Override |
67 | public boolean isSynthetic() { |
68 | return false; |
69 | } |
70 | |
71 | @Override |
72 | public IJavaElement getJavaElement() { |
73 | return null; |
74 | } |
75 | |
76 | @Override |
77 | public String getKey() { |
78 | StringBuilder buffer = new StringBuilder(); |
79 | buffer.append("Recovered#"); //$NON-NLS-1$ |
80 | buffer.append(this.binding.computeUniqueKey()); |
81 | return buffer.toString(); |
82 | } |
83 | |
84 | @Override |
85 | public boolean isEqualTo(IBinding other) { |
86 | if (!other.isRecovered() || other.getKind() != IBinding.PACKAGE) return false; |
87 | return getKey().equals(other.getKey()); |
88 | } |
89 | |
90 | @Override |
91 | public String getName() { |
92 | if (this.name == null) { |
93 | computeNameAndComponents(); |
94 | } |
95 | return this.name; |
96 | } |
97 | |
98 | @Override |
99 | public boolean isUnnamed() { |
100 | return false; |
101 | } |
102 | |
103 | @Override |
104 | public String[] getNameComponents() { |
105 | if (this.components == null) { |
106 | computeNameAndComponents(); |
107 | } |
108 | return this.components; |
109 | } |
110 | @Override |
111 | public IModuleBinding getModule() { |
112 | ModuleBinding moduleBinding = this.binding.enclosingModule; |
113 | return moduleBinding != null ? this.resolver.getModuleBinding(moduleBinding) : null; |
114 | } |
115 | private void computeNameAndComponents() { |
116 | char[][] compoundName = this.binding.compoundName; |
117 | if (compoundName == CharOperation.NO_CHAR_CHAR || compoundName == null) { |
118 | this.name = UNNAMED; |
119 | this.components = NO_NAME_COMPONENTS; |
120 | } else { |
121 | int length = compoundName.length; |
122 | this.components = new String[length]; |
123 | StringBuilder buffer = new StringBuilder(); |
124 | for (int i = 0; i < length - 1; i++) { |
125 | this.components[i] = new String(compoundName[i]); |
126 | buffer.append(compoundName[i]).append(PACKAGE_NAME_SEPARATOR); |
127 | } |
128 | this.components[length - 1] = new String(compoundName[length - 1]); |
129 | buffer.append(compoundName[length - 1]); |
130 | this.name = buffer.toString(); |
131 | } |
132 | } |
133 | |
134 | } |
135 |
Members