Skip to content

Commit

Permalink
Intern cache approach
Browse files Browse the repository at this point in the history
  • Loading branch information
n1hility committed Sep 15, 2014
1 parent 22b3e70 commit ddfd774
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 29 deletions.
74 changes: 51 additions & 23 deletions src/main/java/org/jboss/jandex/GenericSignatureParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -225,11 +226,11 @@ ClassSignature parseClassSignature(String signature) {
this.signature = signature;
this.pos = 0;
Type[] parameters = parseTypeParameters();
Type superClass = parseClassTypeSignature();
Type superClass = names.intern(parseClassTypeSignature());
int end = signature.length();
List<Type> interfaces = new ArrayList<Type>();
while (pos < end) {
interfaces.add(parseClassTypeSignature());
interfaces.add(names.intern(parseClassTypeSignature()));
}

return new ClassSignature(parameters, superClass, interfaces.toArray(new Type[interfaces.size()]));
Expand All @@ -253,7 +254,11 @@ MethodSignature parseMethodSignature(String signature) {
expect('(');
List<Type> parameters = new ArrayList<Type>();
while (signature.charAt(pos) != ')') {
parameters.add(parseJavaType());
Type type = parseJavaType();
if (type == null) {
throw new IllegalArgumentException("Corrupted argument, or unclosed brace at: " + pos);
}
parameters.add(type);
}
pos++;

Expand Down Expand Up @@ -315,15 +320,17 @@ private Type[] parseTypeList(boolean argument) {
}
pos++;

List<Type> types = new ArrayList<Type>();
ArrayList<Type> types = new ArrayList<Type>();
for (;;) {
Type t = argument ? parseTypeArgument() : parseTypeParameter();
if (t == null) {
break;
}
types.add(t);
}

if (!argument) {
resolveTypeList(types);
}
return types.toArray(new Type[types.size()]);
}

Expand Down Expand Up @@ -393,16 +400,22 @@ private Type parseReturnType() {
private Type parseReferenceType() {
int mark = pos;
char c = signature.charAt(mark);
Type type;
switch (c) {
case 'T':
return parseTypeVariable();
type = parseTypeVariable();
break;
case 'L':
return parseClassTypeSignature();
type = parseClassTypeSignature();
break;
case '[':
return parseArrayType();
type = parseArrayType();
break;
default:
return null;
}

return names.intern(type);
}

private Type parseArrayType() {
Expand All @@ -414,7 +427,21 @@ private Type parseArrayType() {
private Type parseTypeVariable() {
String name = names.intern(signature.substring(pos + 1, advancePast(';')));
Type type = typeParameters.get(name);
return type == null ? new TypeVariable(name) : type;
return type == null ? new UnresolvedTypeVariable(name) : type;
}

private void resolveTypeList(ArrayList<Type> list) {
int size = list.size();
for (int i = 0; i < size; i++) {
Type type = list.get(i);
if (type instanceof UnresolvedTypeVariable) {
type = typeParameters.get(((UnresolvedTypeVariable)type).identifier());
if (type != null) {
list.set(i, type);
}
}
}

}

private Type parseJavaType() {
Expand Down Expand Up @@ -471,20 +498,21 @@ private int advanceNameEnd() {

public static void main(String[] args) throws IOException {
GenericSignatureParser parser = new GenericSignatureParser();
MethodSignature sig1 = parser.parseMethodSignature("<U:Ljava/lang/Foo;>(Ljava/lang/Class<TU;>;TU;)Ljava/lang/Class<+TU;>;");
MethodSignature sig2 = parser.parseMethodSignature("<K:Ljava/lang/Object;V:Ljava/lang/Object;>(Ljava/util/Map<TK;TV;>;Ljava/lang/Class<TK;>;Ljava/lang/Class<TV;>;)Ljava/util/Map<TK;TV;>;");
MethodSignature sig3 = parser.parseMethodSignature("<T:Ljava/lang/Object;>(Ljava/util/Collection<-TT;>;[TT;)Z");
MethodSignature sig4 = parser.parseMethodSignature("(Ljava/util/Collection<*>;Ljava/util/Collection<*>;)Z");
MethodSignature sig7 = parser.parseMethodSignature("()Lcom/sun/xml/internal/bind/v2/model/impl/ElementInfoImpl<Ljava/lang/reflect/Type;Ljava/lang/Class;Ljava/lang/reflect/Field;Ljava/lang/reflect/Method;>.PropertyImpl;");
ClassSignature sig5 = parser.parseClassSignature("<C:Lio/undertow/server/protocol/framed/AbstractFramedChannel<TC;TR;TS;>;R:Lio/undertow/server/protocol/framed/AbstractFramedStreamSourceChannel<TC;TR;TS;>;S:Lio/undertow/server/protocol/framed/AbstractFramedStreamSinkChannel<TC;TR;TS;>;>Ljava/lang/Object;Lorg/xnio/channels/ConnectedChannel;");
ClassSignature sig6 = parser.parseClassSignature("Lcom/apple/laf/AquaUtils$RecyclableSingleton<Ljavax/swing/text/LayeredHighlighter$LayerPainter;>;");
System.out.println(sig1);
System.out.println(sig2);
System.out.println(sig3);
System.out.println(sig4);
System.out.println(sig5);
System.out.println(sig6);
System.out.println(sig7);
MethodSignature sig1 = parser.parseMethodSignature("<U:Ljava/lang/Foo;>((Ljava/lang/Class<TU;>;TU;)Ljava/lang/Class<+TU;>;");
// MethodSignature sig1 = parser.parseMethodSignature("<U:Ljava/lang/Foo;>(Ljava/lang/Class<TU;>;TU;)Ljava/lang/Class<+TU;>;");
// MethodSignature sig2 = parser.parseMethodSignature("<K:Ljava/lang/Object;V:Ljava/lang/Object;>(Ljava/util/Map<TK;TV;>;Ljava/lang/Class<TK;>;Ljava/lang/Class<TV;>;)Ljava/util/Map<TK;TV;>;");
// MethodSignature sig3 = parser.parseMethodSignature("<T:Ljava/lang/Object;>(Ljava/util/Collection<-TT;>;[TT;)Z");
// MethodSignature sig4 = parser.parseMethodSignature("(Ljava/util/Collection<*>;Ljava/util/Collection<*>;)Z");
// MethodSignature sig7 = parser.parseMethodSignature("()Lcom/sun/xml/internal/bind/v2/model/impl/ElementInfoImpl<Ljava/lang/reflect/Type;Ljava/lang/Class;Ljava/lang/reflect/Field;Ljava/lang/reflect/Method;>.PropertyImpl;");
// ClassSignature sig5 = parser.parseClassSignature("<C:Lio/undertow/server/protocol/framed/AbstractFramedChannel<TC;TR;TS;>;R:Lio/undertow/server/protocol/framed/AbstractFramedStreamSourceChannel<TC;TR;TS;>;S:Lio/undertow/server/protocol/framed/AbstractFramedStreamSinkChannel<TC;TR;TS;>;>Ljava/lang/Object;Lorg/xnio/channels/ConnectedChannel;");
// ClassSignature sig6 = parser.parseClassSignature("Lcom/apple/laf/AquaUtils$RecyclableSingleton<Ljavax/swing/text/LayeredHighlighter$LayerPainter;>;");
// System.out.println(sig1);
// System.out.println(sig2);
// System.out.println(sig3);
// System.out.println(sig4);
// System.out.println(sig5);
// System.out.println(sig6);
// System.out.println(sig7);

// BufferedReader reader = new BufferedReader(new FileReader("/Users/jason/sigmethods.txt"));
// String line;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/jboss/jandex/NameTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
class NameTable {
private StrongInternPool<String> stringPool = new StrongInternPool<String>();
private StrongInternPool<DotName> namePool = new StrongInternPool<DotName>();
private StrongInternPool<Type> typePool = new StrongInternPool<Type>();
private Map<String, DotName> names = new HashMap<String, DotName>();

DotName convertToName(String name) {
Expand Down Expand Up @@ -71,6 +71,10 @@ String intern(String string) {
return stringPool.intern(string);
}

Type intern(Type type) {
return typePool.intern(type);
}

DotName intern(DotName dotName, char delim) {
String name = dotName.toString(delim);
DotName old = names.get(name);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/jboss/jandex/ParameterizedType.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public String toString() {
return builder.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
Expand All @@ -79,10 +78,11 @@ public boolean equals(Object o) {
}

ParameterizedType other = (ParameterizedType) o;
return owner.equals(other.owner) && Arrays.equals(parameters, other.parameters);

return (owner == other.owner || (owner != null && owner.equals(other.owner)))
&& Arrays.equals(parameters, other.parameters);
}

@Override
public int hashCode() {
int hash = this.hash;
if (hash != 0) {
Expand All @@ -91,7 +91,7 @@ public int hashCode() {

hash = super.hashCode();
hash = 31 * hash + Arrays.hashCode(parameters);
hash = 31 * hash + owner.hashCode();
return this.hash = hash;
hash = 31 * hash + (owner != null ? owner.hashCode() : 0);
return hash;
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/jboss/jandex/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public enum Kind {

TYPE_VARIABLE,

UNRESOLVED_TYPE_VARIABLE,

WILDCARD_TYPE,

PARAMETERIZED_TYPE;
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/org/jboss/jandex/UnresolvedTypeVariable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2013 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.jandex;

/**
* @author Jason T. Greene
*/
public final class UnresolvedTypeVariable extends Type {
private final String name;
private int hash;

UnresolvedTypeVariable(String name) {
super(DotName.OBJECT_NAME);
this.name = name;
}

public String identifier() {
return name;
}

@Override
public Kind kind() {
return Kind.UNRESOLVED_TYPE_VARIABLE;
}

public String toString() {
return name;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (!(o instanceof UnresolvedTypeVariable)) {
return false;
}

UnresolvedTypeVariable other = (UnresolvedTypeVariable) o;

return name.equals(other.name);

}

@Override
public int hashCode() {
return name.hashCode();
}
}

0 comments on commit ddfd774

Please sign in to comment.