Skip to content

Commit

Permalink
Merge pull request apache#3208 from mbien/hints_spi_cleanup
Browse files Browse the repository at this point in the history
[jackpot] Hints SPI code cleanup / warning removal
  • Loading branch information
matthiasblaesing authored Oct 20, 2021
2 parents 0b2ad13 + 7c09388 commit 0c8e413
Show file tree
Hide file tree
Showing 43 changed files with 468 additions and 495 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.concurrent.atomic.AtomicBoolean;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.java.source.CompilationInfo;
import org.netbeans.modules.java.hints.spiimpl.MessageImpl;
import org.netbeans.modules.java.hints.spiimpl.hints.HintsInvoker;
import org.netbeans.modules.java.hints.spiimpl.options.HintsSettings;
import org.netbeans.spi.editor.hints.ErrorDescription;
Expand All @@ -40,7 +39,7 @@ public class HintsRunner {

@CheckForNull
public static Map<HintDescription, List<ErrorDescription>> computeErrors(CompilationInfo info, Iterable<? extends HintDescription> hints, AtomicBoolean cancel) {
return new HintsInvoker(HintsSettings.getSettingsFor(info.getFileObject()), cancel).computeHints(info, new TreePath(info.getCompilationUnit()), hints, new LinkedList<MessageImpl>());
return new HintsInvoker(HintsSettings.getSettingsFor(info.getFileObject()), cancel).computeHints(info, new TreePath(info.getCompilationUnit()), hints, new LinkedList<>());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public abstract class PatternConvertor {
protected abstract @CheckForNull Iterable<? extends HintDescription> parseString(@NonNull String code);

public static @CheckForNull Iterable<? extends HintDescription> create(@NonNull String code) {
Collection<String> patterns = new ArrayList<String>();
Collection<String> patterns = new ArrayList<>();

//XXX:
if (code.contains(";;")) {
Expand All @@ -64,7 +64,7 @@ public abstract class PatternConvertor {
patterns.add(code);
}

Collection<HintDescription> result = new ArrayList<HintDescription>(patterns.size());
Collection<HintDescription> result = new ArrayList<>(patterns.size());

for (String pattern : patterns) {
PatternDescription pd = PatternDescription.create(pattern, Collections.<String, String>emptyMap());
Expand All @@ -83,6 +83,7 @@ public abstract class PatternConvertor {

private static final class WorkerImpl implements Worker {

@Override
public Collection<? extends ErrorDescription> createErrors(HintContext ctx) {
ErrorDescription ed = ErrorDescriptionFactory.forTree(ctx, ctx.getPath(), "Found pattern occurrence");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.netbeans.modules.java.hints.providers.code;

import com.sun.source.tree.Tree.Kind;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
Expand Down Expand Up @@ -54,7 +53,6 @@
import org.netbeans.modules.java.hints.providers.spi.Trigger.Kinds;
import org.netbeans.modules.java.hints.providers.spi.Trigger.PatternDescription;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.Severity;
import org.netbeans.spi.java.hints.BooleanOption;
import org.netbeans.spi.java.hints.ConstraintVariableType;
import org.netbeans.spi.java.hints.Hint;
Expand All @@ -80,12 +78,13 @@ public class CodeHintProviderImpl implements HintProvider {

private static final Logger LOG = Logger.getLogger(WorkerImpl.class.getName());

@Override
public Map<HintMetadata, ? extends Collection<? extends HintDescription>> computeHints() {
return computeHints(findLoader(), "META-INF/nb-hints/hints");
}

private Map<HintMetadata, ? extends Collection<? extends HintDescription>> computeHints(ClassLoader l, String path) {
Map<HintMetadata, Collection<HintDescription>> result = new HashMap<HintMetadata, Collection<HintDescription>>();
Map<HintMetadata, Collection<HintDescription>> result = new HashMap<>();

for (ClassWrapper c : FSWrapper.listClasses()) {
try {
Expand Down Expand Up @@ -176,10 +175,10 @@ private static CustomizerProvider createCustomizerProvider(ClassWrapper clazz, M

if (useOptions == null) return null;

allowedOptions = new HashSet<String>(Arrays.asList(useOptions.value()));
allowedOptions = new HashSet<>(Arrays.asList(useOptions.value()));
}

List<OptionDescriptor> declarativeOptions = new ArrayList<OptionDescriptor>();
List<OptionDescriptor> declarativeOptions = new ArrayList<>();

for (FieldWrapper fw : clazz.getFields()) {
BooleanOption option = fw.getAnnotation(BooleanOption.class);
Expand Down Expand Up @@ -259,13 +258,12 @@ private static void processPatternHint(Map<HintMetadata, Collection<HintDescript
for (TriggerPattern pattern : patternTriggers.value()) {
processPatternHint(hints, pattern, m, metadata);
}
return ;
}
}

private static void processPatternHint(Map<HintMetadata, Collection<HintDescription>> hints, TriggerPattern patternTrigger, MethodWrapper m, HintMetadata metadata) {
String pattern = patternTrigger.value();
Map<String, String> constraints = new HashMap<String, String>();
Map<String, String> constraints = new HashMap<>();

for (ConstraintVariableType c : patternTrigger.constraints()) {
constraints.put(c.variable(), c.type());
Expand All @@ -285,7 +283,7 @@ private static void addHint(Map<HintMetadata, Collection<HintDescription>> hints
Collection<HintDescription> list = hints.get(metadata);

if (list == null) {
hints.put(metadata, list = new LinkedList<HintDescription>());
hints.put(metadata, list = new LinkedList<>());
}

list.add(hint);
Expand All @@ -302,7 +300,7 @@ public WorkerImpl(String className, String methodName) {
this.methodName = methodName;
}

private final AtomicReference<Method> methodRef = new AtomicReference<Method>();
private final AtomicReference<Method> methodRef = new AtomicReference<>();
private Set<FileObject> exceptionThrownFor;

@Override
Expand All @@ -321,7 +319,7 @@ public Collection<? extends ErrorDescription> createErrors(org.netbeans.spi.java
}

if (result instanceof Iterable) {
List<ErrorDescription> out = new LinkedList<ErrorDescription>();
List<ErrorDescription> out = new LinkedList<>();

for (ErrorDescription ed : NbCollections.iterable(NbCollections.checkedIteratorByFilter(((Iterable) result).iterator(), ErrorDescription.class, false))) {
out.add(ed);
Expand All @@ -335,13 +333,7 @@ public Collection<? extends ErrorDescription> createErrors(org.netbeans.spi.java
}

//XXX: log if result was ignored...
} catch (IllegalAccessException ex) {
Exceptions.printStackTrace(ex);
} catch (IllegalArgumentException ex) {
Exceptions.printStackTrace(ex);
} catch (ClassNotFoundException ex) {
Exceptions.printStackTrace(ex);
} catch (NoSuchMethodException ex) {
} catch (IllegalAccessException | IllegalArgumentException | ClassNotFoundException | NoSuchMethodException ex) {
Exceptions.printStackTrace(ex);
} catch (InvocationTargetException ex) {
boolean newOccurrence;
Expand Down Expand Up @@ -371,62 +363,6 @@ Method getMethod() throws NoSuchMethodException, ClassNotFoundException {

}

private static final class EmptyHintMetadataDescription implements Hint {

public String id() {
return "";
}

public String minSourceVersion() {
return "";
}

public String category() {
return "general";
}

public boolean enabled() {
return true;
}

public Severity severity() {
return Severity.VERIFIER;
}

private static final String[] EMPTY_SW = new String[0];

public String[] suppressWarnings() {
return EMPTY_SW;
}

public Class<? extends Annotation> annotationType() {
return Hint.class;
}

public Class<? extends CustomizerProvider> customizerProvider() {
return CustomizerProvider.class;
}

public Kind hintKind() {
return Kind.INSPECTION;
}

private static final Options[] EMPTY_OPTIONS = new Options[0];

public Options[] options() {
return EMPTY_OPTIONS;
}

@Override public String displayName() {
return "";
}

@Override public String description() {
return "";
}

}

private static final class DelegatingCustomizerProvider implements CustomizerProvider {

private final Class<? extends CustomizerProvider> component;
Expand All @@ -438,14 +374,8 @@ public DelegatingCustomizerProvider(Class<? extends CustomizerProvider> componen
@Override
public JComponent getCustomizer(Preferences prefs) {
try {
return component.newInstance().getCustomizer(prefs);
} catch (SecurityException ex) {
Exceptions.printStackTrace(ex);
} catch (InstantiationException ex) {
Exceptions.printStackTrace(ex);
} catch (IllegalAccessException ex) {
Exceptions.printStackTrace(ex);
} catch (IllegalArgumentException ex) {
return component.getDeclaredConstructor().newInstance().getCustomizer(prefs);
} catch (ReflectiveOperationException ex) {
Exceptions.printStackTrace(ex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static Iterable<? extends ClassWrapper> listClasses() {
loader = ClassLoader.getSystemClassLoader();
}

List<ClassWrapper> result = new LinkedList<ClassWrapper>();
List<ClassWrapper> result = new LinkedList<>();
FileObject main = FileUtil.getConfigFile("org-netbeans-modules-java-hints/code-hints/");

if (main != null) {
Expand All @@ -74,7 +74,7 @@ protected AnnotatableWrapper(ClassLoader loader, FileObject folder) {
this.folder = folder;
}

private final Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<Class<? extends Annotation>, Annotation>();
private final Map<Class<? extends Annotation>, Annotation> annotations = new HashMap<>();

public synchronized <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
if (!this.annotations.containsKey(annotationClass)) {
Expand Down Expand Up @@ -109,7 +109,7 @@ public ClassWrapper(ClassLoader loader, FileObject folder) {

public synchronized Iterable<? extends MethodWrapper> getMethods() {
if (this.methods == null) {
List<MethodWrapper> methods = new LinkedList<MethodWrapper>();
List<MethodWrapper> methods = new LinkedList<>();

for (FileObject c : folder.getChildren()) {
if (c.getExt().equals("method")) {
Expand All @@ -127,7 +127,7 @@ public synchronized Iterable<? extends MethodWrapper> getMethods() {

public synchronized Iterable<? extends FieldWrapper> getFields() {
if (this.fields == null) {
List<FieldWrapper> fields = new LinkedList<FieldWrapper>();
List<FieldWrapper> fields = new LinkedList<>();

for (FileObject c : folder.getChildren()) {
if (c.getExt().equals("field")) {
Expand Down Expand Up @@ -216,7 +216,7 @@ private static Object computeAttributeValue(ClassLoader loader, FileObject folde
result = defaulValue;
} else {
if (returnType.isArray()) {
List<Object> items = new LinkedList<Object>();
List<Object> items = new LinkedList<>();
int c = 0;

while (true) {
Expand Down Expand Up @@ -266,23 +266,25 @@ private static Object computeAttributeValue(ClassLoader loader, FileObject folde
}


@SuppressWarnings("unchecked")
private static <T extends Annotation> T loadAnnotation(ClassLoader l, FileObject annotationFolder) throws ClassNotFoundException {
Class<?> clazz = l.loadClass(annotationFolder.getName().replace('-', '.'));

return (T) Proxy.newProxyInstance(l, new Class[] {clazz}, new InvocationHandlerImpl(l, annotationFolder));
return (T) Proxy.newProxyInstance(l, new Class<?>[] {clazz}, new InvocationHandlerImpl(l, annotationFolder));
}

private static final class InvocationHandlerImpl implements InvocationHandler {

private final ClassLoader loader;
private final FileObject folder;
private final Map<String, Object> attributes = new HashMap<String, Object>();
private final Map<String, Object> attributes = new HashMap<>();

public InvocationHandlerImpl(ClassLoader loader, FileObject folder) {
this.loader = loader;
this.folder = folder;
}

@Override
public synchronized Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (!attributes.containsKey(method.getName())) {
Object result = computeAttributeValue(loader, folder, method.getName(), method.getReturnType(), method.getDefaultValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static final class AdditionalQueryConstraints {
public final Set<String> requiredErasedTypes;

public AdditionalQueryConstraints(Set<String> requiredErasedTypes) {
this.requiredErasedTypes = Collections.unmodifiableSet(new HashSet<String>(requiredErasedTypes));
this.requiredErasedTypes = Collections.unmodifiableSet(new HashSet<>(requiredErasedTypes));
}

private static final AdditionalQueryConstraints EMPTY = new AdditionalQueryConstraints(Collections.<String>emptySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static final class Builder {
private boolean enabled;
private Hint.Kind kind;
private Severity severity;
private final Collection<String> suppressWarnings = new ArrayList<String>();
private final Collection<String> suppressWarnings = new ArrayList<>();
private CustomizerProvider customizer;
private final Set<Options> options = EnumSet.noneOf(Options.class);
private SourceVersion sourceVersion;
Expand Down Expand Up @@ -210,7 +210,7 @@ public enum Options {
HEAVY;

public static Set<Options> fromHintOptions(Hint.Options... options) {
Set<Options> result = new HashSet<Options>();
Set<Options> result = new HashSet<>();

for (Hint.Options opt : options) {
result.add(valueOf(opt.name()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected final void setVersion(Document doc, V version) {
}
}

@SuppressWarnings("unchecked")
protected @CheckForNull V getUpToDateDocumentVersion(Context context, Document doc) {
V oldVersion = (V) doc.getProperty(documentKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
Expand Down Expand Up @@ -53,7 +52,7 @@ public void setOptions(String[] opts) {
}

public String[] getOptions() {
return options.isEmpty() ? NO_OPTIONS : options.toArray(new String[options.size()]);
return options.isEmpty() ? NO_OPTIONS : options.toArray(new String[0]);
}

/**Invoke the given hint's worker on the specified {@link Tree.Kind}(s).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@

import com.sun.source.tree.BlockTree;
import com.sun.source.tree.LambdaExpressionTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Scope;
import com.sun.source.tree.StatementTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
import org.netbeans.api.java.source.support.ErrorAwareTreePathScanner;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Enter;
Expand All @@ -39,7 +37,6 @@
import java.util.Collections;

import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
import com.sun.tools.javac.tree.JCTree.JCLambda;
Expand Down
Loading

0 comments on commit 0c8e413

Please sign in to comment.