1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
/*
* Copyright 2019 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.turbine.processing;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import com.google.turbine.binder.sym.ClassSymbol;
import com.google.turbine.binder.sym.FieldSymbol;
import com.google.turbine.binder.sym.PackageSymbol;
import com.google.turbine.binder.sym.Symbol;
import com.google.turbine.model.Const;
import com.google.turbine.model.TurbineFlag;
import com.google.turbine.model.TurbineVisibility;
import com.google.turbine.processing.TurbineElement.TurbineExecutableElement;
import com.google.turbine.processing.TurbineElement.TurbineFieldElement;
import com.google.turbine.processing.TurbineElement.TurbineTypeElement;
import com.google.turbine.processing.TurbineTypeMirror.TurbineExecutableType;
import com.google.turbine.type.AnnoInfo;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.Name;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import org.checkerframework.checker.nullness.qual.Nullable;
/** An implementation of {@link Elements} backed by turbine's {@link Element}. */
@SuppressWarnings("nullness") // TODO(cushon): Address nullness diagnostics.
public class TurbineElements implements Elements {
private final ModelFactory factory;
private final TurbineTypes types;
public TurbineElements(ModelFactory factory, TurbineTypes types) {
this.factory = factory;
this.types = types;
}
private static Symbol asSymbol(Element element) {
if (!(element instanceof TurbineElement)) {
throw new IllegalArgumentException(element.toString());
}
return ((TurbineElement) element).sym();
}
@Override
public PackageElement getPackageElement(CharSequence name) {
ImmutableList<String> packageName = ImmutableList.copyOf(Splitter.on('.').split(name));
if (factory.tli().lookupPackage(packageName) == null) {
return null;
}
return factory.packageElement(new PackageSymbol(Joiner.on('/').join(packageName)));
}
@Override
public TypeElement getTypeElement(CharSequence name) {
ClassSymbol sym = factory.inferSymbol(name);
if (sym == null) {
return null;
}
if (factory.getSymbol(sym) == null) {
return null;
}
return factory.typeElement(sym);
}
@Override
public Map<? extends ExecutableElement, ? extends AnnotationValue> getElementValuesWithDefaults(
AnnotationMirror a) {
return ((TurbineAnnotationMirror) a).getElementValuesWithDefaults();
}
@Override
public String getDocComment(Element e) {
if (!(e instanceof TurbineElement)) {
throw new IllegalArgumentException(e.toString());
}
String comment = ((TurbineElement) e).javadoc();
if (comment == null) {
return null;
}
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String line : Splitter.on('\n').split(comment)) {
int start = 0;
if (!first) {
sb.append('\n');
while (start < line.length() && CharMatcher.whitespace().matches(line.charAt(start))) {
start++;
}
while (start < line.length() && line.charAt(start) == '*') {
start++;
}
}
sb.append(line, start, line.length());
first = false;
}
return sb.toString();
}
@Override
public boolean isDeprecated(Element element) {
if (!(element instanceof TurbineElement)) {
throw new IllegalArgumentException(element.toString());
}
for (AnnoInfo a : ((TurbineElement) element).annos()) {
if (a.sym().equals(ClassSymbol.DEPRECATED)) {
return true;
}
}
return false;
}
@Override
public Name getBinaryName(TypeElement element) {
if (!(element instanceof TurbineTypeElement)) {
throw new IllegalArgumentException(element.toString());
}
return getName(((TurbineTypeElement) element).sym().binaryName().replace('/', '.'));
}
/**
* {@inheritDoc}
*
* @throws IllegalArgumentException for module elements
*/
@Override
public PackageElement getPackageOf(Element element) {
Symbol sym = asSymbol(element);
return factory.packageElement(packageSymbol(sym));
}
private static PackageSymbol packageSymbol(Symbol sym) {
if (sym.symKind().equals(Symbol.Kind.PACKAGE)) {
return (PackageSymbol) sym;
}
return ModelFactory.enclosingClass(sym).owner();
}
@Override
public List<? extends Element> getAllMembers(TypeElement type) {
ClassSymbol s = (ClassSymbol) asSymbol(type);
PackageSymbol from = packageSymbol(s);
// keep track of processed methods grouped by their names, to handle overrides more efficiently
Multimap<String, TurbineExecutableElement> methods =
MultimapBuilder.linkedHashKeys().linkedHashSetValues().build();
// collect all members of each transitive supertype of the input
ImmutableList.Builder<Element> results = ImmutableList.builder();
for (ClassSymbol superType : factory.cha().transitiveSupertypes(s)) {
// Most of JSR-269 is implemented on top of turbine's model, instead of the Element and
// TypeMirror wrappers. We don't do that here because we need most of the Elements returned
// by getEnclosedElements anyways, and the work below benefits from some of the caching done
// by TurbineElement.
for (Element el : factory.typeElement(superType).getEnclosedElements()) {
Symbol sym = asSymbol(el);
switch (sym.symKind()) {
case METHOD:
TurbineExecutableElement m = (TurbineExecutableElement) el;
if (shouldAdd(s, from, methods, m)) {
methods.put(m.info().name(), m);
results.add(el);
}
break;
case FIELD:
if (shouldAdd(s, from, (TurbineFieldElement) el)) {
results.add(el);
}
break;
default:
results.add(el);
}
}
}
return results.build();
}
private boolean shouldAdd(
ClassSymbol s,
PackageSymbol from,
Multimap<String, TurbineExecutableElement> methods,
TurbineExecutableElement m) {
if (m.sym().owner().equals(s)) {
// always include methods (and constructors) declared in the given type
return true;
}
if (m.getKind() == ElementKind.CONSTRUCTOR) {
// skip constructors from super-types, because the spec says so
return false;
}
if (!isVisible(from, packageSymbol(m.sym()), TurbineVisibility.fromAccess(m.info().access()))) {
// skip invisible methods in supers
return false;
}
// otherwise check if we've seen methods that override, or are overridden by, the
// current method
Set<TurbineExecutableElement> overrides = new HashSet<>();
Set<TurbineExecutableElement> overridden = new HashSet<>();
String name = m.info().name();
for (TurbineExecutableElement other : methods.get(name)) {
if (overrides(m, other, (TypeElement) m.getEnclosingElement())) {
overrides.add(other);
continue;
}
if (overrides(other, m, (TypeElement) other.getEnclosingElement())) {
overridden.add(other);
continue;
}
}
if (!overridden.isEmpty()) {
// We've already processed method(s) that override this one; nothing to do here.
// If that's true, and we've *also* processed a methods that this one overrides,
// something has gone terribly wrong: since overriding is transitive the results
// contain a pair of methods that override each other.
checkState(overrides.isEmpty());
return false;
}
// Add this method, and remove any methods we've already processed that it overrides.
for (TurbineExecutableElement override : overrides) {
methods.remove(name, override);
}
return true;
}
private static boolean shouldAdd(ClassSymbol s, PackageSymbol from, TurbineFieldElement f) {
FieldSymbol sym = f.sym();
if (sym.owner().equals(s)) {
// always include fields declared in the given type
return true;
}
if (!isVisible(from, packageSymbol(sym), TurbineVisibility.fromAccess(f.info().access()))) {
// skip invisible fields in supers
return false;
}
return true;
}
/**
* Returns true if an element with the given {@code visibility} and located in package {@code
* from} is visible to elements in package {@code to}.
*/
private static boolean isVisible(
PackageSymbol from, PackageSymbol to, TurbineVisibility visibility) {
switch (visibility) {
case PUBLIC:
case PROTECTED:
break;
case PACKAGE:
return from.equals(to);
case PRIVATE:
return false;
}
return true;
}
@Override
public List<? extends AnnotationMirror> getAllAnnotationMirrors(Element element) {
return ((TurbineElement) element).getAllAnnotationMirrors();
}
@Override
public boolean hides(Element hider, Element hidden) {
if (!(hider instanceof TurbineElement)) {
throw new IllegalArgumentException(hider.toString());
}
if (!(hidden instanceof TurbineElement)) {
throw new IllegalArgumentException(hidden.toString());
}
return hides((TurbineElement) hider, (TurbineElement) hidden);
}
private boolean hides(TurbineElement hider, TurbineElement hidden) {
if (!hider.sym().symKind().equals(hidden.sym().symKind())) {
return false;
}
if (!hider.getSimpleName().equals(hidden.getSimpleName())) {
return false;
}
if (hider.sym().equals(hidden.sym())) {
return false;
}
if (!isVisibleForHiding(hider, hidden)) {
return false;
}
if (hider.sym().symKind().equals(Symbol.Kind.METHOD)) {
int access = ((TurbineExecutableElement) hider).info().access();
if ((access & TurbineFlag.ACC_STATIC) != TurbineFlag.ACC_STATIC) {
return false;
}
// Static interface methods shouldn't be able to hide static methods in super-interfaces,
// but include them anyways for bug-compatibility with javac, see:
// https://bugs.openjdk.java.net/browse/JDK-8275746
if (!types.isSubsignature(
(TurbineExecutableType) hider.asType(), (TurbineExecutableType) hidden.asType())) {
return false;
}
}
Element containingHider = containingClass(hider);
Element containingHidden = containingClass(hidden);
if (containingHider == null || containingHidden == null) {
return false;
}
if (!types.isSubtype(containingHider.asType(), containingHidden.asType())) {
return false;
}
return true;
}
private static @Nullable Element containingClass(TurbineElement element) {
Element enclosing = element.getEnclosingElement();
if (enclosing == null) {
return null;
}
if (!isClassOrInterface(enclosing.getKind())) {
// The immediately enclosing element of a field or method is a class. For classes, annotation
// processing only deals with top-level and nested (but not local or anonymous) classes,
// so the immediately enclosing element is either an enclosing class or a package symbol.
return null;
}
return enclosing;
}
private static boolean isClassOrInterface(ElementKind kind) {
return kind.isClass() || kind.isInterface();
}
private static boolean isVisibleForHiding(TurbineElement hider, TurbineElement hidden) {
int access;
switch (hidden.sym().symKind()) {
case CLASS:
access = ((TurbineTypeElement) hidden).info().access();
break;
case FIELD:
access = ((TurbineFieldElement) hidden).info().access();
break;
case METHOD:
access = ((TurbineExecutableElement) hidden).info().access();
break;
default:
return false;
}
return isVisible(
packageSymbol(asSymbol(hider)),
packageSymbol(asSymbol(hidden)),
TurbineVisibility.fromAccess(access));
}
@Override
public boolean overrides(
ExecutableElement overrider, ExecutableElement overridden, TypeElement type) {
if (!overrider.getSimpleName().contentEquals(overridden.getSimpleName())) {
return false;
}
TypeMirror a = overrider.asType();
TypeMirror b = types.asMemberOfInternal((DeclaredType) type.asType(), overridden);
if (b == null) {
return false;
}
if (!types.isSubsignature((TurbineExecutableType) a, (TurbineExecutableType) b)) {
return false;
}
return isVisible(
packageSymbol(asSymbol(overrider)),
packageSymbol(asSymbol(overridden)),
TurbineVisibility.fromAccess(((TurbineExecutableElement) overridden).info().access()));
}
@Override
public String getConstantExpression(Object value) {
if (value instanceof Byte) {
return new Const.ByteValue((Byte) value).toString();
}
if (value instanceof Long) {
return new Const.LongValue((Long) value).toString();
}
if (value instanceof Float) {
return new Const.FloatValue((Float) value).toString();
}
if (value instanceof Double) {
return new Const.DoubleValue((Double) value).toString();
}
if (value instanceof Short) {
// Special-case short for consistency with javac, see:
// https://bugs.openjdk.java.net/browse/JDK-8227617
return String.format("(short)%d", (Short) value);
}
if (value instanceof String) {
return new Const.StringValue((String) value).toString();
}
if (value instanceof Character) {
return new Const.CharValue((Character) value).toString();
}
return String.valueOf(value);
}
@Override
public void printElements(Writer w, Element... elements) {
PrintWriter pw = new PrintWriter(w, true);
for (Element element : elements) {
pw.println(element.toString());
}
}
@Override
public Name getName(CharSequence cs) {
return new TurbineName(cs.toString());
}
@Override
public boolean isFunctionalInterface(TypeElement type) {
throw new UnsupportedOperationException();
}
}
|