Skip to content

Commit

Permalink
https://github.com/manifold-systems/manifold/issues/611
Browse files Browse the repository at this point in the history
- preserve default values in annotation methods for generated stub classes
  • Loading branch information
rsmckinney committed Jul 23, 2024
1 parent 03fc9b9 commit 4c07ef2
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class AbstractSrcMethod<T extends AbstractSrcMethod<T>> extends SrcStatem
private List<SrcType> _throwTypes;
private boolean _isConstructor;
private boolean _isPrimaryConstructor;
private String _defaultValue;

public AbstractSrcMethod( AbstractSrcClass srcClass )
{
Expand Down Expand Up @@ -132,6 +133,15 @@ public T body( String rawText )
return (T)this;
}

public String getDefaultValue()
{
return _defaultValue;
}
public void setDefaultValue( String defaultValue )
{
_defaultValue = defaultValue;
}

public SrcType getReturnType()
{
return _returns;
Expand Down Expand Up @@ -191,6 +201,11 @@ else if( isConstructor() )
}
if( isAbstractMethod() )
{
String defaultValue = getDefaultValue();
if( defaultValue != null )
{
sb.append( " default " ).append( defaultValue );
}
sb.append( ";\n" );
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ private void addMethod( IModule module, SrcClass srcClass, Symbol.MethodSymbol m
Type throwType = thrownTypes.get( i );
srcMethod.addThrowType( makeSrcType( throwType, method, TargetType.THROWS, i ) );
}
if( (method.owner.flags_field & Flags.ANNOTATION) != 0 )
{
Attribute defaultValue = method.getDefaultValue();
if( defaultValue != null )
{
srcMethod.setDefaultValue( defaultValue.toString() );
}
}
String bodyStmt;
if( srcMethod.isConstructor() && !srcClass.isEnum() )
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2019 - Manifold Systems LLC
*
* 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 xyz;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import static xyz.OtherAnno.Local.*;

@Retention( RetentionPolicy.CLASS )
public @interface OtherAnno
{
String value() default "foo";
boolean booleanValue() default true;
int intValue() default 9;
double doubleValue() default 9.9;
String stringValue() default "hi";
Class<?> classValue() default Local.class;
Local enumValue() default B;
InnerAnno annoValue() default @InnerAnno(stringValue = "abc");
boolean[] booleanArrayValue() default {true,false};
int[] intArrayValue() default {1,2};
double[] doubleArrayValue() default {1.2,2.1};
String[] stringArrayValue() default {"a", "b"};
Class[] classArrayValue() default {String.class, int.class};
Local[] enumArrayValue() default {A, B};
InnerAnno[] annoArrayValue() default {@InnerAnno, @InnerAnno(value=7)};


enum Local {A, B, C}

@Retention( RetentionPolicy.CLASS )
@interface InnerAnno
{
int value() default 0;
String stringValue() default "bar";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package abc;

import xyz.OtherAnno;

public class ClassThatUsesOtherAnno
{
// tests that default values exist in generated stub for extended annotation OtherAnno, see MyOtherAnnoExt
@OtherAnno
public void myMethod() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package manifold.extensions.xyz.OtherAnno;

import manifold.ext.rt.api.Extension;

// tests extending annotation classes with annotations
@Extension
@Deprecated
public class MyOtherAnnoExt {
}

0 comments on commit 4c07ef2

Please sign in to comment.