All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.youshallnotpass.inspections.nullfree.nulls.JavaNulls Maven / Gradle / Ivy

The newest version!
package dev.youshallnotpass.inspections.nullfree.nulls;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.expr.NullLiteralExpr;
import dev.youshallnotpass.inspection.InspectionException;
import dev.youshallnotpass.inspection.InspectionScalar;
import dev.youshallnotpass.inspection.JavaViolations;
import dev.youshallnotpass.inspection.Violations;
import dev.youshallnotpass.javaparser.NodeDescription;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public final class JavaNulls implements Violations {

    private final Violations nulls;

    public JavaNulls(final String... lines) {
        this(ParserConfiguration.LanguageLevel.RAW, lines);
    }

    public JavaNulls(final ParserConfiguration.LanguageLevel level, final String... lines) {
        this(
            new JavaParser(new ParserConfiguration().setLanguageLevel(level)),
            lines
        );
    }

    public JavaNulls(final JavaParser parser, final String... lines) {
        this(
            parser,
            new InspectionScalar<>(() -> {
                final StringBuilder source = new StringBuilder();
                for (final String line : lines) {
                    source.append(line);
                }
                return new ByteArrayInputStream(
                    source.toString().getBytes(StandardCharsets.UTF_8)
                );
            }),
            String.join("", lines)
        );
    }

    public JavaNulls(final String source) {
        this(ParserConfiguration.LanguageLevel.RAW, source);
    }

    public JavaNulls(final ParserConfiguration.LanguageLevel level, final String source) {
        this(
            new JavaParser(new ParserConfiguration().setLanguageLevel(level)),
            source
        );
    }

    public JavaNulls(final JavaParser parser, final String source) {
        this(
            parser,
            new InspectionScalar<>(() -> {
                return new ByteArrayInputStream(
                    source.getBytes(StandardCharsets.UTF_8)
                );
            }),
            source
        );
    }

    public JavaNulls(final File file) {
        this(ParserConfiguration.LanguageLevel.RAW, file);
    }

    public JavaNulls(final ParserConfiguration.LanguageLevel level, final File file) {
        this(
            new JavaParser(new ParserConfiguration().setLanguageLevel(level)),
            file
        );
    }

    public JavaNulls(final JavaParser parser, final File file) {
        this(
            parser,
            new InspectionScalar<>(() -> {
                try {
                    return new FileInputStream(file);
                } catch (final IOException e) {
                    throw new InspectionException(
                        String.format(
                            "Can not get an input stream from the file: %s",
                            file
                        ),
                        e
                    );
                }
            }),
            file.getAbsolutePath()
        );
    }

    public JavaNulls(
        final JavaParser parser,
        final InspectionScalar source,
        final String descriptor
    ) {
        this(
            new JavaViolations<>(
                source,
                parser,
                descriptor,
                (final CompilationUnit unit, final TypeDeclaration root) -> {
                    final List result = new ArrayList<>();
                    final List nulls = unit.findAll(
                        NullLiteralExpr.class
                    );
                    for (final NullLiteralExpr aNull : nulls) {
                        result.add(
                            new JavaNull(
                                aNull,
                                new NodeDescription(aNull, aNull, root)
                            )
                        );
                    }
                    return result;
                }
            )
        );
    }

    public JavaNulls(final Violations nulls) {
        this.nulls = nulls;
    }

    @Override
    public List asList() throws InspectionException {
        return nulls.asList();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy