Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aliasing a java package name in clojure

Tags:

clojure

Given a java package x.y.z, can I alias x.y.z to a shorter name, so that i can then refer to java classes inside the package as my-alias.MyJavaClass.

If that isn't possible, I could just import all classes into my namespace, but I don't want to specify the names of each class manually, and the clojure API docs doesn't seem clear on whether it's possible to import all classes in a package automatically.

like image 778
npad Avatar asked Nov 08 '09 22:11

npad


People also ask

How do you name a package in Java?

Package names are written in all lower case to avoid conflict with the names of classes or interfaces. Companies use their reversed Internet domain name to begin their package names—for example, com. example. mypackage for a package named mypackage created by a programmer at example.com .

What is your default Java package name?

Java compiler imports java. lang package internally by default.

What does package name mean in Java?

A package in Java is used to group related classes. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories: Built-in Packages (packages from the Java API)

How do I import Java?

To import java package into a class, we need to use java import keyword which is used to access package and its classes into the java program. Use import to access built-in and user-defined packages into your java source file so that your class can refer to a class that is in another package by directly using its name.


1 Answers

There isn't any functionality for this (yet). Java packages are in a different namespace than Clojure's, so the usual alias tricks won't work.

What you can do is import each class, which lets you avoid the full package+class name:

(import [java.io File Writer Reader])
(new File "/")
; #<File />

Rich provides his reasons for not supporting (import [java.io.*]) here.

like image 197
Mike Douglas Avatar answered Oct 09 '22 17:10

Mike Douglas