Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference type with partially qualified namespace

Tags:

java

types

Is it possible to reference Java types with a partially qualified name? If so, how?

The scenario: I frequently find myself with a data class (e.g. Activity) which needs a view. My standard practice has been to name this class ActivityView, which works, but this view class invariably ends up in a tld.organization.project.views namespace, where the "View" suffix is entirely redundant.

I'd like to remove "View" suffix (so the types would be tld.organization.project.Activity and tld.organization.project.views.Activity), but this means I must use the namespace to qualify the types when I reference them in the same class. Using the namespace to qualify type references is not a bad thing in and of itself, but repeating the fully qualified name of either type is repetitious and difficult to read.

Referencing a partially qualified type (something like ~.Activity or ~.views.Activity) would remove that cruft. Some kind of type aliasing would answer, but it appears Java doesn't support such functionality. Are there any alternatives?

like image 477
cqcallaw Avatar asked Apr 23 '12 03:04

cqcallaw


People also ask

What is fully qualified namespace?

Fully qualified name. This is an identifier with a namespace separator that begins with a namespace separator, such as \Foo\Bar . The namespace \Foo is also a fully qualified name. Relative name. This is an identifier starting with namespace , such as namespace\Foo\Bar .

What is fully qualified type name?

A fully qualified type name consists of an assembly name specification, a namespace specification, and a type name. Type name specifications are used by methods such as Type. GetType, Module. GetType, ModuleBuilder.

What is qualified name in C++?

The main function. Modules (C++20) [edit] A qualified name is a name that appears on the right hand side of the scope resolution operator :: (see also qualified identifiers).

What is fully qualified class name in Java?

A fully-qualified class name in Java contains the package that the class originated from. An example of this is java. util. ArrayList. The fully-qualified class name can be obtained using the getName() method.


1 Answers

No, you can't do that with packages in Java. The closest you could get would be to organize things into nested hierarchies of classes instead of packages. Between that and strategic static importing, you could get the desired effect, though it would be a terribly messy solution. For example:

package tld.organization.project;
public class Activity {}

and:

package tld.organization.project;
public class Views {
    public static class Activity {}
}

which can then be referred to as:

public void whatever() {
    Activity a = new Activity();
    Views.Activity a2 = new Views.Activity();
}

I'd suggest that the problems you're having with names may be pointing to a design problem that needs to be sorted out.

P.S. If I ever had to work on a project that organized classes so, I might have to shoot myself.

P.P.S. Actually, I'd probably try to shoot you first.

like image 98
Ryan Stewart Avatar answered Nov 16 '22 02:11

Ryan Stewart