Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8265900: Use pattern matching for instanceof at module jdk.compiler(p…
…art 2)

Reviewed-by: mcimadamore
  • Loading branch information
lgxbslgx authored and mcimadamore committed Apr 26, 2021
1 parent 851b219 commit 082abbd
Show file tree
Hide file tree
Showing 29 changed files with 204 additions and 276 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -168,8 +168,8 @@ private Attribute.Compound[] unpackContained(Attribute.Compound container) {
ListBuffer<Attribute.Compound> compounds = new ListBuffer<>();
if (contained0 != null) {
for (Attribute a : contained0)
if (a instanceof Attribute.Compound)
compounds = compounds.append((Attribute.Compound)a);
if (a instanceof Attribute.Compound attributeCompound)
compounds = compounds.append(attributeCompound);
}
return compounds.toArray(new Attribute.Compound[compounds.size()]);
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -87,8 +87,8 @@ public Object getValue() {
}
@DefinedBy(Api.LANGUAGE_MODEL)
public <R, P> R accept(AnnotationValueVisitor<R, P> v, P p) {
if (value instanceof String)
return v.visitString((String) value, p);
if (value instanceof String str)
return v.visitString(str, p);
if (value instanceof Integer) {
int i = (Integer) value;
switch (type.getTag()) {
Expand Down Expand Up @@ -198,12 +198,10 @@ private Compound getFirstEmbeddedTC() {
if (values.size() == 1) {
Pair<MethodSymbol, Attribute> val = values.get(0);
if (val.fst.getSimpleName().contentEquals("value")
&& val.snd instanceof Array) {
Array arr = (Array) val.snd;
if (arr.values.length != 0
&& arr.values[0] instanceof Attribute.TypeCompound)
return (Attribute.TypeCompound) arr.values[0];
}
&& val.snd instanceof Array arr
&& arr.values.length != 0
&& arr.values[0] instanceof Attribute.TypeCompound compound)
return compound;
}
return null;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -209,12 +209,11 @@ protected ClassFinder(Context context) {
// Temporary, until more info is available from the module system.
boolean useCtProps;
JavaFileManager fm = context.get(JavaFileManager.class);
if (fm instanceof DelegatingJavaFileManager) {
fm = ((DelegatingJavaFileManager) fm).getBaseFileManager();
if (fm instanceof DelegatingJavaFileManager delegatingJavaFileManager) {
fm = delegatingJavaFileManager.getBaseFileManager();
}
if (fm instanceof JavacFileManager) {
JavacFileManager jfm = (JavacFileManager) fm;
useCtProps = jfm.isDefaultBootClassPath() && jfm.isSymbolFileEnabled();
if (fm instanceof JavacFileManager javacFileManager) {
useCtProps = javacFileManager.isDefaultBootClassPath() && javacFileManager.isSymbolFileEnabled();
} else if (fm.getClass().getName().equals("com.sun.tools.sjavac.comp.SmartFileManager")) {
useCtProps = !options.isSet("ignore.symbol.file");
} else {
Expand Down Expand Up @@ -642,27 +641,26 @@ private void scanUserPaths(PackageSymbol p, boolean includeSourcePath) throws IO

if (verbose && verbosePath) {
verbosePath = false; // print once per compile
if (fileManager instanceof StandardJavaFileManager) {
StandardJavaFileManager fm = (StandardJavaFileManager)fileManager;
if (fileManager instanceof StandardJavaFileManager standardJavaFileManager) {
if (haveSourcePath && wantSourceFiles) {
List<Path> path = List.nil();
for (Path sourcePath : fm.getLocationAsPaths(SOURCE_PATH)) {
for (Path sourcePath : standardJavaFileManager.getLocationAsPaths(SOURCE_PATH)) {
path = path.prepend(sourcePath);
}
log.printVerbose("sourcepath", path.reverse().toString());
} else if (wantSourceFiles) {
List<Path> path = List.nil();
for (Path classPath : fm.getLocationAsPaths(CLASS_PATH)) {
for (Path classPath : standardJavaFileManager.getLocationAsPaths(CLASS_PATH)) {
path = path.prepend(classPath);
}
log.printVerbose("sourcepath", path.reverse().toString());
}
if (wantClassFiles) {
List<Path> path = List.nil();
for (Path platformPath : fm.getLocationAsPaths(PLATFORM_CLASS_PATH)) {
for (Path platformPath : standardJavaFileManager.getLocationAsPaths(PLATFORM_CLASS_PATH)) {
path = path.prepend(platformPath);
}
for (Path classPath : fm.getLocationAsPaths(CLASS_PATH)) {
for (Path classPath : standardJavaFileManager.getLocationAsPaths(CLASS_PATH)) {
path = path.prepend(classPath);
}
log.printVerbose("classpath", path.reverse().toString());
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -263,11 +263,9 @@ public <R, P> R accept(DirectiveVisitor<R, P> v, P p) {
// TODO: delete?
@Override
public boolean equals(Object obj) {
if (!(obj instanceof ProvidesDirective)) {
return false;
}
ProvidesDirective other = (ProvidesDirective)obj;
return service == other.service && impls.equals(other.impls);
return (obj instanceof ProvidesDirective directive)
&& service == directive.service
&& impls.equals(directive.impls);
}

// TODO: delete?
Expand Down Expand Up @@ -359,11 +357,8 @@ public <R, P> R accept(DirectiveVisitor<R, P> v, P p) {
// TODO: delete?
@Override
public boolean equals(Object obj) {
if (!(obj instanceof UsesDirective)) {
return false;
}
UsesDirective other = (UsesDirective)obj;
return service == other.service;
return (obj instanceof UsesDirective directive)
&& service == directive.service;
}

// TODO: delete?
Expand Down
Expand Up @@ -753,8 +753,9 @@ public void finalizeScope() {
}

protected Scope finalizeSingleScope(Scope impScope) {
if (impScope instanceof FilterImportScope && impScope.owner.kind == Kind.TYP &&
((FilterImportScope) impScope).isStaticallyImported()) {
if (impScope instanceof FilterImportScope filterImportScope
&& impScope.owner.kind == Kind.TYP
&& filterImportScope.isStaticallyImported()) {
WriteableScope finalized = WriteableScope.create(impScope.owner);

for (Symbol sym : impScope.getSymbols()) {
Expand Down
46 changes: 21 additions & 25 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/code/Symbol.java
Expand Up @@ -1420,13 +1420,12 @@ public void complete() throws CompletionFailure {
@DefinedBy(Api.LANGUAGE_MODEL)
public List<Type> getInterfaces() {
apiComplete();
if (type instanceof ClassType) {
ClassType t = (ClassType)type;
if (t.interfaces_field == null) // FIXME: shouldn't be null
t.interfaces_field = List.nil();
if (t.all_interfaces_field != null)
return Type.getModelTypes(t.all_interfaces_field);
return t.interfaces_field;
if (type instanceof ClassType classType) {
if (classType.interfaces_field == null) // FIXME: shouldn't be null
classType.interfaces_field = List.nil();
if (classType.all_interfaces_field != null)
return Type.getModelTypes(classType.all_interfaces_field);
return classType.interfaces_field;
} else {
return List.nil();
}
Expand All @@ -1435,14 +1434,13 @@ public List<Type> getInterfaces() {
@DefinedBy(Api.LANGUAGE_MODEL)
public Type getSuperclass() {
apiComplete();
if (type instanceof ClassType) {
ClassType t = (ClassType)type;
if (t.supertype_field == null) // FIXME: shouldn't be null
t.supertype_field = Type.noType;
if (type instanceof ClassType classType) {
if (classType.supertype_field == null) // FIXME: shouldn't be null
classType.supertype_field = Type.noType;
// An interface has no superclass; its supertype is Object.
return t.isInterface()
return classType.isInterface()
? Type.noType
: t.supertype_field.getModelType();
: classType.supertype_field.getModelType();
} else {
return Type.noType;
}
Expand Down Expand Up @@ -1585,15 +1583,14 @@ public void reset() {
erasure_field = null;
members_field = null;
flags_field = 0;
if (type instanceof ClassType) {
ClassType t = (ClassType)type;
t.setEnclosingType(Type.noType);
t.rank_field = -1;
t.typarams_field = null;
t.allparams_field = null;
t.supertype_field = null;
t.interfaces_field = null;
t.all_interfaces_field = null;
if (type instanceof ClassType classType) {
classType.setEnclosingType(Type.noType);
classType.rank_field = -1;
classType.typarams_field = null;
classType.allparams_field = null;
classType.supertype_field = null;
classType.interfaces_field = null;
classType.all_interfaces_field = null;
}
clearAnnotationMetadata();
}
Expand Down Expand Up @@ -1754,13 +1751,12 @@ public Object getConstValue() {
if (data == ElementKind.EXCEPTION_PARAMETER ||
data == ElementKind.RESOURCE_VARIABLE) {
return null;
} else if (data instanceof Callable<?>) {
} else if (data instanceof Callable<?> callableData) {
// In this case, this is a final variable, with an as
// yet unevaluated initializer.
Callable<?> eval = (Callable<?>)data;
data = null; // to make sure we don't evaluate this twice.
try {
data = eval.call();
data = callableData.call();
} catch (Exception ex) {
throw new AssertionError(ex);
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -276,8 +276,7 @@ public void removeDeclarationMetadata(Attribute.Compound compound) {
if (attrCompound.isSynthesized() && !attrCompound.values.isEmpty()) {
Pair<Symbol.MethodSymbol, Attribute> val = attrCompound.values.get(0);
if (val.fst.getSimpleName().contentEquals("value") &&
val.snd instanceof Attribute.Array) {
Attribute.Array arr = (Attribute.Array) val.snd;
val.snd instanceof Attribute.Array arr) {
if (arr.values.length != 0
&& arr.values[0] instanceof Attribute.Compound
&& arr.values[0].type == compound.type) {
Expand Down
Expand Up @@ -1359,13 +1359,8 @@ public String toString() {

@Override @DefinedBy(Api.LANGUAGE_MODEL)
public boolean equals(Object obj) {
if (obj instanceof ArrayType) {
ArrayType that = (ArrayType)obj;
return this == that ||
elemtype.equals(that.elemtype);
}

return false;
return (obj instanceof ArrayType arrayType)
&& (this == arrayType || elemtype.equals(arrayType.elemtype));
}

@DefinedBy(Api.LANGUAGE_MODEL)
Expand Down
Expand Up @@ -103,11 +103,9 @@ public String toString() {

@Override
public boolean equals(Object other) {
if (! (other instanceof TypePathEntry)) {
return false;
}
TypePathEntry tpe = (TypePathEntry) other;
return this.tag == tpe.tag && this.arg == tpe.arg;
return (other instanceof TypePathEntry entry)
&& this.tag == entry.tag
&& this.arg == entry.arg;
}

@Override
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -155,11 +155,11 @@ public List<Attribute> annotationTargets(TypeSymbol tsym) {
}

Attribute atValue = atTarget.member(names.value);
if (!(atValue instanceof Attribute.Array)) {
if (!(atValue instanceof Attribute.Array arrayVal)) {
return null;
}

List<Attribute> targets = ((Array)atValue).getValue();
List<Attribute> targets = arrayVal.getValue();
if (targets.stream().anyMatch(a -> !(a instanceof Attribute.Enum))) {
return null;
}
Expand Down
25 changes: 10 additions & 15 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java
Expand Up @@ -3161,12 +3161,9 @@ class Entry {

@Override
public boolean equals(Object obj) {
if (obj instanceof Entry) {
Entry e = (Entry)obj;
return e.msym == msym && isSameType(site, e.site);
} else {
return false;
}
return (obj instanceof Entry entry)
&& entry.msym == msym
&& isSameType(site, entry.site);
}

@Override
Expand Down Expand Up @@ -3850,11 +3847,9 @@ public int hashCode() {
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof TypePair))
return false;
TypePair typePair = (TypePair)obj;
return isSameType(t1, typePair.t1)
&& isSameType(t2, typePair.t2);
return (obj instanceof TypePair typePair)
&& isSameType(t1, typePair.t1)
&& isSameType(t2, typePair.t2);
}
}
Set<TypePair> mergeCache = new HashSet<>();
Expand Down Expand Up @@ -4877,8 +4872,8 @@ public int hashCode() {
}

public boolean equals(Object obj) {
return (obj instanceof UniqueType) &&
types.isSameType(type, ((UniqueType)obj).type);
return (obj instanceof UniqueType uniqueType) &&
types.isSameType(type, uniqueType.type);
}

public String toString() {
Expand Down Expand Up @@ -5032,8 +5027,8 @@ public RetentionPolicy getRetention(TypeSymbol sym) {
Attribute.Compound c = sym.attribute(syms.retentionType.tsym);
if (c != null) {
Attribute value = c.member(names.value);
if (value != null && value instanceof Attribute.Enum) {
Name levelName = ((Attribute.Enum)value).value.name;
if (value != null && value instanceof Attribute.Enum attributeEnum) {
Name levelName = attributeEnum.value.name;
if (levelName == names.SOURCE) vis = RetentionPolicy.SOURCE;
else if (levelName == names.CLASS) vis = RetentionPolicy.CLASS;
else if (levelName == names.RUNTIME) vis = RetentionPolicy.RUNTIME;
Expand Down

1 comment on commit 082abbd

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.