Cleanup (deprecated API; unneeded qualifiers; typos; formatting)

GitOrigin-RevId: 6a8febf6fcc170bea7b35b4e167ebdcd18128b69
This commit is contained in:
Roman Shevchenko
2024-02-26 20:04:45 +01:00
committed by intellij-monorepo-bot
parent 73c3190027
commit a3247d238c
2 changed files with 63 additions and 76 deletions

View File

@@ -1,7 +1,6 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.compiler.notNullVerification;
import com.intellij.compiler.instrumentation.FailSafeClassReader;
import com.intellij.compiler.instrumentation.FailSafeMethodVisitor;
import org.jetbrains.org.objectweb.asm.*;
@@ -9,7 +8,9 @@ import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
public final class NotNullVerifyingInstrumenter extends ClassVisitor implements Opcodes {
import static org.jetbrains.org.objectweb.asm.Opcodes.*;
public final class NotNullVerifyingInstrumenter extends ClassVisitor {
private static final String IAE_CLASS_NAME = "java/lang/IllegalArgumentException";
private static final String ISE_CLASS_NAME = "java/lang/IllegalStateException";
@@ -24,7 +25,7 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
private final AuxiliaryMethodGenerator myAuxGenerator;
private NotNullVerifyingInstrumenter(ClassVisitor classVisitor, ClassReader reader, String[] notNullAnnotations) {
super(Opcodes.API_VERSION, classVisitor);
super(API_VERSION, classVisitor);
Set<String> annoSet = new HashSet<>();
for (String annotation : notNullAnnotations) {
annoSet.add('L' + annotation.replace('.', '/') + ';');
@@ -33,14 +34,6 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
myAuxGenerator = new AuxiliaryMethodGenerator(reader);
}
/**
* @deprecated use {@link NotNullVerifyingInstrumenter#processClassFile(ClassReader, ClassVisitor, String[])} instead
*/
@Deprecated
public static boolean processClassFile(FailSafeClassReader reader, ClassVisitor writer, String[] notNullAnnotations) {
return processClassFile((ClassReader)reader, writer, notNullAnnotations);
}
public static boolean processClassFile(ClassReader reader, ClassVisitor writer, String[] notNullAnnotations) {
NotNullVerifyingInstrumenter instrumenter = new NotNullVerifyingInstrumenter(writer, reader, notNullAnnotations);
reader.accept(instrumenter, 0);
@@ -89,7 +82,7 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
private static MethodData collectMethodData(ClassReader reader, final Set<String> notNullAnnotations) {
final MethodData result = new MethodData();
reader.accept(new ClassVisitor(Opcodes.API_VERSION) {
reader.accept(new ClassVisitor(API_VERSION) {
private boolean myEnum, myInner;
@Override
@@ -121,7 +114,7 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
}
final MethodInfo methodInfo = new MethodInfo();
methodInfo.isStable = (access & (Opcodes.ACC_FINAL | Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE)) != 0;
methodInfo.isStable = (access & (ACC_FINAL | ACC_STATIC | ACC_PRIVATE)) != 0;
methodInfo.paramAnnotationOffset = !"<init>".equals(name) ? 0 : myEnum ? 2 : myInner ? 1 : 0;
result.myMethodInfos.put(MethodData.key(name, desc), methodInfo);
@@ -180,7 +173,7 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
}
private AnnotationVisitor collectNotNullArgs(AnnotationVisitor base, final NotNullState state) {
return new AnnotationVisitor(Opcodes.API_VERSION, base) {
return new AnnotationVisitor(API_VERSION, base) {
@Override
public void visit(String methodName, Object o) {
if (ANNOTATION_DEFAULT_METHOD.equals(methodName) && !((String) o).isEmpty()) {
@@ -258,14 +251,14 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
@Override
public MethodVisitor visitMethod(int access, final String name, final String desc, String signature, String[] exceptions) {
final MethodInfo info = myMethodData.myMethodInfos.get(MethodData.key(name, desc));
if ((access & Opcodes.ACC_BRIDGE) != 0 || info == null) {
return new FailSafeMethodVisitor(Opcodes.API_VERSION, super.visitMethod(access, name, desc, signature, exceptions));
if ((access & ACC_BRIDGE) != 0 || info == null) {
return new FailSafeMethodVisitor(API_VERSION, super.visitMethod(access, name, desc, signature, exceptions));
}
final boolean isStatic = isStatic(access);
final Type[] args = Type.getArgumentTypes(desc);
final NotNullInstructionTracker instrTracker = new NotNullInstructionTracker(cv.visitMethod(access, name, desc, signature, exceptions));
return new FailSafeMethodVisitor(Opcodes.API_VERSION, instrTracker) {
return new FailSafeMethodVisitor(API_VERSION, instrTracker) {
private Label myStartGeneratedCodeLabel;
@Override
@@ -275,7 +268,7 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
iterator.remove();
}
}
if (info.paramNullability.size() > 0) {
if (!info.paramNullability.isEmpty()) {
myStartGeneratedCodeLabel = new Label();
mv.visitLabel(myStartGeneratedCodeLabel);
}
@@ -329,6 +322,7 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
}
@Override
@SuppressWarnings("SpellCheckingInspection")
public void visitMaxs(int maxStack, int maxLocals) {
try {
super.visitMaxs(maxStack, maxLocals);
@@ -387,7 +381,7 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
private boolean myCanBeNull = true; // initially assume the value can be null
NotNullInstructionTracker(MethodVisitor delegate) {
super(Opcodes.API_VERSION, delegate);
super(API_VERSION, delegate);
}
public boolean canBeNull() {
@@ -426,7 +420,7 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
@Override
public void visitInvokeDynamicInsn(String name, String descriptor, Handle bootstrapMethodHandle, Object... bootstrapMethodArguments) {
myCanBeNull = nextCanBeNullValue(Opcodes.INVOKEDYNAMIC);
myCanBeNull = nextCanBeNullValue(INVOKEDYNAMIC);
super.visitInvokeDynamicInsn(name, descriptor, bootstrapMethodHandle, bootstrapMethodArguments);
}
@@ -438,31 +432,31 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
@Override
public void visitLdcInsn(Object value) {
myCanBeNull = nextCanBeNullValue(Opcodes.LDC);
myCanBeNull = nextCanBeNullValue(LDC);
super.visitLdcInsn(value);
}
@Override
public void visitIincInsn(int var, int increment) {
myCanBeNull = nextCanBeNullValue(Opcodes.IINC);
myCanBeNull = nextCanBeNullValue(IINC);
super.visitIincInsn(var, increment);
}
@Override
public void visitTableSwitchInsn(int min, int max, Label dflt, Label... labels) {
myCanBeNull = nextCanBeNullValue(Opcodes.TABLESWITCH);
super.visitTableSwitchInsn(min, max, dflt, labels);
public void visitTableSwitchInsn(int min, int max, Label defaultLabel, Label... labels) {
myCanBeNull = nextCanBeNullValue(TABLESWITCH);
super.visitTableSwitchInsn(min, max, defaultLabel, labels);
}
@Override
public void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels) {
myCanBeNull = nextCanBeNullValue(Opcodes.LOOKUPSWITCH);
super.visitLookupSwitchInsn(dflt, keys, labels);
public void visitLookupSwitchInsn(Label defaultLabel, int[] keys, Label[] labels) {
myCanBeNull = nextCanBeNullValue(LOOKUPSWITCH);
super.visitLookupSwitchInsn(defaultLabel, keys, labels);
}
@Override
public void visitMultiANewArrayInsn(String descriptor, int numDimensions) {
myCanBeNull = nextCanBeNullValue(Opcodes.MULTIANEWARRAY);
myCanBeNull = nextCanBeNullValue(MULTIANEWARRAY);
super.visitMultiANewArrayInsn(descriptor, numDimensions);
}
@@ -473,11 +467,11 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
}
private boolean nextCanBeNullValue(int nextMethodCallOpcode, String owner, String name, String descriptor) {
if (nextMethodCallOpcode == Opcodes.INVOKESPECIAL && ("<init>".equals(name) || myMethodData.isAlwaysNotNull(owner, name, descriptor))) {
if (nextMethodCallOpcode == INVOKESPECIAL && ("<init>".equals(name) || myMethodData.isAlwaysNotNull(owner, name, descriptor))) {
// a constructor call or a NotNull marked own method
return false;
}
if ((nextMethodCallOpcode == Opcodes.INVOKESTATIC || nextMethodCallOpcode == Opcodes.INVOKEVIRTUAL) &&
if ((nextMethodCallOpcode == INVOKESTATIC || nextMethodCallOpcode == INVOKEVIRTUAL) &&
myMethodData.isAlwaysNotNull(owner, name, descriptor)) {
return false;
}
@@ -486,19 +480,18 @@ public final class NotNullVerifyingInstrumenter extends ClassVisitor implements
private boolean nextCanBeNullValue(int nextOpcode) {
// if instruction guaranteed produces non-null stack value
if (nextOpcode == Opcodes.LDC || nextOpcode == NEW ||
nextOpcode == ANEWARRAY || nextOpcode == Opcodes.NEWARRAY || nextOpcode == Opcodes.MULTIANEWARRAY) {
if (nextOpcode == LDC || nextOpcode == NEW || nextOpcode == ANEWARRAY || nextOpcode == NEWARRAY || nextOpcode == MULTIANEWARRAY) {
return false;
}
// for some instructions it is safe not to change previously calculated flag value
if (nextOpcode == Opcodes.DUP || nextOpcode == Opcodes.DUP_X1 || nextOpcode == Opcodes.DUP_X2 ||
nextOpcode == Opcodes.DUP2 || nextOpcode == Opcodes.DUP2_X1 || nextOpcode == Opcodes.DUP2_X2 ||
nextOpcode == Opcodes.JSR || nextOpcode == Opcodes.GOTO || nextOpcode == Opcodes.NOP ||
nextOpcode == Opcodes.RET || nextOpcode == Opcodes.CHECKCAST) {
// for some instructions, it is safe not to change the previously calculated flag value
if (nextOpcode == DUP || nextOpcode == DUP_X1 || nextOpcode == DUP_X2 ||
nextOpcode == DUP2 || nextOpcode == DUP2_X1 || nextOpcode == DUP2_X2 ||
nextOpcode == JSR || nextOpcode == GOTO || nextOpcode == NOP ||
nextOpcode == RET || nextOpcode == CHECKCAST) {
return myCanBeNull;
}
// by default assume nullable
return true;
}
}
}
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.ant;
import com.intellij.compiler.instrumentation.FailSafeClassReader;
@@ -20,16 +20,17 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
@SuppressWarnings("IOStreamConstructor")
public class Javac2 extends Javac {
public static final String PROPERTY_INSTRUMENTATION_INCLUDE_JAVA_RUNTIME = "javac2.instrumentation.includeJavaRuntime";
private ArrayList<File> myFormFiles;
private List<PrefixedPath> myNestedFormPathList;
private boolean instrumentNotNull = true;
private String myNotNullAnnotations = "org.jetbrains.annotations.NotNull";
private final List<Regexp> myClassFilterAnnotationRegexpList = new ArrayList<Regexp>(0);
private final List<Regexp> myClassFilterAnnotationRegexpList = new ArrayList<>(0);
public Javac2() {
}
public Javac2() { }
/**
* Check if Java classes should be actually compiled by the task. This method is overridden by
@@ -58,6 +59,7 @@ public class Javac2 extends Javac {
return instrumentNotNull;
}
@SuppressWarnings("unused")
public void setInstrumentNotNull(boolean instrumentNotNull) {
this.instrumentNotNull = instrumentNotNull;
}
@@ -72,6 +74,7 @@ public class Javac2 extends Javac {
/**
* @param notNullAnnotations semicolon-separated names of not-null annotations to be instrumented. Example: {@code "org.jetbrains.annotations.NotNull;javax.annotation.Nonnull"}
*/
@SuppressWarnings("unused")
public void setNotNullAnnotations(String notNullAnnotations) {
myNotNullAnnotations = notNullAnnotations;
}
@@ -201,7 +204,8 @@ public class Javac2 extends Javac {
* compilation.
* @param nestedformdirs a list of {@link PrefixedPath}
*/
public void setNestedformdirs(List nestedformdirs) {
@SuppressWarnings({"unused", "SpellCheckingInspection"})
public void setNestedformdirs(List<PrefixedPath> nestedformdirs) {
myNestedFormPathList = nestedformdirs;
}
@@ -210,7 +214,8 @@ public class Javac2 extends Javac {
* compilation.
* @return the extension directories as a list of {@link PrefixedPath}
*/
public List getNestedformdirs() {
@SuppressWarnings({"unused", "SpellCheckingInspection"})
public List<PrefixedPath> getNestedformdirs() {
return myNestedFormPathList;
}
@@ -218,17 +223,16 @@ public class Javac2 extends Javac {
* Adds a path to nested form directories.
* @return a path to be configured
*/
@SuppressWarnings({"unused", "SpellCheckingInspection"})
public PrefixedPath createNestedformdirs() {
PrefixedPath p = new PrefixedPath(getProject());
if (myNestedFormPathList == null) {
myNestedFormPathList = new ArrayList<PrefixedPath>();
myNestedFormPathList = new ArrayList<>();
}
myNestedFormPathList.add(p);
return p;
}
/**
* The overridden compile method that does not actually compiles java sources but only instruments
* class files.
@@ -272,7 +276,7 @@ public class Javac2 extends Javac {
return;
}
final HashMap<String, File> class2form = new HashMap<String, File>();
final HashMap<String, File> class2form = new HashMap<>();
for (File formFile : formsToInstrument) {
@@ -318,13 +322,9 @@ public class Javac2 extends Javac {
try {
int version;
InputStream stream = new FileInputStream(classFile);
try {
try (InputStream stream = new FileInputStream(classFile)) {
version = InstrumenterClassWriter.getClassFileVersion(new ClassReader(stream));
}
finally {
stream.close();
}
AntNestedFormLoader formLoader = new AntNestedFormLoader(finder.getLoader(), myNestedFormPathList);
InstrumenterClassWriter classWriter = new InstrumenterClassWriter(InstrumenterClassWriter.getAsmClassWriterFlags(version), finder);
final AsmCodeGenerator codeGenerator = new AsmCodeGenerator(rootContainer, finder, formLoader, false, classWriter);
@@ -347,7 +347,7 @@ public class Javac2 extends Javac {
}
}
catch (Exception e) {
fireError("Forms instrumentation failed for " + formFile.getAbsolutePath() + ": " + e.toString());
fireError("Forms instrumentation failed for " + formFile.getAbsolutePath() + ": " + e);
}
}
}
@@ -424,7 +424,7 @@ public class Javac2 extends Javac {
* @param p the path to append
*/
private static void appendPath(Path cp, final Path p) {
if (p != null && p.size() > 0) {
if (p != null && !p.isEmpty()) {
cp.append(p);
}
}
@@ -434,47 +434,41 @@ public class Javac2 extends Javac {
*
* @param dir the directory with classes to instrument (the directory is processed recursively)
* @param finder the classloader to use
* @return the amount of classes actually affected by instrumentation
* @return the number of classes actually affected by instrumentation
*/
private int instrumentNotNull(File dir, final InstrumentationClassFinder finder) {
int instrumented = 0;
final File[] files = dir.listFiles();
File[] files = dir.listFiles();
if (files == null) return 0;
for (File file : files) {
final String name = file.getName();
if (name.endsWith(".class")) {
final String path = file.getPath();
log("Adding @NotNull assertions to " + path, Project.MSG_VERBOSE);
try {
final FileInputStream inputStream = new FileInputStream(file);
try {
FailSafeClassReader reader = new FailSafeClassReader(inputStream);
try (FileInputStream inputStream = new FileInputStream(file)) {
ClassReader reader = new FailSafeClassReader(inputStream);
int version = InstrumenterClassWriter.getClassFileVersion(reader);
if ((version & 0xFFFF) >= Opcodes.V1_5 && !shouldBeSkippedByAnnotationPattern(reader)) {
ClassWriter writer = new InstrumenterClassWriter(reader, InstrumenterClassWriter.getAsmClassWriterFlags(version), finder);
if (NotNullVerifyingInstrumenter.processClassFile(reader, writer, myNotNullAnnotations.split(";"))) {
final FileOutputStream fileOutputStream = new FileOutputStream(path);
try {
if (NotNullVerifyingInstrumenter.processClassFile(reader, writer, getNotNullAnnotations().split(";"))) {
try (FileOutputStream fileOutputStream = new FileOutputStream(path)) {
fileOutputStream.write(writer.toByteArray());
instrumented++;
}
finally {
fileOutputStream.close();
}
}
}
}
finally {
inputStream.close();
}
}
catch (IOException e) {
log("Failed to instrument @NotNull assertion for " + path + ": " + e.getMessage(), Project.MSG_WARN);
}
catch (Exception e) {
fireError("@NotNull instrumentation failed for " + path + ": " + e.toString());
fireError("@NotNull instrumentation failed for " + path + ": " + e);
}
}
else if (file.isDirectory()) {
@@ -536,7 +530,7 @@ public class Javac2 extends Javac {
@Override
protected void resetFileLists() {
super.resetFileLists();
myFormFiles = new ArrayList<File>();
myFormFiles = new ArrayList<>();
}
@Override
@@ -551,7 +545,7 @@ public class Javac2 extends Javac {
}
private static InstrumentationClassFinder createInstrumentationClassFinder(final String classPath, boolean shouldIncludeJavaRuntime) throws MalformedURLException {
final ArrayList<URL> urls = new ArrayList<URL>();
final ArrayList<URL> urls = new ArrayList<>();
if (shouldIncludeJavaRuntime) {
final URL jrt = tryGetJrtURL();
if (jrt != null) {
@@ -569,9 +563,9 @@ public class Javac2 extends Javac {
private class AntNestedFormLoader implements NestedFormLoader {
private final ClassLoader myLoader;
private final List<PrefixedPath> myNestedFormPathList;
private final HashMap<String, LwRootContainer> myFormCache = new HashMap<String, LwRootContainer>();
private final HashMap<String, LwRootContainer> myFormCache = new HashMap<>();
AntNestedFormLoader(final ClassLoader loader, List nestedFormPathList) {
AntNestedFormLoader(final ClassLoader loader, List<PrefixedPath> nestedFormPathList) {
myLoader = loader;
myNestedFormPathList = nestedFormPathList;
}