Skip to content

Commit

Permalink
feat(objectionary#534): assemble annotation default values
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Apr 2, 2024
1 parent 451292b commit 62398cb
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public BytecodeAnnotation(
*/
public BytecodeAnnotation write(final ClassVisitor visitor) {
final AnnotationVisitor avisitor = visitor.visitAnnotation(this.descriptor, this.visible);
this.properties.forEach(property -> property.write(avisitor));
this.properties.forEach(property -> property.writeTo(avisitor));
return this;
}

Expand All @@ -99,7 +99,7 @@ public BytecodeAnnotation write(final ClassVisitor visitor) {
*/
public BytecodeAnnotation write(final MethodVisitor visitor) {
final AnnotationVisitor avisitor = visitor.visitAnnotation(this.descriptor, this.visible);
this.properties.forEach(property -> property.write(avisitor));
this.properties.forEach(property -> property.writeTo(avisitor));
return this;
}

Expand All @@ -110,13 +110,13 @@ public BytecodeAnnotation write(final MethodVisitor visitor) {
*/
public BytecodeAnnotation write(final FieldVisitor visitor) {
final AnnotationVisitor avisitor = visitor.visitAnnotation(this.descriptor, this.visible);
this.properties.forEach(property -> property.write(avisitor));
this.properties.forEach(property -> property.writeTo(avisitor));
return this;
}

@Override
public void write(final AnnotationVisitor visitor) {
public void writeTo(final AnnotationVisitor visitor) {
final AnnotationVisitor inner = visitor.visitAnnotation(this.descriptor, this.descriptor);
this.properties.forEach(property -> property.write(inner));
this.properties.forEach(property -> property.writeTo(inner));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static BytecodeAnnotationProperty byType(final String type, final List<Ob
}

@Override
public void write(final AnnotationVisitor avisitor) {
public void writeTo(final AnnotationVisitor avisitor) {
switch (this.type) {
case PLAIN:
avisitor.visit((String) this.params.get(0), this.params.get(1));
Expand All @@ -108,7 +108,7 @@ public void write(final AnnotationVisitor avisitor) {
Optional.ofNullable(this.params.get(0)).map(String.class::cast).orElse(null)
);
for (final Object param : this.params.subList(1, this.params.size())) {
((BytecodeAnnotationValue) param).write(array);
((BytecodeAnnotationValue) param).writeTo(array);
}
array.visitEnd();
break;
Expand All @@ -118,7 +118,7 @@ public void write(final AnnotationVisitor avisitor) {
(String) this.params.get(1)
);
for (final Object param : this.params.subList(2, this.params.size())) {
((BytecodeAnnotationValue) param).write(annotation);
((BytecodeAnnotationValue) param).writeTo(annotation);
}
annotation.visitEnd();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ public interface BytecodeAnnotationValue {
* Write the value to the given visitor.
* @param visitor Visitor.
*/
void write(AnnotationVisitor visitor);
void writeTo(AnnotationVisitor visitor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.jeo.representation.bytecode;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.MethodVisitor;

/**
* Bytecode default value.
*
* @since 0.3
*/
public final class BytecodeDefaultValue {

/**
* Annotation property as a value.
*/
private final BytecodeAnnotationProperty property;

/**
* Constructor.
* @param property Annotation property as a value.
*/
public BytecodeDefaultValue(final BytecodeAnnotationProperty property) {
this.property = property;
}

/**
* Write the default value to the given visitor.
* @param mvisitor Visitor.
*/
public void writeTo(final MethodVisitor mvisitor) {
final AnnotationVisitor visitor = mvisitor.visitAnnotationDefault();
this.property.writeTo(visitor);
visitor.visitEnd();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public final class BytecodeMethod implements Testable {
*/
private final BytecodeMethodProperties properties;

/**
* Default value.
*/
private final List<BytecodeDefaultValue> defvalues;

/**
* Stack size.
*/
Expand Down Expand Up @@ -131,6 +136,7 @@ public BytecodeMethod(
this.tryblocks = new ArrayList<>(0);
this.instructions = new ArrayList<>(0);
this.annotations = new ArrayList<>(0);
this.defvalues = new ArrayList<>(0);
this.stack = stack;
this.locals = locals;
}
Expand Down Expand Up @@ -203,6 +209,16 @@ public BytecodeMethod annotation(final BytecodeAnnotation annotation) {
return this;
}

/**
* Add default value.
* @param defvalue Default value.
* @return This object.
*/
public BytecodeMethod defvalue(final BytecodeDefaultValue defvalue) {
this.defvalues.add(defvalue);
return this;
}

@Override
@SuppressWarnings("PMD.InsufficientStringBufferDeclaration")
public String testCode() {
Expand All @@ -229,6 +245,7 @@ void write() {
try {
final MethodVisitor mvisitor = this.properties.writeMethod(this.visitor);
this.annotations.forEach(annotation -> annotation.write(mvisitor));
this.defvalues.forEach(defvalue -> defvalue.writeTo(mvisitor));
if (!this.properties.isAbstract()) {
mvisitor.visitCode();
this.tryblocks.forEach(block -> block.writeTo(mvisitor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public Bytecode bytecode() {
xmlmethod.annotations().forEach(method::annotation);
xmlmethod.instructions().forEach(inst -> inst.writeTo(method));
xmlmethod.trycatchEntries().forEach(exc -> exc.writeTo(method));
xmlmethod.defvalue().ifPresent(defv -> defv.writeTo(method));
}
return bytecode.bytecode();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2023 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eolang.jeo.representation.xmir;

import org.eolang.jeo.representation.bytecode.BytecodeDefaultValue;
import org.eolang.jeo.representation.bytecode.BytecodeMethod;

/**
* XMIR of annotation default value.
*
* @since 0.3
*/
public final class XmlDefaultValue {

/**
* Default value XMIR node.
*/
private final XmlNode node;

/**
* Constructor.
* @param node Default value XMIR node.
*/
public XmlDefaultValue(final XmlNode node) {
this.node = node;
}

/**
* Write to method.
* @param method Method.
*/
public void writeTo(final BytecodeMethod method) {
this.node.children().findFirst().ifPresent(
property -> method.defvalue(
new BytecodeDefaultValue(
new XmlAnnotationProperty(property).toBytecode()
)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ public void replaceInstructions(final XmlNode... entries) {
}
}

/**
* Annotation default value.
* @return Optional XMIR of the default value.
*/
public Optional<XmlDefaultValue> defvalue() {
return this.node.optchild("base", "annotation-default-value")
.map(XmlDefaultValue::new);
}

/**
* Method exceptions.
* @return Exceptions.
Expand Down

0 comments on commit 62398cb

Please sign in to comment.