下面列出了java.lang.reflect.MalformedParameterizedTypeException#java.lang.reflect.GenericSignatureFormatError 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
Type parseFieldTypeSignature() {
// FieldTypeSignature ::= ClassTypeSignature | ArrayTypeSignature
// | TypeVariableSignature.
switch (symbol) {
case 'L':
return parseClassTypeSignature();
case '[':
// ArrayTypeSignature ::= "[" TypSignature.
scanSymbol();
return new GenericArrayTypeImpl(parseTypeSignature());
case 'T':
return parseTypeVariableSignature();
default:
throw new GenericSignatureFormatError();
}
}
public void run(){
new GenericSignatureFormatError();
ParameterizedType<MyStringType> pt = new ParameterizedType<MyStringType>();
pt.add(new MyStringType());
pt.add(new MyStringType(){});
for (MyType myType : pt.getList()){
System.out.println();
System.out.println("myType " + myType);
System.out.println("Enclosed by " + myType.getClass().getEnclosingMethod());
System.out.println("Enclosed by " + myType.getClass().getEnclosingClass().getName());
}
// Field[] fields = this.getClass().getDeclaredFields();
for (Field field : this.getClass().getDeclaredFields()){
System.out.println();
for (Annotation a : field.getAnnotations()){
System.out.println(a);
}
System.out.println(field);
System.out.println("generic type " + field.getGenericType());
}
for (TypeVariable tv : pt.getClass().getTypeParameters()){
System.out.println();
System.out.println(tv);
for (Type t : tv.getBounds()){
System.out.println("bounds " + t);
}
}
}
void scanSymbol() {
if (!eof) {
if (pos < buffer.length) {
symbol = buffer[pos];
pos++;
} else {
symbol = 0;
eof = true;
}
} else {
throw new GenericSignatureFormatError();
}
}
void expect(char c) {
if (symbol == c) {
scanSymbol();
} else {
throw new GenericSignatureFormatError();
}
}
void scanIdentifier() {
if (!eof) {
StringBuilder identBuf = new StringBuilder(32);
if (!isStopSymbol(symbol)) {
identBuf.append(symbol);
do {
char ch = buffer[pos];
if ((ch >= 'a') && (ch <= 'z') || (ch >= 'A') && (ch <= 'Z')
|| !isStopSymbol(ch)) {
identBuf.append(ch);
pos++;
} else {
identifier = identBuf.toString();
scanSymbol();
return;
}
} while (pos != buffer.length);
identifier = identBuf.toString();
symbol = 0;
eof = true;
} else {
// Ident starts with incorrect char.
symbol = 0;
eof = true;
throw new GenericSignatureFormatError();
}
} else {
throw new GenericSignatureFormatError();
}
}
/**
* {@inheritDoc}
*/
public String getGenericSignature() {
TypeDescription.Generic fieldType = getType();
try {
return fieldType.getSort().isNonGeneric()
? NON_GENERIC_SIGNATURE
: fieldType.accept(new TypeDescription.Generic.Visitor.ForSignatureVisitor(new SignatureWriter())).toString();
} catch (GenericSignatureFormatError ignored) {
return NON_GENERIC_SIGNATURE;
}
}
/**
* {@inheritDoc}
*/
public String getGenericSignature() {
try {
SignatureWriter signatureWriter = new SignatureWriter();
boolean generic = false;
for (TypeDescription.Generic typeVariable : getTypeVariables()) {
signatureWriter.visitFormalTypeParameter(typeVariable.getSymbol());
boolean classBound = true;
for (TypeDescription.Generic upperBound : typeVariable.getUpperBounds()) {
upperBound.accept(new TypeDescription.Generic.Visitor.ForSignatureVisitor(classBound
? signatureWriter.visitClassBound()
: signatureWriter.visitInterfaceBound()));
classBound = false;
}
generic = true;
}
for (TypeDescription.Generic parameterType : getParameters().asTypeList()) {
parameterType.accept(new TypeDescription.Generic.Visitor.ForSignatureVisitor(signatureWriter.visitParameterType()));
generic = generic || !parameterType.getSort().isNonGeneric();
}
TypeDescription.Generic returnType = getReturnType();
returnType.accept(new TypeDescription.Generic.Visitor.ForSignatureVisitor(signatureWriter.visitReturnType()));
generic = generic || !returnType.getSort().isNonGeneric();
TypeList.Generic exceptionTypes = getExceptionTypes();
if (!exceptionTypes.filter(not(ofSort(TypeDefinition.Sort.NON_GENERIC))).isEmpty()) {
for (TypeDescription.Generic exceptionType : exceptionTypes) {
exceptionType.accept(new TypeDescription.Generic.Visitor.ForSignatureVisitor(signatureWriter.visitExceptionType()));
generic = generic || !exceptionType.getSort().isNonGeneric();
}
}
return generic
? signatureWriter.toString()
: NON_GENERIC_SIGNATURE;
} catch (GenericSignatureFormatError ignored) {
return NON_GENERIC_SIGNATURE;
}
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
if (DEBUG) System.out.println("Parse error:" + errorMsg);
return new GenericSignatureFormatError();
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
if (DEBUG) System.out.println("Parse error:" + errorMsg);
return new GenericSignatureFormatError();
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
private Error error(String errorMsg) {
return new GenericSignatureFormatError("Signature Parse error: " + errorMsg +
"\n\tRemaining input: " + remainder());
}
@Test(expected = GenericSignatureFormatError.class)
public void testMalformedTypeSignature() throws Exception {
TypeDescription typeDescription = describe(SignatureMalformer.malform(MalformedBase.class));
assertThat(typeDescription.getInterfaces().size(), is(1));
typeDescription.getInterfaces().getOnly().getSort();
}
@Test(expected = GenericSignatureFormatError.class)
public void testMalformedFieldSignature() throws Exception {
TypeDescription typeDescription = describe(SignatureMalformer.malform(MalformedBase.class));
assertThat(typeDescription.getDeclaredFields().size(), is(1));
typeDescription.getDeclaredFields().getOnly().getType().getSort();
}
@Test(expected = GenericSignatureFormatError.class)
public void testMalformedMethodSignature() throws Exception {
TypeDescription typeDescription = describe(SignatureMalformer.malform(MalformedBase.class));
assertThat(typeDescription.getDeclaredMethods().filter(isMethod()).size(), is(1));
typeDescription.getDeclaredMethods().filter(isMethod()).getOnly().getReturnType().getSort();
}