Skip to content

Commit

Permalink
[Blazebit#1422] Fix support for non-public @PostLoad methods in entit…
Browse files Browse the repository at this point in the history
…y view annotation processor
  • Loading branch information
beikov committed Jan 16, 2022
1 parent 7ca54bc commit 8a50d75
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Not yet released
### Bug fixes

* Fix dev UI support for Quarkus 2
* Fix support for non-public `@PostLoad` methods in entity view annotation processor

### Backwards-incompatible changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1874,15 +1874,15 @@ private static boolean preparePostLoad(StringBuilder sb, MetaEntityView entity,
sb.append(" private static final ").append(entity.implementationImportType(Method.class.getName())).append(" $$_post_load;").append(NEW_LINE);
sb.append(" static {").append(NEW_LINE);
sb.append(" try {").append(NEW_LINE);
sb.append(" Method m = ").append(entity.implementationImportType(declaringType.getQualifiedName().toString())).append(".class.getDeclaredMethod(\"").append(entity.getPostCreate().getSimpleName()).append("\"");
sb.append(" Method m = ").append(entity.implementationImportType(declaringType.getQualifiedName().toString())).append(".class.getDeclaredMethod(\"").append(entity.getPostLoad().getSimpleName()).append("\"");
if (!entity.getPostLoad().getParameters().isEmpty()) {
for (VariableElement parameter : entity.getPostLoad().getParameters()) {
sb.append(", ").append(entity.implementationImportType(parameter.asType().toString())).append(".class");
}
}
sb.append(");").append(NEW_LINE);
sb.append(" m.setAccessible(true);").append(NEW_LINE);
sb.append(" $$_post_create = m;").append(NEW_LINE);
sb.append(" $$_post_load = m;").append(NEW_LINE);
sb.append(" } catch (Exception ex) {").append(NEW_LINE);
sb.append(" throw new RuntimeException(\"Could not initialize post construct accessor!\", ex);").append(NEW_LINE);
sb.append(" }").append(NEW_LINE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void testAbstractClass() throws Exception {
Assert.assertEquals(1, obj.getId());
Assert.assertEquals("Test", obj.getName());
Assert.assertEquals("Test", obj.getCapturedName());
Assert.assertEquals("Test", obj.getPostLoadName());
}

private Compilation test(Class<?>... views) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.blazebit.persistence.view.processor.model;

import com.blazebit.persistence.view.EntityView;
import com.blazebit.persistence.view.PostLoad;
import com.blazebit.persistence.view.Self;
import com.blazebit.persistence.view.ViewConstructor;
import com.blazebit.persistence.view.processor.model.sub.BaseView;
Expand All @@ -11,6 +12,7 @@
public abstract class BView<X extends Serializable> extends BaseView<Integer> {

private final String capturedName;
private String postLoadName;

@ViewConstructor("create")
public BView() {
Expand All @@ -21,11 +23,20 @@ public BView(@Self BView self) {
this.capturedName = self == null ? null : self.getName();
}

@PostLoad
void postLoad() {
this.postLoadName = getName();
}

public abstract String getName();

public abstract void setName(String name);

public String getCapturedName() {
return capturedName;
}

public String getPostLoadName() {
return postLoadName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.blazebit.persistence.view.spi.type.EntityViewProxy;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Generated;
Expand All @@ -22,6 +23,16 @@ public class BViewImpl<X extends Serializable> extends BaseView_com_blazebit_per
private String name;
private Integer parent;

private static final Method $$_post_load;
static {
try {
Method m = BView.class.getDeclaredMethod("postLoad");
m.setAccessible(true);
$$_post_load = m;
} catch (Exception ex) {
throw new RuntimeException("Could not initialize post construct accessor!", ex);
}
}
public BViewImpl(BViewImpl noop, Map<String, Object> optionalParameters) {
this.id = null;
if (this.name == null) {
Expand Down Expand Up @@ -56,6 +67,11 @@ public BViewImpl(
if (this.parent == null) {
this.parent = parent;
}
try {
$$_post_load.invoke(this);
} catch (Exception ex) {
throw new RuntimeException("Could not invoke post load method", ex);
}
}

public BViewImpl(BViewImpl noop, int offset, Object[] tuple) {
Expand All @@ -67,6 +83,11 @@ public BViewImpl(BViewImpl noop, int offset, Object[] tuple) {
if (this.parent == null) {
this.parent = (Integer) tuple[offset + 2];
}
try {
$$_post_load.invoke(this);
} catch (Exception ex) {
throw new RuntimeException("Could not invoke post load method", ex);
}
}

public BViewImpl(BViewImpl noop, int offset, int[] assignment, Object[] tuple) {
Expand All @@ -78,6 +99,11 @@ public BViewImpl(BViewImpl noop, int offset, int[] assignment, Object[] tuple) {
if (this.parent == null) {
this.parent = (Integer) tuple[offset + assignment[2]];
}
try {
$$_post_load.invoke(this);
} catch (Exception ex) {
throw new RuntimeException("Could not invoke post load method", ex);
}
}

public BViewImpl(
Expand All @@ -96,6 +122,11 @@ public BViewImpl(
if (this.parent == null) {
this.parent = parent;
}
try {
$$_post_load.invoke(this);
} catch (Exception ex) {
throw new RuntimeException("Could not invoke post load method", ex);
}
}

public BViewImpl(
Expand All @@ -118,6 +149,11 @@ public BViewImpl(
if (this.parent == null) {
this.parent = (Integer) tuple[offset + 2];
}
try {
$$_post_load.invoke(this);
} catch (Exception ex) {
throw new RuntimeException("Could not invoke post load method", ex);
}
}

public BViewImpl(
Expand All @@ -141,6 +177,11 @@ public BViewImpl(
if (this.parent == null) {
this.parent = (Integer) tuple[offset + assignment[2]];
}
try {
$$_post_load.invoke(this);
} catch (Exception ex) {
throw new RuntimeException("Could not invoke post load method", ex);
}
}

@Override
Expand Down Expand Up @@ -248,7 +289,7 @@ public static BView createSelf(
private static class BViewSer<X extends Serializable> extends BaseView_com_blazebit_persistence_view_processor_model_BView<java.lang.Integer, X> implements Serializable {

private static final long serialVersionUID = 1L;
private static final byte[] EMPTY_INSTANCE_BYTES = new byte[]{ (byte) 0xAC, (byte) 0xED, (byte) 0x00, (byte) 0x05, (byte) 0x73, (byte) 0x72, (byte) 0x00, (byte) 0x40, (byte) 0x63, (byte) 0x6F, (byte) 0x6D, (byte) 0x2E, (byte) 0x62, (byte) 0x6C, (byte) 0x61, (byte) 0x7A, (byte) 0x65, (byte) 0x62, (byte) 0x69, (byte) 0x74, (byte) 0x2E, (byte) 0x70, (byte) 0x65, (byte) 0x72, (byte) 0x73, (byte) 0x69, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x6E, (byte) 0x63, (byte) 0x65, (byte) 0x2E, (byte) 0x76, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x2E, (byte) 0x70, (byte) 0x72, (byte) 0x6F, (byte) 0x63, (byte) 0x65, (byte) 0x73, (byte) 0x73, (byte) 0x6F, (byte) 0x72, (byte) 0x2E, (byte) 0x6D, (byte) 0x6F, (byte) 0x64, (byte) 0x65, (byte) 0x6C, (byte) 0x2E, (byte) 0x42, (byte) 0x56, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x49, (byte) 0x6D, (byte) 0x70, (byte) 0x6C, (byte) 0x24, (byte) 0x42, (byte) 0x56, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x53, (byte) 0x65, (byte) 0x72, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x00, (byte) 0x03, (byte) 0x4C, (byte) 0x00, (byte) 0x02, (byte) 0x69, (byte) 0x64, (byte) 0x74, (byte) 0x00, (byte) 0x16, (byte) 0x4C, (byte) 0x6A, (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2F, (byte) 0x69, (byte) 0x6F, (byte) 0x2F, (byte) 0x53, (byte) 0x65, (byte) 0x72, (byte) 0x69, (byte) 0x61, (byte) 0x6C, (byte) 0x69, (byte) 0x7A, (byte) 0x61, (byte) 0x62, (byte) 0x6C, (byte) 0x65, (byte) 0x3B, (byte) 0x4C, (byte) 0x00, (byte) 0x04, (byte) 0x6E, (byte) 0x61, (byte) 0x6D, (byte) 0x65, (byte) 0x74, (byte) 0x00, (byte) 0x12, (byte) 0x4C, (byte) 0x6A, (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2F, (byte) 0x6C, (byte) 0x61, (byte) 0x6E, (byte) 0x67, (byte) 0x2F, (byte) 0x53, (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6E, (byte) 0x67, (byte) 0x3B, (byte) 0x4C, (byte) 0x00, (byte) 0x06, (byte) 0x70, (byte) 0x61, (byte) 0x72, (byte) 0x65, (byte) 0x6E, (byte) 0x74, (byte) 0x74, (byte) 0x00, (byte) 0x16, (byte) 0x4C, (byte) 0x6A, (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2F, (byte) 0x69, (byte) 0x6F, (byte) 0x2F, (byte) 0x53, (byte) 0x65, (byte) 0x72, (byte) 0x69, (byte) 0x61, (byte) 0x6C, (byte) 0x69, (byte) 0x7A, (byte) 0x61, (byte) 0x62, (byte) 0x6C, (byte) 0x65, (byte) 0x3B, (byte) 0x78, (byte) 0x72, (byte) 0x00, (byte) 0x33, (byte) 0x63, (byte) 0x6F, (byte) 0x6D, (byte) 0x2E, (byte) 0x62, (byte) 0x6C, (byte) 0x61, (byte) 0x7A, (byte) 0x65, (byte) 0x62, (byte) 0x69, (byte) 0x74, (byte) 0x2E, (byte) 0x70, (byte) 0x65, (byte) 0x72, (byte) 0x73, (byte) 0x69, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x6E, (byte) 0x63, (byte) 0x65, (byte) 0x2E, (byte) 0x76, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x2E, (byte) 0x70, (byte) 0x72, (byte) 0x6F, (byte) 0x63, (byte) 0x65, (byte) 0x73, (byte) 0x73, (byte) 0x6F, (byte) 0x72, (byte) 0x2E, (byte) 0x6D, (byte) 0x6F, (byte) 0x64, (byte) 0x65, (byte) 0x6C, (byte) 0x2E, (byte) 0x42, (byte) 0x56, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0xF4, (byte) 0x2F, (byte) 0x62, (byte) 0x48, (byte) 0xFA, (byte) 0x77, (byte) 0x2D, (byte) 0x22, (byte) 0x02, (byte) 0x00, (byte) 0x01, (byte) 0x4C, (byte) 0x00, (byte) 0x0C, (byte) 0x63, (byte) 0x61, (byte) 0x70, (byte) 0x74, (byte) 0x75, (byte) 0x72, (byte) 0x65, (byte) 0x64, (byte) 0x4E, (byte) 0x61, (byte) 0x6D, (byte) 0x65, (byte) 0x74, (byte) 0x00, (byte) 0x12, (byte) 0x4C, (byte) 0x6A, (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2F, (byte) 0x6C, (byte) 0x61, (byte) 0x6E, (byte) 0x67, (byte) 0x2F, (byte) 0x53, (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6E, (byte) 0x67, (byte) 0x3B, (byte) 0x78, (byte) 0x72, (byte) 0x00, (byte) 0x3A, (byte) 0x63, (byte) 0x6F, (byte) 0x6D, (byte) 0x2E, (byte) 0x62, (byte) 0x6C, (byte) 0x61, (byte) 0x7A, (byte) 0x65, (byte) 0x62, (byte) 0x69, (byte) 0x74, (byte) 0x2E, (byte) 0x70, (byte) 0x65, (byte) 0x72, (byte) 0x73, (byte) 0x69, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x6E, (byte) 0x63, (byte) 0x65, (byte) 0x2E, (byte) 0x76, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x2E, (byte) 0x70, (byte) 0x72, (byte) 0x6F, (byte) 0x63, (byte) 0x65, (byte) 0x73, (byte) 0x73, (byte) 0x6F, (byte) 0x72, (byte) 0x2E, (byte) 0x6D, (byte) 0x6F, (byte) 0x64, (byte) 0x65, (byte) 0x6C, (byte) 0x2E, (byte) 0x73, (byte) 0x75, (byte) 0x62, (byte) 0x2E, (byte) 0x42, (byte) 0x61, (byte) 0x73, (byte) 0x65, (byte) 0x56, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x45, (byte) 0x82, (byte) 0xA1, (byte) 0xAC, (byte) 0x40, (byte) 0x09, (byte) 0xDC, (byte) 0x93, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x78, (byte) 0x70, (byte) 0x70, (byte) 0x70, (byte) 0x70, (byte) 0x70};
private static final byte[] EMPTY_INSTANCE_BYTES = new byte[]{ (byte) 0xAC, (byte) 0xED, (byte) 0x00, (byte) 0x05, (byte) 0x73, (byte) 0x72, (byte) 0x00, (byte) 0x40, (byte) 0x63, (byte) 0x6F, (byte) 0x6D, (byte) 0x2E, (byte) 0x62, (byte) 0x6C, (byte) 0x61, (byte) 0x7A, (byte) 0x65, (byte) 0x62, (byte) 0x69, (byte) 0x74, (byte) 0x2E, (byte) 0x70, (byte) 0x65, (byte) 0x72, (byte) 0x73, (byte) 0x69, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x6E, (byte) 0x63, (byte) 0x65, (byte) 0x2E, (byte) 0x76, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x2E, (byte) 0x70, (byte) 0x72, (byte) 0x6F, (byte) 0x63, (byte) 0x65, (byte) 0x73, (byte) 0x73, (byte) 0x6F, (byte) 0x72, (byte) 0x2E, (byte) 0x6D, (byte) 0x6F, (byte) 0x64, (byte) 0x65, (byte) 0x6C, (byte) 0x2E, (byte) 0x42, (byte) 0x56, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x49, (byte) 0x6D, (byte) 0x70, (byte) 0x6C, (byte) 0x24, (byte) 0x42, (byte) 0x56, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x53, (byte) 0x65, (byte) 0x72, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x00, (byte) 0x03, (byte) 0x4C, (byte) 0x00, (byte) 0x02, (byte) 0x69, (byte) 0x64, (byte) 0x74, (byte) 0x00, (byte) 0x16, (byte) 0x4C, (byte) 0x6A, (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2F, (byte) 0x69, (byte) 0x6F, (byte) 0x2F, (byte) 0x53, (byte) 0x65, (byte) 0x72, (byte) 0x69, (byte) 0x61, (byte) 0x6C, (byte) 0x69, (byte) 0x7A, (byte) 0x61, (byte) 0x62, (byte) 0x6C, (byte) 0x65, (byte) 0x3B, (byte) 0x4C, (byte) 0x00, (byte) 0x04, (byte) 0x6E, (byte) 0x61, (byte) 0x6D, (byte) 0x65, (byte) 0x74, (byte) 0x00, (byte) 0x12, (byte) 0x4C, (byte) 0x6A, (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2F, (byte) 0x6C, (byte) 0x61, (byte) 0x6E, (byte) 0x67, (byte) 0x2F, (byte) 0x53, (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6E, (byte) 0x67, (byte) 0x3B, (byte) 0x4C, (byte) 0x00, (byte) 0x06, (byte) 0x70, (byte) 0x61, (byte) 0x72, (byte) 0x65, (byte) 0x6E, (byte) 0x74, (byte) 0x74, (byte) 0x00, (byte) 0x16, (byte) 0x4C, (byte) 0x6A, (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2F, (byte) 0x69, (byte) 0x6F, (byte) 0x2F, (byte) 0x53, (byte) 0x65, (byte) 0x72, (byte) 0x69, (byte) 0x61, (byte) 0x6C, (byte) 0x69, (byte) 0x7A, (byte) 0x61, (byte) 0x62, (byte) 0x6C, (byte) 0x65, (byte) 0x3B, (byte) 0x78, (byte) 0x72, (byte) 0x00, (byte) 0x33, (byte) 0x63, (byte) 0x6F, (byte) 0x6D, (byte) 0x2E, (byte) 0x62, (byte) 0x6C, (byte) 0x61, (byte) 0x7A, (byte) 0x65, (byte) 0x62, (byte) 0x69, (byte) 0x74, (byte) 0x2E, (byte) 0x70, (byte) 0x65, (byte) 0x72, (byte) 0x73, (byte) 0x69, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x6E, (byte) 0x63, (byte) 0x65, (byte) 0x2E, (byte) 0x76, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x2E, (byte) 0x70, (byte) 0x72, (byte) 0x6F, (byte) 0x63, (byte) 0x65, (byte) 0x73, (byte) 0x73, (byte) 0x6F, (byte) 0x72, (byte) 0x2E, (byte) 0x6D, (byte) 0x6F, (byte) 0x64, (byte) 0x65, (byte) 0x6C, (byte) 0x2E, (byte) 0x42, (byte) 0x56, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x2E, (byte) 0x8B, (byte) 0x3F, (byte) 0x63, (byte) 0x38, (byte) 0xF4, (byte) 0x15, (byte) 0x11, (byte) 0x02, (byte) 0x00, (byte) 0x02, (byte) 0x4C, (byte) 0x00, (byte) 0x0C, (byte) 0x63, (byte) 0x61, (byte) 0x70, (byte) 0x74, (byte) 0x75, (byte) 0x72, (byte) 0x65, (byte) 0x64, (byte) 0x4E, (byte) 0x61, (byte) 0x6D, (byte) 0x65, (byte) 0x74, (byte) 0x00, (byte) 0x12, (byte) 0x4C, (byte) 0x6A, (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2F, (byte) 0x6C, (byte) 0x61, (byte) 0x6E, (byte) 0x67, (byte) 0x2F, (byte) 0x53, (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6E, (byte) 0x67, (byte) 0x3B, (byte) 0x4C, (byte) 0x00, (byte) 0x0C, (byte) 0x70, (byte) 0x6F, (byte) 0x73, (byte) 0x74, (byte) 0x4C, (byte) 0x6F, (byte) 0x61, (byte) 0x64, (byte) 0x4E, (byte) 0x61, (byte) 0x6D, (byte) 0x65, (byte) 0x74, (byte) 0x00, (byte) 0x12, (byte) 0x4C, (byte) 0x6A, (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2F, (byte) 0x6C, (byte) 0x61, (byte) 0x6E, (byte) 0x67, (byte) 0x2F, (byte) 0x53, (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6E, (byte) 0x67, (byte) 0x3B, (byte) 0x78, (byte) 0x72, (byte) 0x00, (byte) 0x3A, (byte) 0x63, (byte) 0x6F, (byte) 0x6D, (byte) 0x2E, (byte) 0x62, (byte) 0x6C, (byte) 0x61, (byte) 0x7A, (byte) 0x65, (byte) 0x62, (byte) 0x69, (byte) 0x74, (byte) 0x2E, (byte) 0x70, (byte) 0x65, (byte) 0x72, (byte) 0x73, (byte) 0x69, (byte) 0x73, (byte) 0x74, (byte) 0x65, (byte) 0x6E, (byte) 0x63, (byte) 0x65, (byte) 0x2E, (byte) 0x76, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x2E, (byte) 0x70, (byte) 0x72, (byte) 0x6F, (byte) 0x63, (byte) 0x65, (byte) 0x73, (byte) 0x73, (byte) 0x6F, (byte) 0x72, (byte) 0x2E, (byte) 0x6D, (byte) 0x6F, (byte) 0x64, (byte) 0x65, (byte) 0x6C, (byte) 0x2E, (byte) 0x73, (byte) 0x75, (byte) 0x62, (byte) 0x2E, (byte) 0x42, (byte) 0x61, (byte) 0x73, (byte) 0x65, (byte) 0x56, (byte) 0x69, (byte) 0x65, (byte) 0x77, (byte) 0x45, (byte) 0x82, (byte) 0xA1, (byte) 0xAC, (byte) 0x40, (byte) 0x09, (byte) 0xDC, (byte) 0x93, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x78, (byte) 0x70, (byte) 0x70, (byte) 0x70, (byte) 0x70, (byte) 0x70, (byte) 0x70};
public Integer id;
public String name;
public Integer parent;
Expand Down

0 comments on commit 8a50d75

Please sign in to comment.