Skip to content

Commit

Permalink
Partial - Parser and type refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
n1hility committed Sep 6, 2014
1 parent f56d97e commit 44d77cf
Show file tree
Hide file tree
Showing 16 changed files with 1,102 additions and 61 deletions.
73 changes: 73 additions & 0 deletions src/main/java/org/jboss/jandex/ArrayType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 ArrayType extends Type {
private Type component;
private int dimensions;

ArrayType(Type component, int dimensions) {
super(null);
this.dimensions = dimensions;
this.component = component;
}

public ArrayType(DotName name) {
// FIXME parse;
super(name);
}

public Type component() {
return component;
}

@Override
public DotName name() {
StringBuilder builder = new StringBuilder();
int dimensions = this.dimensions;
while (dimensions-- > 0) {
builder.append('[');
}
if (component instanceof PrimitiveType) {
builder.append(((PrimitiveType)component).toCode());
} else {
// This relies on name() representing the erased type name
// FIXME - Revisit whether we need /s or .s
builder.append('L').append(component.name().toString().replace('.', '/')).append(';');
}

return DotName.createSimple(builder.toString());
}

public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(component);
for (int i = 0; i < dimensions; i++) {
builder.append("[]");
}
return builder.toString();
}

@Override
public Kind kind() {
return Kind.ARRAY;
}
}
4 changes: 2 additions & 2 deletions src/main/java/org/jboss/jandex/ClassInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Java class, it is not intended as a complete replacement for Java reflection.
* Only the methods and fields which are references by an annotation are stored.
*
* <p>Global information including the parent class, implemented interfaces, and
* <p>Global information including the parent class, implemented methodParameters, and
* access flags are also provided since this information is often necessary.
*
* <p>Note that a parent class and interface may exist outside of the scope of the
Expand Down Expand Up @@ -73,7 +73,7 @@ public final class ClassInfo implements AnnotationTarget {
* @param name the name of this class
* @param superName the name of the parent class
* @param flags the class attributes
* @param interfaces the interfaces this class implements
* @param interfaces the methodParameters this class implements
* @param annotations the annotations on this class
* @return a new mock class representation
*/
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/jboss/jandex/ClassType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 ClassType extends Type {
ClassType(DotName name) {
super(name);
}

@Override
public Kind kind() {
return Kind.CLASS;
}
}
60 changes: 48 additions & 12 deletions src/main/java/org/jboss/jandex/DotName.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,21 @@
*
*/
public final class DotName implements Comparable<DotName> {
static final DotName JAVA_NAME;
static final DotName JAVA_LANG_NAME;
static final DotName OBJECT_NAME;

private final DotName prefix;
private final String local;
private int hash;
private boolean componentized = false;
private boolean innerClass = false;

static {
JAVA_NAME = new DotName(null, "java", true, false);
JAVA_LANG_NAME = new DotName(JAVA_NAME, "lang", true, false);
OBJECT_NAME = new DotName(JAVA_LANG_NAME, "Object", true, false);
}

/**
* Constructs a simple DotName which stores the string in it's entirety. This variant is ideal
Expand All @@ -57,7 +68,7 @@ public final class DotName implements Comparable<DotName> {
* @return a simple DotName that wraps name
*/
public static DotName createSimple(String name) {
return new DotName(null, name, false);
return new DotName(null, name, false, false);
}

/**
Expand All @@ -77,16 +88,37 @@ public static DotName createComponentized(DotName prefix, String localName) {
if (localName.indexOf('.') != -1)
throw new IllegalArgumentException("A componentized DotName can not contain '.' characters in a local name");

return new DotName(prefix, localName, true);
return new DotName(prefix, localName, true, false);
}

DotName(DotName prefix, String local, boolean noDots) {
/**
* Constructs a componentized DotName. Each DotName refers to a parent
* prefix (or null if there is no further prefix) in addition to a local
* name that has no dot separator. The fully qualified name this DotName
* represents is consructed by recursing all parent prefixes and joining all
* local name values with the '.' character.
*
* @param prefix Another DotName that is the portion to the left of
* localName, this may be null if there is not one
* @param localName the local non-null portion of this name, which does not contain
* '.'
* @return a componentized DotName.
*/
public static DotName createComponentized(DotName prefix, String localName, boolean innerClass) {
if (localName.indexOf('.') != -1)
throw new IllegalArgumentException("A componentized DotName can not contain '.' characters in a local name");

return new DotName(prefix, localName, true, innerClass);
}

DotName(DotName prefix, String local, boolean noDots, boolean innerClass) {
if (local == null)
throw new IllegalArgumentException("Local string can not be null");

this.prefix = prefix;
this.local = local;
this.componentized = (prefix == null || prefix.componentized) && noDots;
this.innerClass = innerClass;
}

/**
Expand Down Expand Up @@ -125,13 +157,18 @@ public boolean isComponentized() {
* @return The fully qualified class name
*/
public String toString() {
StringBuilder string = new StringBuilder();
if (prefix != null)
string.append(prefix).append(".");
return toString('.');
}

string.append(local);
public String toString(char delim) {
String string = local;
if (prefix != null) {
StringBuilder builder = new StringBuilder();
builder.append(prefix.toString(delim)).append(innerClass ? '$' : delim).append(string);
string = builder.toString();
}

return string.toString();
return string;
}

public int hashCode() {
Expand All @@ -140,7 +177,7 @@ public int hashCode() {
return hash;

if (prefix != null) {
hash = prefix.hashCode() * 31 + '.';
hash = prefix.hashCode() * 31 + (innerClass ? '$' : '.');

// Luckily String.hashCode documents the algorithm it follows
for (int i = 0; i < local.length(); i++) {
Expand Down Expand Up @@ -205,13 +242,12 @@ public boolean equals(Object o) {
if (other.prefix == null && prefix == null)
return local.equals(other.local);

if (other.prefix == null && prefix != null)
if (!other.componentized && componentized)
return toString().equals(other.local);

if (other.prefix != null && prefix == null)
if (other.componentized && !componentized)
return other.toString().equals(local);


return local.equals(other.local) && prefix.equals(other.prefix);
}
}
Loading

0 comments on commit 44d77cf

Please # to comment.