Skip to content

Commit

Permalink
Refined model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Winterhalter committed May 21, 2015
1 parent 79f36c9 commit cc06527
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 53 deletions.
Expand Up @@ -6,7 +6,7 @@
/**
* Implementations describe an element represented in byte code, i.e. a type, a field or a method or a constructor.
*/
public interface ByteCodeElement extends NamedElement, ModifierReviewable, DeclaredByType, AnnotatedCodeElement {
public interface ByteCodeElement extends NamedElement.WithRuntimeName, ModifierReviewable, DeclaredByType, AnnotatedCodeElement {

/**
* Returns the descriptor of this byte code element.
Expand Down
Expand Up @@ -10,30 +10,22 @@ public interface NamedElement {
*/
String EMPTY_NAME = "";

/**
* Returns the internalName of this byte code element.
*
* @return The internalName of this byte code element as visible from within a running Java application.
*/
String getName();
String getSourceCodeName();

/**
* Returns the internal internalName of this byte code element.
*
* @return The internal internalName of this byte code element as used within the Java class file format.
*/
String getInternalName();
interface WithRuntimeName extends NamedElement {

/**
* Returns the name of this byte code element as it is defined in Java source code. This means:
* <ul>
* <li>For type descriptions, the main distinction is the display of arrays whose actual names are blended
* with internal names when calling {@link ByteCodeElement#getName()}.</li>
* <li>For method descriptions, representations of constructors and the type initializer, return the
* empty string.</li>
* </ul>
*
* @return The name of this type as represented in Java source code.
*/
String getSourceCodeName();
/**
* Returns the internalName of this byte code element.
*
* @return The internalName of this byte code element as visible from within a running Java application.
*/
String getName();

/**
* Returns the internal internalName of this byte code element.
*
* @return The internal internalName of this byte code element as used within the Java class file format.
*/
String getInternalName();
}
}
Expand Up @@ -14,12 +14,11 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;

/**
* Description of the parameter of a Java method or constructor.
*/
public interface ParameterDescription extends AnnotatedCodeElement, NamedElement, ModifierReviewable {
public interface ParameterDescription extends AnnotatedCodeElement, NamedElement.WithRuntimeName, ModifierReviewable {

/**
* The prefix for names of an unnamed parameter.
Expand Down
Expand Up @@ -7,7 +7,7 @@
/**
* A package description represents a Java package.
*/
public interface PackageDescription extends NamedElement, AnnotatedCodeElement {
public interface PackageDescription extends NamedElement.WithRuntimeName, AnnotatedCodeElement {

/**
* Checks if this package description represents a sealed package.
Expand Down
@@ -1,5 +1,6 @@
package net.bytebuddy.description.type.generic;

import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.utility.JavaMethod;
Expand All @@ -8,7 +9,7 @@
import java.util.Collections;
import java.util.List;

public interface GenericType {
public interface GenericType extends NamedElement {

Sort getSort();

Expand Down Expand Up @@ -122,6 +123,11 @@ public String getTypeName() {
return toString();
}

@Override
public String getSourceCodeName() {
return toString();
}

@Override
public boolean equals(Object other) {
if (!(other instanceof GenericType)) return false;
Expand Down Expand Up @@ -233,6 +239,11 @@ public String getTypeName() {
return toString();
}

@Override
public String getSourceCodeName() {
return toString();
}

@Override
public int hashCode() {
int lowerHash = 1, upperHash = 1;
Expand Down Expand Up @@ -368,6 +379,11 @@ public String getSymbol() {
return null;
}

@Override
public String getSourceCodeName() {
return toString();
}

@Override
public int hashCode() {
int result = 1;
Expand Down Expand Up @@ -456,10 +472,6 @@ public static class Latent extends ForParameterizedType {

private final GenericType ownerType;

public static GenericType of(TypeDescription rawType, List<? extends GenericType> parameters) {
return new Latent(rawType, parameters, rawType.getEnclosingType());
}

public Latent(TypeDescription rawType, List<? extends GenericType> parameters, GenericType ownerType) {
this.rawType = rawType;
this.parameters = parameters;
Expand Down Expand Up @@ -523,6 +535,11 @@ public String getTypeName() {
return toString();
}

@Override
public String getSourceCodeName() {
return getSymbol();
}

@Override
public int hashCode() {
return getVariableSource().hashCode() ^ getSymbol().hashCode();
Expand Down Expand Up @@ -655,6 +672,11 @@ public String getSymbol() {
return resolve().getSymbol();
}

@Override
public String getSourceCodeName() {
return resolve().getSourceCodeName();
}

@Override
public int hashCode() {
return resolve().hashCode();
Expand Down
66 changes: 46 additions & 20 deletions byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java
Expand Up @@ -14,6 +14,7 @@
import net.bytebuddy.description.type.TypeList;
import net.bytebuddy.description.type.generic.GenericType;
import net.bytebuddy.description.type.generic.GenericTypeList;
import net.bytebuddy.description.type.generic.TypeVariableSource;
import net.bytebuddy.dynamic.ClassFileLocator;
import net.bytebuddy.implementation.bytecode.StackSize;
import net.bytebuddy.matcher.ElementMatchers;
Expand Down Expand Up @@ -1368,13 +1369,23 @@ class ForTypeVariable implements Token {

private final String name;

public ForTypeVariable(String name) {
private final TypeVariableSourceToken typeVariableSourceToken;

public ForTypeVariable(String name, TypeVariableSourceToken typeVariableSourceToken) {
this.name = name;
this.typeVariableSourceToken = typeVariableSourceToken;
}

@Override
public GenericType toGenericType(TypePool typePool) {
return null;
// TODO: Need to traverse hierarchy! method -> type -> nested type
return typeVariableSourceToken.resolve(typePool).getTypeVariables().filter(named(name)).getOnly();
}

public interface TypeVariableSourceToken {

TypeVariableSource resolve(TypePool typePool);

}
}

Expand All @@ -1392,11 +1403,11 @@ public GenericType toGenericType(TypePool typePool) {
}
}

class ForLowerBound implements Token {
class ForLowerBoundWildcard implements Token {

private final Token baseType;

public ForLowerBound(Token baseType) {
public ForLowerBoundWildcard(Token baseType) {
this.baseType = baseType;
}

Expand All @@ -1406,11 +1417,11 @@ public GenericType toGenericType(TypePool typePool) {
}
}

class ForUpperBound implements Token {
class ForUpperBoundWildcard implements Token {

private final Token baseType;

public ForUpperBound(Token baseType) {
public ForUpperBoundWildcard(Token baseType) {
this.baseType = baseType;
}

Expand All @@ -1437,7 +1448,8 @@ public GenericType toGenericType(TypePool typePool) {
for (Token parameter : parameters) {
genericTypes.add(parameter.toGenericType(typePool));
}
return GenericType.ForParameterizedType.Latent.of(typePool.describe(name).resolve(), genericTypes);
TypeDescription rawType = typePool.describe(name).resolve();
return new GenericType.ForParameterizedType.Latent(rawType, genericTypes, rawType.getEnclosingType());
}

public static class Nested implements Token {
Expand Down Expand Up @@ -1575,7 +1587,7 @@ public void visitBaseType(char descriptor) {

@Override
public void visitTypeVariable(String name) {
genericTypeRegistrant.register(new Token.ForTypeVariable(name));
genericTypeRegistrant.register(new Token.ForTypeVariable(name, null)); // TODO!
}

@Override
Expand Down Expand Up @@ -1627,6 +1639,8 @@ protected interface IncompleteToken {

boolean isParameterized();

String getName();

Token toToken();

abstract class AbstractBase implements IncompleteToken {
Expand Down Expand Up @@ -1664,62 +1678,74 @@ protected class ForUpperBound implements GenericTypeRegistrant {

@Override
public void register(Token token) {
parameters.add(new Token.ForUpperBound(token));
parameters.add(new Token.ForUpperBoundWildcard(token));
}
}

protected class ForLowerBound implements GenericTypeRegistrant {

@Override
public void register(Token token) {
parameters.add(new Token.ForLowerBound(token));
parameters.add(new Token.ForLowerBoundWildcard(token));
}
}
}

class ForTopLevelClass extends AbstractBase {

private final String name;
private final String internalName;

public ForTopLevelClass(String name) {
this.name = name;
public ForTopLevelClass(String internalName) {
this.internalName = internalName;
}

@Override
public Token toToken() {
return isParameterized()
? new Token.ForParameterizedType(name, parameters)
: new Token.ForRawType(name);
? new Token.ForParameterizedType(getName(), parameters)
: new Token.ForRawType(getName());
}

@Override
public boolean isParameterized() {
return !parameters.isEmpty();
}

@Override
public String getName() {
return internalName.replace('/', '.');
}
}

class ForInnerClass extends AbstractBase {

private final String name;
private static final char INNER_CLASS_SEPERATOR = '$';

private final String internalName;

private final IncompleteToken outerClassToken;

public ForInnerClass(String name, IncompleteToken outerClassToken) {
this.name = name;
public ForInnerClass(String internalName, IncompleteToken outerClassToken) {
this.internalName = internalName;
this.outerClassToken = outerClassToken;
}

@Override
public Token toToken() {
return isParameterized() || outerClassToken.isParameterized()
? new Token.ForParameterizedType.Nested(name, parameters, outerClassToken.toToken())
: new Token.ForRawType(name);
? new Token.ForParameterizedType.Nested(getName(), parameters, outerClassToken.toToken())
: new Token.ForRawType(getName());
}

@Override
public boolean isParameterized() {
return !parameters.isEmpty() || !outerClassToken.isParameterized();
}

@Override
public String getName() {
return outerClassToken.getName() + INNER_CLASS_SEPERATOR + internalName.replace('/', '.');
}
}
}
}
Expand Down

0 comments on commit cc06527

Please sign in to comment.