Skip to content

Commit

Permalink
feat(objectionary#627): fix some qulice findings
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Jul 6, 2024
1 parent ee141c3 commit 511fb52
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 80 deletions.
13 changes: 6 additions & 7 deletions src/main/java/org/eolang/jeo/representation/MethodName.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ public final class MethodName {
*/
private static final Base64.Encoder ENCODER = Base64.getEncoder();


/**
* Method name in source code.
* Human-readable method name in source code.
*/
private final String name;

Expand All @@ -61,7 +60,7 @@ public final class MethodName {
* @param encoded Method name and descriptor encoded.
*/
public MethodName(final String encoded) {
this(MethodName.name(encoded), MethodName.descriptor(encoded));
this(MethodName.prefix(encoded), MethodName.suffix(encoded));
}

/**
Expand All @@ -81,7 +80,7 @@ public MethodName(final String name, final String descriptor) {
public String encoded() {
return String.format(
"%s-%s",
this.name(),
this.decoded(),
MethodName.ENCODER.encodeToString(this.descriptor.getBytes(StandardCharsets.UTF_8))
);
}
Expand All @@ -90,7 +89,7 @@ public String encoded() {
* Method name.
* @return Method name.
*/
public String name() {
public String decoded() {
final String res;
if ("<init>".equals(this.name)) {
res = "new";
Expand All @@ -105,7 +104,7 @@ public String name() {
* @param encoded Encoded method name and descriptor.
* @return Method name.
*/
private static String name(final String encoded) {
private static String prefix(final String encoded) {
return encoded.substring(0, encoded.indexOf('-'));
}

Expand All @@ -114,7 +113,7 @@ private static String name(final String encoded) {
* @param encoded Encoded method name and descriptor.
* @return Method descriptor.
*/
private static String descriptor(final String encoded) {
private static String suffix(final String encoded) {
return new String(
MethodName.DECODER.decode(encoded.substring(encoded.indexOf('-') + 1)),
StandardCharsets.UTF_8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,11 @@ MethodVisitor writeMethod(final CustomClassWriter writer, final boolean compute)
this.access,
new JavaName(this.name).decode(),
this.descriptor,
BytecodeMethodProperties.nullIfEmpty(this.signature),
Optional.of(this.signature).filter(s -> !s.isEmpty()).orElse(null),
this.exceptions,
compute
);
this.parameters.write(visitor);
return visitor;
}

/**
* Return null if the text is empty.
* @param text Text.
* @return Text or null.
*/
private static String nullIfEmpty(final String text) {
if (text.isEmpty()) {
return null;
}
return text;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public DirectivesAnnotations() {
* @param name Name.
*/
public DirectivesAnnotations(final String name) {
this(new ArrayList<>(), name);
this(new ArrayList<>(0), name);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ public Iterator<Directive> iterator() {
} else {
final Directives directives = new Directives().add("o")
.attr("base", "tuple")
.attr("star", "")
// .attr("name", "attributes")
;
.attr("star", "");
this.attributes.forEach(directives::append);
result = directives.up().iterator();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public Iterator<Directive> iterator() {
directives.add("o")
.attr("base", "tuple")
.attr("star", "")
.attr("name", String.format("trycatchblocks-%s", this.name.name()));
.attr("name", String.format("trycatchblocks-%s", this.name.decoded()));
this.exceptions.forEach(directives::append);
directives.up();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import com.jcabi.xml.XMLDocument;
import java.util.List;
import java.util.Optional;
import org.eolang.jeo.representation.DefaultVersion;
import org.eolang.jeo.representation.bytecode.BytecodeClassProperties;

Expand Down Expand Up @@ -58,21 +57,6 @@ public XmlClassProperties(final XMLDocument clazz) {
this.clazz = clazz;
}

/**
* Retrieve bytecode 'version'.
* @return Bytecode version.
*/
int version() {
final List<String> version = this.clazz.xpath("./o[@name='version']/text()");
final int result;
if (version.isEmpty()) {
result = new DefaultVersion().bytecode();
} else {
result = new HexString(version.get(0)).decodeAsInt();
}
return result;
}

/**
* Retrieve 'access' modifiers of a class.
* @return Access modifiers.
Expand All @@ -85,12 +69,13 @@ public int access() {
* Retrieve 'signature' of a class.
* @return Signature.
*/
public Optional<String> signature() {
public String signature() {
return this.clazz.xpath("./o[@name='signature']/text()")
.stream()
.map(HexString::new)
.map(HexString::decode)
.findFirst();
.findFirst()
.orElse(null);
}

/**
Expand All @@ -116,6 +101,21 @@ public String[] interfaces() {
.map(HexString::decode).toArray(String[]::new);
}

/**
* Retrieve bytecode 'version'.
* @return Bytecode version.
*/
int version() {
final List<String> version = this.clazz.xpath("./o[@name='version']/text()");
final int result;
if (version.isEmpty()) {
result = new DefaultVersion().bytecode();
} else {
result = new HexString(version.get(0)).decodeAsInt();
}
return result;
}

/**
* Convert to bytecode properties.
* @return Bytecode properties.
Expand All @@ -124,7 +124,7 @@ BytecodeClassProperties toBytecodeProperties() {
return new BytecodeClassProperties(
this.version(),
this.access(),
this.signature().orElse(null),
this.signature(),
this.supername(),
this.interfaces()
);
Expand Down
43 changes: 22 additions & 21 deletions src/main/java/org/eolang/jeo/representation/xmir/XmlMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public XmlMethod(final XmlNode xmlnode) {
* Currently we have some ad-hoc solution for method retrieval.
* The problem is code is a bit cryptic and hard to understand.
* Moreover the logic under method naming is spread around several places like
* inside {@link #name()} method as well as inside {@link MethodName#name()} method.
* inside {@link #name()} method as well as inside {@link MethodName#decoded()} method.
* In other words, it would be great to find sophisticated solution for this problem.
*/
public String name() {
Expand All @@ -142,9 +142,9 @@ public String name() {
} else {
result = original;
}
final int endIndex = result.lastIndexOf('-');
if (endIndex > 0) {
result = result.substring(0, endIndex);
final int dash = result.lastIndexOf('-');
if (dash > 0) {
result = result.substring(0, dash);
}
return result;
}
Expand Down Expand Up @@ -182,20 +182,6 @@ public String signature() {
).decode();
}

/**
* Method exceptions.
*
* @return Exceptions.
*/
private String[] exceptions() {
return this.node.children().collect(Collectors.toList()).get(3)
.children()
.map(XmlNode::text)
.map(HexString::new)
.map(HexString::decode)
.toArray(String[]::new);
}

/**
* Method max stack and locals.
*
Expand All @@ -212,9 +198,10 @@ public Optional<XmlMaxs> maxs() {
*/
public List<XmlTryCatchEntry> trycatchEntries() {
return this.node.children()
.filter(element -> element.attribute("name")
.map(s -> s.contains("trycatchblocks"))
.orElse(false))
.filter(
element -> element.attribute("name")
.map(s -> s.contains("trycatchblocks"))
.orElse(false))
.flatMap(XmlNode::children)
.map(entry -> new XmlTryCatchEntry(entry, this.labels))
.collect(Collectors.toList());
Expand Down Expand Up @@ -408,6 +395,20 @@ private BytecodeParameters params() {
);
}

/**
* Method exceptions.
*
* @return Exceptions.
*/
private String[] exceptions() {
return this.node.children().collect(Collectors.toList()).get(3)
.children()
.map(XmlNode::text)
.map(HexString::new)
.map(HexString::decode)
.toArray(String[]::new);
}

/**
* Create Method XmlNode by directives.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void createsMethodNameUsingMainConstructor() {
final String name = "foo";
MatcherAssert.assertThat(
"Method name shouldn't be changed after creation",
new MethodName(name, "()I").name(),
new MethodName(name, "()I").decoded(),
Matchers.equalTo(name)
);
}
Expand All @@ -47,7 +47,7 @@ void createsMethodNameUsingMainConstructor() {
void createsConstructorMethodName() {
MatcherAssert.assertThat(
"Constructor name should be changed after creation from '<init>' to 'new'",
new MethodName("<init>", "()I").name(),
new MethodName("<init>", "()I").decoded(),
Matchers.equalTo("new")
);
}
Expand All @@ -65,7 +65,7 @@ void encodesMethodNameAndDescriptor() {
void decodesMethodName() {
MatcherAssert.assertThat(
"Decoded method name should be correct",
new MethodName("foo-KCgpSUk=").name(),
new MethodName("foo-KCgpSUk=").decoded(),
Matchers.equalTo("foo")
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void appendsMethod() {
new XMLDocument(xml)
),
xml,
XhtmlMatchers.hasXPath("/o[@name='Neo']/o[@name='method']")
XhtmlMatchers.hasXPath("/o[@name='Neo']/o[contains(@name,'method')]")
);
}

Expand Down Expand Up @@ -134,7 +134,7 @@ void correctlyConvertsToDirectives() throws ImpossibleModificationException {
);
MatcherAssert.assertThat(
"Class signature is not equal to expected",
clazz.properties().signature().get(),
clazz.properties().signature(),
Matchers.equalTo(signature)
);
MatcherAssert.assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.eolang.jeo.representation.directives;

import com.jcabi.matchers.XhtmlMatchers;
import org.eolang.jeo.representation.xmir.XmlFrame;
import org.eolang.jeo.representation.xmir.XmlNode;
import org.hamcrest.MatcherAssert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
package org.eolang.jeo.representation.xmir;

import java.util.Arrays;
import java.util.Optional;
import org.eolang.jeo.representation.directives.DirectivesClassProperties;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
Expand Down Expand Up @@ -58,21 +57,16 @@ void retrievesAccessModifier() {
@Test
void retrievesSignature() {
final String expected = "Ljava/util/List<Ljava/lang/String;>;";
final Optional<String> actual = new XmlClass(
final String actual = new XmlClass(
new DirectivesClassProperties(0, expected)
).properties().signature();
MatcherAssert.assertThat(
String.format("Signature is not present, expected %s", expected),
actual.isPresent(),
Matchers.is(true)
);
MatcherAssert.assertThat(
String.format(
"Can't retrieve signature correctly, expected %s, got %s",
expected,
actual.get()
actual
),
actual.get(),
actual,
Matchers.is(expected)
);
}
Expand Down

0 comments on commit 511fb52

Please sign in to comment.