Skip to content

Commit

Permalink
paul-hammant#75 CodeBlock for Annotation with FieldReference should p…
Browse files Browse the repository at this point in the history
…refix field with canonical name
  • Loading branch information
rfscholte committed Oct 23, 2021
1 parent c771d7c commit b98e628
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
41 changes: 39 additions & 2 deletions src/main/java/com/thoughtworks/qdox/model/expression/FieldRef.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thoughtworks.qdox.model.expression;

import java.util.List;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand Down Expand Up @@ -94,7 +96,15 @@ public String getParameterValue()
@Override
public String toString()
{
return getName();
JavaField field = getField();
if ( field != null && !getDeclaringClass().equals( field.getDeclaringClass() ) )
{
return field.getDeclaringClass().getCanonicalName() + "." + field.getName();
}
else
{
return name;
}
}

public void setDeclaringClass( JavaClass declaringClass )
Expand Down Expand Up @@ -139,7 +149,7 @@ protected JavaField resolveField( JavaClass javaClass, int start, int end )
{
field = javaClass.getFieldByName( getNamePart( i ) );

if ( field == null )
if ( field != null )
{
break;
}
Expand Down Expand Up @@ -178,6 +188,33 @@ public JavaField getField()
}
}
}

if ( field == null )
{
ClassLibrary classLibrary = getClassLibrary();
if ( classLibrary != null )
{
List<String> imports = getDeclaringClass().getSource().getImports();
for ( String i : imports )
{
if ( i.startsWith( "static" ) )
{
String member = i.substring( i.lastIndexOf( '.' ) + 1 );
if ( "*".equals( member ) || getNamePrefix( 0 ).equals( member ) )
{
String className = i.substring( 7, i.lastIndexOf( '.' ) ).trim();
JavaClass javaClass = classLibrary.getJavaClass( className );
JavaField tmpField = javaClass.getFieldByName( member );
if ( tmpField != null && ( javaClass.isInterface() || tmpField.isStatic() ) )
{
field = tmpField;
break;
}
}
}
}
}
}
}
return field;
}
Expand Down
19 changes: 18 additions & 1 deletion src/test/java/com/thoughtworks/qdox/JavaProjectBuilderTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.thoughtworks.qdox;

import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.same;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -1670,6 +1669,24 @@ public void testGetInterface()
assertNotNull(clss);
}

// github #75
public void testAnnotationWithConstant() {
String constantSource = "package com.xenoamess;\n"
+ "public interface Constants {\n"
+ " public String STATIC_STRING_A=\"SOME_VALUE\";" + "}";
builder.addSource( new StringReader( constantSource ) );

String source = "import static com.xenoamess.Constants.STATIC_STRING_A;\n"
+ "\n" + "public class ClassA{\n"
+ " @AnnotationA(annotationFieldA = STATIC_STRING_A)\n"
+ " public void functionA(){\n" + " }\n"
+ "}";
JavaMethod method =
builder.addSource( new StringReader( source ) ).getClassByName( "ClassA" ).getMethods().get( 0 );
assertEquals( "@AnnotationA(annotationFieldA=com.xenoamess.Constants.STATIC_STRING_A)\n",
method.getAnnotations().get( 0 ).getCodeBlock() );
}

// Github #76
public void testAtInComment() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,10 @@ public void testVisitFieldRef()
}

JavaField nonStaticNonFinalfield = mock( JavaField.class );

JavaClass declaringClass = mock( JavaClass.class );
when( declaringClass.getFieldByName( "fieldname" ) ).thenReturn( nonStaticNonFinalfield );
when( nonStaticNonFinalfield.getDeclaringClass() ).thenReturn( declaringClass );

JavaClass annotatedClass = mock( JavaClass.class );
ref.setDeclaringClass( declaringClass );
try
{
Expand Down

0 comments on commit b98e628

Please sign in to comment.