Skip to content

Commit

Permalink
[With] fixing the internal aliasing system
Browse files Browse the repository at this point in the history
old Wither annotations were no longer being picked up.
  • Loading branch information
rzwitserloot committed Aug 29, 2019
1 parent 514fd71 commit 67fccba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
21 changes: 21 additions & 0 deletions src/core/lombok/core/LombokInternalAliasing.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package lombok.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -30,6 +31,7 @@ public class LombokInternalAliasing {
/** Maps a package name to a space separated list of packages. If the key package is star-imported, assume all packages in the 'value' part of the MapEntry are too. */
public static final Map<String, Collection<String>> IMPLIED_EXTRA_STAR_IMPORTS;
public static final Map<String, String> ALIASES;
public static final Map<String, Collection<String>> REVERSE_ALIASES;

/**
* Provide a fully qualified name (FQN), and the canonical version of this is returned.
Expand All @@ -53,5 +55,24 @@ public static String processAliases(String in) {
m2.put("lombok.Delegate", "lombok.experimental.Delegate");
m2.put("lombok.experimental.Wither", "lombok.With");
ALIASES = Collections.unmodifiableMap(m2);

Map<String, Collection<String>> m3 = new HashMap<String, Collection<String>>();
for (Map.Entry<String, String> e : m2.entrySet()) {
Collection<String> c = m3.get(e.getValue());
if (c == null) {
m3.put(e.getValue(), Collections.singleton(e.getKey()));
} else if (c.size() == 1) {
Collection<String> newC = new ArrayList<String>(2);
newC.addAll(c);
m3.put(e.getValue(), c);
} else {
c.add(e.getKey());
}
}
for (Map.Entry<String, Collection<String>> e : m3.entrySet()) {
Collection<String> c = e.getValue();
if (c.size() > 1) e.setValue(Collections.unmodifiableList((ArrayList<String>) c));
}
REVERSE_ALIASES = Collections.unmodifiableMap(m3);
}
}
23 changes: 16 additions & 7 deletions src/core/lombok/core/TypeLibrary.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2015 The Project Lombok Authors.
* Copyright (C) 2009-2019 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,6 +21,7 @@
*/
package lombok.core;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -75,6 +76,14 @@ private TypeLibrary(String fqnSingleton) {
}

public static TypeLibrary createLibraryForSingleType(String fqnSingleton) {
if (LombokInternalAliasing.REVERSE_ALIASES.containsKey(fqnSingleton)) {
// Internal aliasing is a little too complex to handle with the map-less 'efficient' implementation.
TypeLibrary tl = new TypeLibrary();
tl.addType(fqnSingleton);
tl.lock();
return tl;
}

return new TypeLibrary(fqnSingleton);
}

Expand All @@ -89,16 +98,19 @@ public void addType(String fullyQualifiedTypeName) {
if (locked) throw new IllegalStateException("locked");
int idx = fullyQualifiedTypeName.lastIndexOf('.');
if (idx == -1) throw new IllegalArgumentException(
"Only fully qualified types are allowed (and stuff in the default package is not palatable to us either!)");
"Only fully qualified types are allowed (types in the default package cannot be added here either)");
String unqualified = fullyQualifiedTypeName.substring(idx + 1);
if (unqualifiedToQualifiedMap == null) throw new IllegalStateException("SingleType library");

unqualifiedToQualifiedMap.put(unqualified.replace("$", "."), dotBased);
unqualifiedToQualifiedMap.put(unqualified, dotBased);
unqualifiedToQualifiedMap.put(fullyQualifiedTypeName, dotBased);
unqualifiedToQualifiedMap.put(dotBased, dotBased);
for (Map.Entry<String, String> e : LombokInternalAliasing.ALIASES.entrySet()) {
if (fullyQualifiedTypeName.equals(e.getValue())) unqualifiedToQualifiedMap.put(e.getKey(), dotBased);
Collection<String> oldNames = LombokInternalAliasing.REVERSE_ALIASES.get(fullyQualifiedTypeName);
if (oldNames != null) for (String oldName : oldNames) {
unqualifiedToQualifiedMap.put(oldName, dotBased);
int li = oldName.lastIndexOf('.');
if (li != -1) unqualifiedToQualifiedMap.put(oldName.substring(li + 1), dotBased);
}

int idx2 = fullyQualifiedTypeName.indexOf('$', idx + 1);
Expand All @@ -119,9 +131,6 @@ public void addType(String fullyQualifiedTypeName) {
public String toQualified(String typeReference) {
if (unqualifiedToQualifiedMap == null) {
if (typeReference.equals(unqualified) || typeReference.equals(qualified)) return qualified;
for (Map.Entry<String, String> e : LombokInternalAliasing.ALIASES.entrySet()) {
if (e.getKey().equals(typeReference)) return e.getValue();
}
return null;
}
return unqualifiedToQualifiedMap.get(typeReference);
Expand Down
4 changes: 2 additions & 2 deletions src/core/lombok/core/TypeResolver.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009-2015 The Project Lombok Authors.
* Copyright (C) 2009-2019 The Project Lombok Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -45,7 +45,7 @@ public boolean typeMatches(LombokNode<?, ?, ?> context, String fqn, String typeR

public String typeRefToFullyQualifiedName(LombokNode<?, ?, ?> context, TypeLibrary library, String typeRef) {
typeRef = LombokInternalAliasing.processAliases(typeRef);
// When asking if 'Foo' could possibly be referring to 'bar.Baz', the answer is obviously no.
// When asking if 'Foo' could possibly be referring to 'bar.Baz', the answer is obviously no.
String qualified = library.toQualified(typeRef);
if (qualified == null) return null;

Expand Down

0 comments on commit 67fccba

Please sign in to comment.