Skip to content

Commit

Permalink
Provide qute.native.enabled setting
Browse files Browse the repository at this point in the history
Fixes redhat-developer#629

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed May 4, 2022
1 parent f7ef2e8 commit a3d3f54
Show file tree
Hide file tree
Showing 33 changed files with 1,656 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.acme.qute;

import java.math.BigDecimal;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection(fields = false)
public class ItemWithRegisterForReflection {

public final String name;

public final BigDecimal price;

public ItemWithRegisterForReflection(BigDecimal price, String name) {
this.price = price;
this.name = name;
}

public Item[] getDerivedItems() {
return null;
}

public static BigDecimal staticMethod(Item item) {
return item.price.multiply(new BigDecimal("0.9"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.acme.qute;

import java.math.BigDecimal;

import io.quarkus.qute.TemplateData;

@TemplateData(target = BigDecimal.class)
@TemplateData(ignoreSuperclasses = true)
public class ItemWithTemplateData {

public final String name;

public final BigDecimal price;

public ItemWithTemplateData(BigDecimal price, String name) {
this.price = price;
this.name = name;
}

public Item[] getDerivedItems() {
return null;
}

public static BigDecimal staticMethod(Item item) {
return item.price.multiply(new BigDecimal("0.9"));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 Red Hat Inc. and others.
0* Copyright (c) 2021 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
Expand All @@ -17,6 +17,7 @@
import java.util.List;
import java.util.Optional;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.junit.Assert;
import org.junit.Assume;
Expand Down Expand Up @@ -294,6 +295,90 @@ public void string() throws Exception {
assertExtendedTypes("java.lang.String", "java.lang.CharSequence", extendedTypes);
}

@Test
public void templateData() throws CoreException, Exception {
loadMavenProject(QuteMavenProjectName.qute_quickstart);

QuteResolvedJavaTypeParams params = new QuteResolvedJavaTypeParams("org.acme.qute.ItemWithTemplateData",
QuteMavenProjectName.qute_quickstart);
ResolvedJavaTypeInfo result = QuteSupportForTemplate.getInstance().getResolvedJavaType(params, getJDTUtils(),
new NullProgressMonitor());
Assert.assertNotNull(result);
Assert.assertEquals("org.acme.qute.ItemWithTemplateData", result.getSignature());
Assert.assertFalse(result.isIterable());

// @TemplateData

// @TemplateData(target = BigDecimal.class)
// @TemplateData(ignoreSuperclasses = true)
// public class ItemWithTemplateData {
Assert.assertNotNull(result.getTemplateDataAnnotations());
Assert.assertEquals(2, result.getTemplateDataAnnotations().size());
// @TemplateData(target = BigDecimal.class)
Assert.assertFalse(result.getTemplateDataAnnotations().get(0).isIgnoreSuperclasses());
// @TemplateData(ignoreSuperclasses = true)
Assert.assertTrue(result.getTemplateDataAnnotations().get(1).isIgnoreSuperclasses());

// Fields
Assert.assertNotNull(result.getFields());
Assert.assertEquals(2, result.getFields().size());
Assert.assertEquals("name", result.getFields().get(0).getName());
Assert.assertEquals("java.lang.String", result.getFields().get(0).getType());
Assert.assertEquals("price", result.getFields().get(1).getName());
Assert.assertEquals("java.math.BigDecimal", result.getFields().get(1).getType());

// Methods
Assert.assertNotNull(result.getMethods());
Assert.assertEquals(1, result.getMethods().size());
Assert.assertEquals("getDerivedItems() : org.acme.qute.Item[]", result.getMethods().get(0).getSignature());

// Invalid methods(static method)
JavaMethodInfo discountedPriceMethod = findMethod(result, "staticMethod");
Assert.assertNull(discountedPriceMethod);
InvalidMethodReason reason = result.getInvalidMethodReason("staticMethod");
Assert.assertEquals(InvalidMethodReason.Static, reason);
}

@Test
public void registerForReflection() throws CoreException, Exception {
loadMavenProject(QuteMavenProjectName.qute_quickstart);

QuteResolvedJavaTypeParams params = new QuteResolvedJavaTypeParams("org.acme.qute.ItemWithRegisterForReflection",
QuteMavenProjectName.qute_quickstart);
ResolvedJavaTypeInfo result = QuteSupportForTemplate.getInstance().getResolvedJavaType(params, getJDTUtils(),
new NullProgressMonitor());
Assert.assertNotNull(result);
Assert.assertEquals("org.acme.qute.ItemWithRegisterForReflection", result.getSignature());
Assert.assertFalse(result.isIterable());

// @RegisterForReflection

// @RegisterForReflection(fields = false)
// public class ItemWithRegisterForReflection {
Assert.assertNotNull(result.getRegisterForReflectionAnnotation());
Assert.assertFalse(result.getRegisterForReflectionAnnotation().isFields());
Assert.assertTrue(result.getRegisterForReflectionAnnotation().isMethods());

// Fields
Assert.assertNotNull(result.getFields());
Assert.assertEquals(2, result.getFields().size());
Assert.assertEquals("name", result.getFields().get(0).getName());
Assert.assertEquals("java.lang.String", result.getFields().get(0).getType());
Assert.assertEquals("price", result.getFields().get(1).getName());
Assert.assertEquals("java.math.BigDecimal", result.getFields().get(1).getType());

// Methods
Assert.assertNotNull(result.getMethods());
Assert.assertEquals(1, result.getMethods().size());
Assert.assertEquals("getDerivedItems() : org.acme.qute.Item[]", result.getMethods().get(0).getSignature());

// Invalid methods(static method)
JavaMethodInfo discountedPriceMethod = findMethod(result, "staticMethod");
Assert.assertNull(discountedPriceMethod);
InvalidMethodReason reason = result.getInvalidMethodReason("staticMethod");
Assert.assertEquals(InvalidMethodReason.Static, reason);
}

private static void assertExtendedTypes(String type, String extendedType, List<String> extendedTypes) {
Assert.assertTrue("The Java type '" + type + "' should extends '" + extendedType + "'.",
extendedTypes.contains(extendedType));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.qute.commons;

import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

/**
* '@io.quarkus.runtime.annotations.RegisterForReflection' annotation
*
* This annotation that can be used to force a class to be registered for
* reflection in native image mode
*/
public class RegisterForReflectionAnnotation {

/**
* If the methods should be registered
*/
private Boolean methods;

/**
* If the fields should be registered
*/
private Boolean fields;

public boolean isMethods() {
if (methods == null) {
return true;
}
return methods.booleanValue();
}

public void setMethods(Boolean methods) {
this.methods = methods;
}

public boolean isFields() {
if (fields == null) {
return true;
}
return fields.booleanValue();
}

public void setFields(Boolean fields) {
this.fields = fields;
}

@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("fields", this.isFields());
b.add("methods", this.isMethods());
return b.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public class ResolvedJavaTypeInfo extends JavaTypeInfo {

private Boolean isIterable;

private RegisterForReflectionAnnotation registerForReflectionAnnotation;

private List<TemplateDataAnnotation> templateDataAnnotations;

/**
* Returns list of extended types.
*
Expand Down Expand Up @@ -178,13 +182,59 @@ private synchronized boolean computeIsIterable() {
return iterable;
}

/**
* Returns the list of '@io.quarkus.qute.TemplateData' annotations for the Java
* type.
*
* @return the list of '@io.quarkus.qute.TemplateData' annotations for the Java
* type.
*/
public List<TemplateDataAnnotation> getTemplateDataAnnotations() {
return templateDataAnnotations;
}

/**
* Set the list of '@io.quarkus.qute.TemplateData' annotations for the Java
* type.
*
* @param templateDataAnnotations the list of '@io.quarkus.qute.TemplateData'
* annotations for the Java type.
*/
public void setTemplateDataAnnotations(List<TemplateDataAnnotation> templateDataAnnotations) {
this.templateDataAnnotations = templateDataAnnotations;
}

/**
* Returns '@io.quarkus.runtime.annotations.RegisterForReflection' for the Java
* type.
*
* @return '@io.quarkus.runtime.annotations.RegisterForReflection' for the Java
* type.
*/
public RegisterForReflectionAnnotation getRegisterForReflectionAnnotation() {
return registerForReflectionAnnotation;
}

/**
* Set the '@io.quarkus.runtime.annotations.RegisterForReflection' for the Java
* type.
*
* @param registerForReflectionAnnotation '@io.quarkus.runtime.annotations.RegisterForReflection'
* for the Java type.
*/
public void setRegisterForReflectionAnnotation(RegisterForReflectionAnnotation registerForReflectionAnnotation) {
this.registerForReflectionAnnotation = registerForReflectionAnnotation;
}

@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("name", this.getName());
b.add("signature", this.getSignature());
b.add("iterableOf", this.getIterableOf());
b.add("iterableType", this.getIterableType());
b.add("templateDataAnnotations", this.getTemplateDataAnnotations());
b.add("registerForReflectionAnnotation", this.getRegisterForReflectionAnnotation());
return b.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright (c) 2022 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.qute.commons;

import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

/**
* '@io.quarkus.qute.TemplateData' annotation.
*
* @author Angelo ZERR
*
*/
public class TemplateDataAnnotation {

/**
* If set to true do not automatically analyze superclasses.
*/
private Boolean ignoreSuperclasses;

public boolean isIgnoreSuperclasses() {
return ignoreSuperclasses != null && ignoreSuperclasses.booleanValue();
}

public void setIgnoreSuperclasses(Boolean ignoreSuperclasses) {
this.ignoreSuperclasses = ignoreSuperclasses;
}

@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("ignoreSuperclasses", this.isIgnoreSuperclasses());
return b.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
import com.redhat.qute.jdt.internal.template.TemplateDataSupport;
import com.redhat.qute.jdt.utils.IJDTUtils;
import com.redhat.qute.jdt.utils.JDTQuteProjectUtils;
import com.redhat.qute.jdt.utils.QuteReflectionAnnotationUtils;

/**
* Qute support for Template file.
Expand Down Expand Up @@ -409,7 +410,7 @@ public ResolvedJavaTypeInfo getResolvedJavaType(QuteResolvedJavaTypeParams param
}
} else {
// ex : String implements CharSequence, ....
ITypeHierarchy typeHierarchy = type.newSupertypeHierarchy(monitor);
ITypeHierarchy typeHierarchy = type.newSupertypeHierarchy(monitor);
IType[] allSuperTypes = typeHierarchy.getSupertypes(type);
extendedTypes = Stream.of(allSuperTypes) //
.map(superType -> superType.getFullyQualifiedName()) //
Expand All @@ -427,6 +428,7 @@ public ResolvedJavaTypeInfo getResolvedJavaType(QuteResolvedJavaTypeParams param
resolvedType.setMethods(methodsInfo);
resolvedType.setInvalidMethods(invalidMethods);
resolvedType.setExtendedTypes(extendedTypes);
QuteReflectionAnnotationUtils.collectAnnotations(resolvedType, type);
return resolvedType;
}

Expand Down
Loading

0 comments on commit a3d3f54

Please sign in to comment.