Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ public interface ImportContext {
/**
* Add fqcn to the import list. Returns fqcn as needed in source code.
* Attempts to handle fqcn with array and generics references.
*
* <p>
* e.g.
* java.util.Collection<org.marvel.Hulk> imports java.util.Collection and returns Collection
* org.marvel.Hulk[] imports org.marvel.Hulk and returns Hulk
*
*
* @param fqcn
* @return import string
*/
public abstract String importType(String fqcn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.hibernate.tool.internal.export.java;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -57,13 +56,12 @@ public ImportContextImpl(String basePackage) {
/**
* Add fqcn to the import list. Returns fqcn as needed in source code.
* Attempts to handle fqcn with array and generics references.
*
* <p>
* e.g.
* java.util.Collection<org.marvel.Hulk> imports java.util.Collection and returns Collection
* org.marvel.Hulk[] imports org.marvel.Hulk and returns Hulk
*
*
* @param fqcn
* @return import string
*/
public String importType(String fqcn) {
Expand All @@ -88,14 +86,9 @@ public String importType(String fqcn) {
String simpleName = StringHelper.unqualify(fqcn);
if(simpleNames.containsKey(simpleName)) {
String existingFqcn = (String) simpleNames.get(simpleName);
if(existingFqcn.equals(pureFqcn)) {
canBeSimple = true;
} else {
canBeSimple = false;
}
canBeSimple = existingFqcn.equals(pureFqcn);
} else {
canBeSimple = true;
simpleNames.put(simpleName, pureFqcn);
simpleNames.put(simpleName, pureFqcn);
imports.add( pureFqcn );
}

Expand Down Expand Up @@ -127,38 +120,32 @@ public String staticImport(String fqcn, String member) {
}

private boolean inDefaultPackage(String className) {
return className.indexOf( "." ) < 0;
return !className.contains(".");
}

private boolean isPrimitive(String className) {
return PRIMITIVES.containsKey( className );
}

private boolean inSamePackage(String className) {
String other = StringHelper.qualifier( className );
return other == basePackage
|| (other != null && other.equals( basePackage ) );
return StringHelper.qualifier( className ).equals(basePackage);
}

private boolean inJavaLang(String className) {
return "java.lang".equals( StringHelper.qualifier( className ) );
}

public String generateImports() {
StringBuffer buf = new StringBuffer();

for ( Iterator<String> imps = imports.iterator(); imps.hasNext(); ) {
String next = imps.next();
if(isPrimitive(next) || inDefaultPackage(next) || inJavaLang(next) || inSamePackage(next)) {
// dont add automatically "imported" stuff
} else {
if(staticImports.contains(next)) {
buf.append("import static " + next + ";\r\n");
} else {
buf.append("import " + next + ";\r\n");
}
}
}
StringBuilder buf = new StringBuilder();
for (String next : imports) {
if (!(isPrimitive(next) || inDefaultPackage(next) || inJavaLang(next) || inSamePackage(next))) {
if (staticImports.contains(next)) {
buf.append("import static ").append(next).append(";\r\n");
} else {
buf.append("import ").append(next).append(";\r\n");
}
}
}

if(buf.indexOf( "$" )>=0) {
return buf.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,11 @@ public String getName() {
}

@Override
protected void visitProperty(PersistentClass clazz, Property property, IssueCollector collector) {
protected void visitProperty(Property property, IssueCollector collector) {
Value value = property.getValue();

if(value instanceof Collection) {
Collection col = (Collection) value;
if(col.getCacheConcurrencyStrategy()!=null) { // caching is enabled
if(value instanceof Collection col) {
if(col.getCacheConcurrencyStrategy()!=null) { // caching is enabled
if (!col.getElement().isSimpleValue()) {
String entityName = (String) col.getElement().accept( new EntityNameFromValueVisitor() );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@
*/
package org.hibernate.tool.internal.export.lint;

import java.util.Iterator;

import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;

public abstract class EntityModelDetector extends Detector {

public void visit(IssueCollector collector) {
for (Iterator<PersistentClass> iter = getMetadata().getEntityBindings().iterator(); iter.hasNext();) {
PersistentClass clazz = iter.next();
this.visit(clazz, collector);
}
for (PersistentClass clazz : getMetadata().getEntityBindings()) {
this.visit(clazz, collector);
}
}

protected void visit(PersistentClass clazz, IssueCollector collector) {
Expand All @@ -37,13 +34,13 @@ protected void visit(PersistentClass clazz, IssueCollector collector) {

private void visitProperties(PersistentClass clazz, IssueCollector collector) {
if(clazz.hasIdentifierProperty()) {
this.visitProperty(clazz, clazz.getIdentifierProperty(), collector);
this.visitProperty(clazz.getIdentifierProperty(), collector);
}
for (Property property : clazz.getProperties()) {
this.visitProperty(clazz, property, collector);
this.visitProperty(property, collector);
}
}

protected abstract void visitProperty(PersistentClass clazz, Property property, IssueCollector collector);
protected abstract void visitProperty(Property property, IssueCollector collector);

}
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,23 @@ protected void visit(PersistentClass clazz, IssueCollector collector) {
} else if(enhanceEnabled){
Class<?>[] interfaces = mappedClass.getInterfaces();
boolean enhanced = false;
for (int i = 0; i < interfaces.length; i++) {
Class<?> intface = interfaces[i];
if(intface.getName().equals(Managed.class.getName())) {
enhanced = true;
}
}
for (Class<?> intface : interfaces) {
if (intface.getName().equals(Managed.class.getName())) {
enhanced = true;
break;
}
}

if (enhanceEnabled && !enhanced) {
if (!enhanced) {
collector.reportIssue( new Issue("LAZY_NOT_INSTRUMENTED", Issue.HIGH_PRIORITY, "'" + clazz.getEntityName() + "' has lazy='false', but its class '" + mappedClass.getName() + "' has not been instrumented with javaassist") );
return;
} else {
// unknown bytecodeprovider...can't really check for that.
}

}
}

@Override
protected void visitProperty(
PersistentClass clazz,
Property property,
IssueCollector collector) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package org.hibernate.tool.internal.export.lint;

import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;

public class ShadowedIdentifierDetector extends EntityModelDetector {
Expand All @@ -27,7 +26,7 @@ public String getName() {
}

@Override
protected void visitProperty(PersistentClass clazz, Property property, IssueCollector collector) {
protected void visitProperty(Property property, IssueCollector collector) {
if(property.getName().equals("id")) {
if (property != property.getPersistentClass().getIdentifierProperty()) {
collector.reportIssue(new Issue("ID_SHADOWED", Issue.LOW_PRIORITY, property.getPersistentClass().getEntityName() + " has a normal property named 'id'. This can cause issues since HQL queries will always interpret 'id' as the identifier and not the concrete property"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

final public class NameConverter {

private static Set<String> RESERVED_KEYWORDS;
private static final Set<String> RESERVED_KEYWORDS;
static {
RESERVED_KEYWORDS = new HashSet<String>();
RESERVED_KEYWORDS = new HashSet<>();

RESERVED_KEYWORDS.add( "abstract" );
RESERVED_KEYWORDS.add( "continue" );
Expand Down Expand Up @@ -78,29 +78,26 @@ final public class NameConverter {
RESERVED_KEYWORDS.add( "while" );
}

private NameConverter() {

}
private NameConverter() {}

/**
* Converts a database name (table or column) to a java name (first letter capitalised).
* employee_name -> EmployeeName.
*
* <p>
* Derived from middlegen's dbnameconverter.
* @param s The database name to convert.
*
* @return The converted database name.
*/
public static String toUpperCamelCase(String s) {
if ( "".equals(s) ) {
if (s.isEmpty()) {
return s;
}
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();

boolean capitalize = true;
boolean lastCapital = false;
boolean lastDecapitalized = false;
String p = null;
for (int i = 0; i < s.length(); i++) {
String c = s.substring(i, i + 1);
if ( "_".equals(c) || " ".equals(c) || "-".equals(c) ) {
Expand All @@ -121,26 +118,16 @@ public static String toUpperCamelCase(String s) {
//if(forceFirstLetter && result.length()==0) capitalize = false;

if (capitalize) {
if (p == null || !p.equals("_") ) {
result.append(c.toUpperCase() );
capitalize = false;
p = c;
}
else {
result.append(c.toLowerCase() );
capitalize = false;
p = c;
}
}
result.append(c.toUpperCase());
capitalize = false;
}
else {
result.append(c.toLowerCase() );
lastDecapitalized = true;
p = c;
}

}
String r = result.toString();
return r;
}

}
return result.toString();
}

static public String simplePluralize(String singular) {
Expand Down
25 changes: 12 additions & 13 deletions orm/src/main/java/org/hibernate/tool/internal/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.hibernate.tool.internal.util;

import java.util.Objects;
import java.util.StringTokenizer;

/**
Expand Down Expand Up @@ -44,7 +45,7 @@ public static String[] split(String str, String separator) {
StringTokenizer tok = null;
if (separator == null) {
tok = new StringTokenizer(str);
}
}
else {
tok = new StringTokenizer(str, separator);
}
Expand All @@ -59,26 +60,24 @@ public static String[] split(String str, String separator) {
lastTokenBegin = str.indexOf(list[i], lastTokenEnd);
lastTokenEnd = lastTokenBegin + list[i].length();
i++;
}
}
return list;
}

public static String leftPad(String str, int size) {
size = (size - str.length() );
if (size > 0) {
StringBuffer buffer = new StringBuffer(size);
for (int i = 0; i < size; i++) {
buffer.append(" ");
}
str = buffer.toString() + str;
str = " ".repeat(size) + str;
}
return str;
}

public static boolean isEqual(String str1, String str2) {
if(str1==str2) return true;
if(str1!=null && str1.equals(str2) ) return true;
return false;
}

public static boolean isEqual(String str1, String str2) {
return Objects.equals( str1, str2 );
}

public static boolean isEmptyOrNull(String string) {
return string == null || string.isEmpty();
}

}