io.ebean.enhance.querybean.DetectQueryBean Maven / Gradle / Ivy
package io.ebean.enhance.querybean;
import java.util.HashSet;
import java.util.Set;
/**
* Detects if a class is a query bean.
*
* Used by enhancement to detect when GETFIELD access on query beans should be replaced by
* appropriate method calls.
*
*/
public class DetectQueryBean {
private final Set entityPackages = new HashSet<>();
public DetectQueryBean() {
}
public void addAll(Set rawEntityPackages) {
for (String rawEntityPackage : rawEntityPackages) {
entityPackages.add(convert(rawEntityPackage));
}
}
/**
* Convert package to slash notation taking into account trailing wildcard.
*/
private static String convert(String pkg) {
pkg = pkg.trim();
if (pkg.endsWith("*")) {
pkg = pkg.substring(0, pkg.length() - 1);
}
if (pkg.endsWith(".query")) {
// always work with entity bean packages so trim
pkg = pkg.substring(0, pkg.length() - 6);
}
pkg = pkg.replace('.', '/');
return pkg.endsWith("/") ? pkg : pkg + "/";
}
@Override
public String toString() {
return entityPackages.toString();
}
/**
* Return true if there are no known packages.
*/
public boolean isEmpty() {
return entityPackages.isEmpty();
}
/**
* Return true if this class is a query bean using naming conventions for query beans.
*/
public boolean isQueryBean(String owner) {
int subPackagePos = owner.lastIndexOf("/query/");
if (subPackagePos > -1) {
String suffix = owner.substring(subPackagePos);
if (isQueryBeanSuffix(suffix)) {
String domainPackage = owner.substring(0, subPackagePos + 1);
return isEntityBeanPackage(domainPackage);
}
}
return false;
}
private boolean isEntityBeanPackage(String domainPackage) {
for (String pkg : entityPackages) {
if (domainPackage.startsWith(pkg)) {
return true;
}
}
return false;
}
public boolean isEntityBean(String owner) {
return isEntityBeanPackage(owner);
}
/**
* Check that the class follows query bean naming convention.
*/
private boolean isQueryBeanSuffix(String suffix) {
return (suffix.startsWith("/query/Q") || suffix.startsWith("/query/assoc/Q"));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy