autoimport

package module
v1.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2023 License: BSD-3-Clause Imports: 13 Imported by: 3

README

Autoimport

Given source code, class names can be found in available .jar files, and import statements can be generated, for Java and for Kotlin.

This currently only works for OpenJDK 8, not OpenJDK 11 and beyond.

Includes the autoimport utility for looking up packages, given the start of a class name.

Experimental functionality for organizing imports is also included.

Example use

With OpenJDK 8 installed
$ autoimport FilePe
import java.io.*; // FilePermissionCollection
import java.io.*; // FilePermission
import sun.security.tools.policytool.*; // FilePerm
import net.rubygrapefruit.platform.*; // FilePermissionException
With OpenJDK 19 and openjdk-src installed
$ autoimport -e FileSystem
import java.io.*; // FileSystem
Given a Java file without imports

Main.java:

package com.example.demo;

public class Main {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");

        Map<String, Integer> ageMapping = new HashMap<>();
        ageMapping.put("Alice", 30);
        ageMapping.put("Bob", 25);

        for (String name : names) {
            System.out.println(name + " is " + ageMapping.get(name) + " years old.");
        }
    }
}
Features and limitation
  • Searches directories of .jar files for class names.
  • Given the start of the class name, searches for the matching shortest class, and also returns the import path (like java.io.*).
  • Also searches */lib/src.zip files, if found.
  • Intended to be used for simple autocompletion of class names.
General info

Documentation

Overview

Package autoimport tries to find which import should be used, given the start of a class name

Index

Constants

This section is empty.

Variables

View Source
var KotlinTypes = []string{
	"Annotation", "Any", "Array", "Boolean", "Byte", "Char", "CharSequence",
	"Collection", "Comparable", "Double", "Enum", "Float", "Function", "Int",
	"IntrinsicConstEvaluation", "Iterable", "Iterator", "List", "ListIterator",
	"Long", "Map", "Map.Entry", "MutableCollection", "MutableIterable",
	"MutableIterator", "MutableList", "MutableListIterator", "MutableMap",
	"MutableMap.MutableEntry", "MutableSet", "Nothing", "Number", "Pair",
	"PlatformDependent", "PureReifiable", "Runnable", "Set", "Short", "String",
	"Throwable", "Triple", "Unit", "UByte", "UInt", "ULong", "UShort",
}

KotlinTypes lists classes and types that are available in Kotlin (with imports in mind)

Functions

func DeGlob added in v1.4.4

func DeGlob(imports string) []string

DeGlob takes a string like "import java.util.*; // ArrayList" and returns "import java.util.ArrayList", for each class/type name that is listed as a comma separated list after "//".

func FindJava

func FindJava() (string, error)

FindJava finds the most likely location of a Java installation (with subfolders with .jar files) on the system.

func FindKotlin

func FindKotlin() (string, error)

FindKotlin finds the most likely location of a Kotlin installation (with subfolders with .jar files) on the system.

func Fix added in v1.5.0

func Fix(filename string, removeExistingImports, deGlob, verbose bool) ([]byte, error)

Fix reads in a file and tries to organize the imports. removeExistingImports can be set to true to remove existing imports deGlob can be set to true to try to expand wildcard imports

func ForEachByteLine added in v1.1.0

func ForEachByteLine(filename string, process func([]byte)) error

ForEachByteLine splits a file on '\n' and iterates over the byte slices

func ForEachByteLineInData added in v1.3.0

func ForEachByteLineInData(data []byte, process func([]byte))

ForEachByteLineInData splits data on '\n' and iterates over the byte slices

func ForEachLine added in v1.1.0

func ForEachLine(filename string, process func(string, string)) error

ForEachLine splits a file on '\n' and iterates over the lines. The callback function will be given each line and trimmed line as the function iterates.

func ForEachLineInData added in v1.3.0

func ForEachLineInData(data []byte, process func(string, string))

ForEachLineInData splits data on '\n' and iterates over the lines. The callback function will be given each line and trimmed line as the function iterates.

Types

type ImportMatcher

type ImportMatcher struct {
	JARPaths []string // list of paths to examine for .jar files

	DeGlob bool // generate import statements without "*"
	// contains filtered or unexported fields
}

ImportMatcher is a struct that contains a list of JAR file paths, and a lookup map from class names to class paths, which is populated when New or NewCustom is called.

func New

func New(settings ...bool) (*ImportMatcher, error)

New creates a new ImportMatcher. If onlyJava is false, /usr/share/kotlin/lib will be added to the .jar file search path. The first (optional) bool should be set to true if only Java should be considered, and not Kotlin. The second (optional) bool should be set to true if the import organizer should always start out with removing existing imports. The third (optional) bool should be set to true if the generated imports should be exact intead of with a glob ("*").

func NewCustom

func NewCustom(JARPaths []string, settings ...bool) (*ImportMatcher, error)

NewCustom creates a new ImportMatcher, given a slice of paths to search for .jar files The first (optional) bool should be set to true if only Java should be considered, and not Kotlin. The second (optional) bool should be set to true if the import organizer should always start out with removing existing imports. The third (optional) bool should be set to true if the generated imports should be exact intead of with a glob ("*").

func (*ImportMatcher) ClassMap

func (ima *ImportMatcher) ClassMap() map[string]string

ClassMap returns the mapping from class names to class paths

func (*ImportMatcher) FileImports added in v1.1.0

func (ima *ImportMatcher) FileImports(filename string, verbose bool) (string, error)

FileImports generates sorted "import" lines for a .java or .kotlin file (the ImportMatcher should be configured to be either for Java or Kotlin as well)

func (*ImportMatcher) FindImports

func (impM *ImportMatcher) FindImports(sourceCode string) []string

FindImports can find words that looks like classes, and then look up appropriate import package paths. Ignores "java.lang." classes.

func (*ImportMatcher) FixImports added in v1.3.0

func (ima *ImportMatcher) FixImports(data []byte, verbose bool) ([]byte, error)

FixImports generates sorted "import" lines for a .java or .kotlin file (the ImportMatcher should be configured to be either for Java or Kotlin as well). The existing imports (if any) are the replaced with the generated imports.

func (*ImportMatcher) ImportBlock added in v1.4.0

func (ima *ImportMatcher) ImportBlock(data []byte, verbose bool) ([]byte, error)

ImportBlock generates "import" lines for the given Java or Kotlin source code

func (*ImportMatcher) ImportPathExact

func (ima *ImportMatcher) ImportPathExact(exactClassName string) string

ImportPathExact takes the exact class name and tries to return the shortest specific import path for the matching class. For example, "File" could result in "java.io.File". The function returns an empty string if there are no matches.

func (*ImportMatcher) OrganizedImports

func (impM *ImportMatcher) OrganizedImports(sourceCode string, onlyJava bool) string

OrganizedImports generates import statements for packages that belongs to classes that are found in the given source code. If onlyJava is true, a semicolon is added after each line, and Kotlin jar files are not considered.

func (*ImportMatcher) StarPath

func (ima *ImportMatcher) StarPath(startOfClassName string) (string, string)

StarPath takes the start of the class name and tries to return the shortest found class name, and also the import path like "java.io.*" Returns empty strings if there are no matches.

func (*ImportMatcher) StarPathAll

func (ima *ImportMatcher) StarPathAll(startOfClassName string) ([]string, []string)

StarPathAll takes the start of the class name and tries to return all found class names, and also the import paths, like "java.io.*". Returns empty strings if there are no matches.

func (*ImportMatcher) StarPathAllExact

func (ima *ImportMatcher) StarPathAllExact(exactClassName string) ([]string, []string)

StarPathAllExact takes the exact class name and tries to return all matching class names, and also the import paths, like "java.io.*". Returns empty strings if there are no matches.

func (*ImportMatcher) StarPathExact

func (ima *ImportMatcher) StarPathExact(exactClassName string) string

StarPathExact takes the exact class name and tries to return the shortest import path for the matching class, if found, like "java.io.*". Returns empty string if there are no matches.

func (*ImportMatcher) String

func (ima *ImportMatcher) String() string

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL