Skip to content

Commit

Permalink
feat(objectionary#636): fix all the code offences
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Sep 9, 2024
1 parent b76d331 commit 41d5420
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 161 deletions.
194 changes: 90 additions & 104 deletions src/main/java/org/eolang/jeo/representation/bytecode/BytecodeClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.eolang.jeo.representation.ClassName;
import org.eolang.jeo.representation.xmir.XmlAnnotations;
import org.objectweb.asm.Opcodes;

/**
Expand Down Expand Up @@ -157,53 +156,8 @@ public BytecodeClass(
this.props = props;
}

/**
* Constructor.
* @return Class name.
*/
public String name() {
return this.name;
}

/**
* Constructor.
* @param visitor Writer.
*/
void writeTo(final CustomClassWriter visitor, final String pckg) {
try {
visitor.visit(
this.props.version(),
this.props.access(),
new ClassName(pckg, this.name).full(),
this.props.signature(),
this.props.supername(),
this.props.interfaces()
);
this.annotations.forEach(annotation -> annotation.write(visitor));
this.fields.forEach(field -> field.write(visitor));
this.methods.forEach(method -> method.write(visitor));
this.attributes.forEach(attr -> attr.write(visitor));
visitor.visitEnd();
} catch (final IllegalArgumentException exception) {
throw new IllegalArgumentException(
String.format("Can't create bytecode for the class '%s' ", this.name),
exception
);
} catch (final IllegalStateException exception) {
throw new IllegalStateException(
String.format(
"Bytecode creation for the '%s' class is not possible due to unmet preconditions. To reproduce the problem, you can write the following test: %n%s%n",
this.name,
this.testCode()
),
exception
);
}
}

/**
* Add constructor.
*
* @param modifiers Constructor modifiers.
* @return This object.
*/
Expand All @@ -213,7 +167,6 @@ public BytecodeMethodBuilder withConstructor(final int... modifiers) {

/**
* Add method.
*
* @param properties Method properties.
* @return This object.
*/
Expand All @@ -223,20 +176,18 @@ public BytecodeMethodBuilder withMethod(final BytecodeMethodProperties propertie

/**
* Add method.
*
* @param props Method properties.
* @param properties Method properties.
* @param maxs Method maxs.
* @return This object.
*/
public BytecodeMethodBuilder withMethod(
final BytecodeMethodProperties props, final BytecodeMaxs maxs
final BytecodeMethodProperties properties, final BytecodeMaxs maxs
) {
return this.withMethod(new BytecodeMethod(props, maxs));
return this.withMethod(new BytecodeMethod(properties, maxs));
}

/**
* Add constructor.
*
* @param descriptor Constructor descriptor.
* @param modifiers Constructor modifiers.
* @return This object.
Expand All @@ -245,33 +196,8 @@ public BytecodeMethodBuilder withConstructor(final String descriptor, final int.
return this.withMethod("<init>", descriptor, modifiers);
}

/**
* Add method.
*
* @param mname Method name.
* @param descriptor Method descriptor.
* @param modifiers Access modifiers.
* @return This object.
*/
public BytecodeMethodBuilder withMethod(
final String mname, final String descriptor, final int... modifiers
) {
return this.withMethod(new BytecodeMethod(mname, descriptor, modifiers));
}

/**
* Add method.
* @param method Method.
* @return This object.
*/
private BytecodeMethodBuilder withMethod(final BytecodeMethod method) {
this.methods.add(method);
return new BytecodeMethodBuilder(this, method);
}

/**
* Add field.
*
* @param fname Field name.
* @return This object.
*/
Expand All @@ -287,40 +213,26 @@ public BytecodeClass withField(final String fname) {
}

/**
* Add attribute.
* @param attribute Attribute.
* Add method.
* @param mname Method name.
* @param descriptor Method descriptor.
* @param modifiers Access modifiers.
* @return This object.
*/
public BytecodeClass withAttribute(final BytecodeAttribute attribute) {
this.attributes.add(attribute);
return this;
public BytecodeMethodBuilder withMethod(
final String mname, final String descriptor, final int... modifiers
) {
return this.withMethod(new BytecodeMethod(mname, descriptor, modifiers));
}

/**
* Add field.
*
* @param fname Field name.
* @param descriptor Field descriptor.
* @param signature Field signature.
* @param value Field value.
* @param modifiers Access modifiers.
* Add attribute.
* @param attribute Attribute.
* @return This object.
* @checkstyle ParameterNumberCheck (5 lines)
*/
private BytecodeField withField(
final String fname,
final String descriptor,
final String signature,
final Object value,
final int... modifiers
) {
int access = 0;
for (final int modifier : modifiers) {
access |= modifier;
}
final BytecodeField field = new BytecodeField(fname, descriptor, signature, value, access);
this.fields.add(field);
return field;
public BytecodeClass withAttribute(final BytecodeAttribute attribute) {
this.attributes.add(attribute);
return this;
}

@Override
Expand Down Expand Up @@ -378,4 +290,78 @@ public BytecodeClass withoutMethods() {
this.methods.clear();
return this;
}

/**
* Constructor.
* @param visitor Writer.
* @param pckg Package.
*/
void writeTo(final CustomClassWriter visitor, final String pckg) {
try {
visitor.visit(
this.props.version(),
this.props.access(),
new ClassName(pckg, this.name).full(),
this.props.signature(),
this.props.supername(),
this.props.interfaces()
);
this.annotations.forEach(annotation -> annotation.write(visitor));
this.fields.forEach(field -> field.write(visitor));
this.methods.forEach(method -> method.write(visitor));
this.attributes.forEach(attr -> attr.write(visitor));
visitor.visitEnd();
} catch (final IllegalArgumentException exception) {
throw new IllegalArgumentException(
String.format("Can't create bytecode for the class '%s' ", this.name),
exception
);
} catch (final IllegalStateException exception) {
throw new IllegalStateException(
String.format(
"Bytecode creation for the '%s' class is not possible due to unmet preconditions. To reproduce the problem, you can write the following test: %n%s%n",
this.name,
this.testCode()
),
exception
);
}
}

/**
* Add method.
* @param method Method.
* @return This object.
*/
private BytecodeMethodBuilder withMethod(final BytecodeMethod method) {
this.methods.add(method);
return new BytecodeMethodBuilder(this, method);
}

/**
* Add field.
*
* @param fname Field name.
* @param descriptor Field descriptor.
* @param signature Field signature.
* @param value Field value.
* @param modifiers Access modifiers.
* @return This object.
* @checkstyle ParameterNumberCheck (5 lines)
*/
private BytecodeField withField(
final String fname,
final String descriptor,
final String signature,
final Object value,
final int... modifiers
) {
int access = 0;
for (final int modifier : modifiers) {
access |= modifier;
}
final BytecodeField field = new BytecodeField(fname, descriptor, signature, value, access);
this.fields.add(field);
return field;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.eolang.jeo.representation.xmir.AllLabels;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
Expand Down Expand Up @@ -242,6 +241,7 @@ public String testCode() {

/**
* Generate bytecode.
* @param visitor Visitor.
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
void write(final CustomClassWriter visitor) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
/*
* 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.Label;

/**
* Bytecode method builder.
* @since 0.6
*/
public final class BytecodeMethodBuilder {

/**
* Class for the method.
*/
private final BytecodeClass clazz;

/**
* Bytecode method.
*/
private final BytecodeMethod method;

/**
* Constructor.
* @param clazz Class.
* @param method Method.
*/
public BytecodeMethodBuilder(final BytecodeClass clazz, final BytecodeMethod method) {
this.clazz = clazz;
this.method = method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public BytecodeProgram(final String pckg, final List<BytecodeClass> classes) {
* @return XML representation of bytecode.
*/
public XML xml() {
return new BytecodeRepresentation(this.bytecode(true)).toEO();
return new BytecodeRepresentation(this.bytecode()).toEO();
}

/**
Expand All @@ -116,6 +116,7 @@ public Bytecode bytecode() {

/**
* Traverse XML and build bytecode class.
* @param verify Verify bytecode.
* @return Bytecode.
*/
public Bytecode bytecode(final boolean verify) {
Expand All @@ -124,6 +125,10 @@ public Bytecode bytecode(final boolean verify) {
return writer.bytecode();
}

/**
* Get top class.
* @return Top class.
*/
public BytecodeClass top() {
return this.classes.get(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ public Optional<BytecodeAttribute> attribute() {
String.format("Attribute base is missing in XML node %s", this.node)
)
);
final Optional<BytecodeAttribute> result;
if ("inner-class".equals(base)) {
return Optional.of(
result = Optional.of(
new BytecodeAttribute.InnerClass(
Optional.ofNullable(this.node.children().collect(Collectors.toList()).get(0))
.map(XmlOperand::new)
Expand All @@ -90,8 +91,10 @@ public Optional<BytecodeAttribute> attribute() {
.map(Integer.class::cast)
.orElse(0)
));
} else {
result = Optional.empty();
}
return Optional.empty();
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eolang.jeo.representation.bytecode.BytecodeClass;
import org.eolang.jeo.representation.directives.DirectivesClass;
import org.eolang.jeo.representation.directives.DirectivesClassProperties;
import org.objectweb.asm.Opcodes;
import org.w3c.dom.Node;
import org.xembly.Transformers;
import org.xembly.Xembler;
Expand Down Expand Up @@ -186,7 +187,7 @@ private Optional<XmlAttributes> attributes() {
* @return Class node.
*/
private static Node empty(final String classname) {
return XmlClass.withProps(classname, new DirectivesClassProperties(0));
return XmlClass.withProps(classname, new DirectivesClassProperties(Opcodes.ACC_PUBLIC));
}

/**
Expand Down
Loading

0 comments on commit 41d5420

Please sign in to comment.