Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup j2ee.persistence module #5444

Merged
merged 13 commits into from
Feb 10, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -125,20 +125,16 @@ public FileObject generate(final GenerationOptions options,
Parameters.notNull("options", options); //NOI18N
Parameters.notNull("strategyClass", strategyClass); //NOI18N

Task task = new Task<WorkingCopy>() {
Task task = (Task<WorkingCopy>) (WorkingCopy workingCopy) -> {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();

public void run(WorkingCopy workingCopy) throws Exception {

workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();

for (Tree typeDeclaration : cut.getTypeDecls()){
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDeclaration.getKind())){
ClassTree clazz = (ClassTree) typeDeclaration;
EntityManagerGenerationStrategy strategy = instantiateStrategy(strategyClass, workingCopy, make, clazz, options);
workingCopy.rewrite(clazz, strategy.generate());
}
for (Tree typeDeclaration : cut.getTypeDecls()){
if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDeclaration.getKind())){
ClassTree clazz = (ClassTree) typeDeclaration;
EntityManagerGenerationStrategy strategy = instantiateStrategy(strategyClass, workingCopy, make, clazz, options);
workingCopy.rewrite(clazz, strategy.generate());
}
}
};
Expand Down Expand Up @@ -171,10 +167,8 @@ private EntityManagerGenerationStrategy instantiateStrategy(Class<? extends Enti
result.setGenerationOptions(options);
result.setTreeMaker(make);
result.setPersistenceUnit(getPersistenceUnit());
} catch (IllegalAccessException iae){
throw new RuntimeException(iae); //TODO
} catch (InstantiationException ie){
throw new RuntimeException(ie); //TODO
} catch (IllegalAccessException | InstantiationException ex){
throw new RuntimeException(ex); //TODO
}

return result;
Expand All @@ -197,7 +191,9 @@ private PersistenceUnit getPersistenceUnit() {
PersistenceUnit forOne=null;
for(int i=0;i<pus.length && forOne==null;i++) {
PersistenceUnit tmp=pus[i];
if(forAll ==null && !tmp.isExcludeUnlistedClasses()) forAll=tmp;//first match sutable for all entities in the project
if(forAll ==null && !tmp.isExcludeUnlistedClasses()) {
forAll=tmp;//first match sutable for all entities in the project
}
if(tmp.isExcludeUnlistedClasses()) {
String []classes = tmp.getClass2();
for(String clas:classes){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.netbeans.modules.j2ee.persistence.action;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -88,7 +87,9 @@ public String getBody(String version){
{
return body2_0;
}
else return body;
else {
return body;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,22 @@ public static class Factory implements CodeGenerator.Factory {

@Override
public List<? extends CodeGenerator> create(Lookup context) {
ArrayList<CodeGenerator> ret = new ArrayList<CodeGenerator>();
ArrayList<CodeGenerator> ret = new ArrayList<>();
JTextComponent component = context.lookup(JTextComponent.class);
CompilationController controller = context.lookup(CompilationController.class);
TreePath path = context.lookup(TreePath.class);
path = path != null ? getPathElementOfKind(TreeUtilities.CLASS_TREE_KINDS, path) : null;
if (component == null || controller == null || path == null)
if (component == null || controller == null || path == null) {
return ret;
}
try {
controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
Element elem = controller.getTrees().getElement(path);
if (elem != null) {
UseEntityManagerCodeGenerator gen = createUseEntityManagerCodeGenerator(component, controller, elem);
if (gen != null)
if (gen != null) {
ret.add(gen);
}
}
} catch (IOException ioe) {
}
Expand All @@ -80,8 +82,9 @@ public List<? extends CodeGenerator> create(Lookup context) {
}

static UseEntityManagerCodeGenerator createUseEntityManagerCodeGenerator(JTextComponent component, CompilationController cc, Element el) throws IOException {
if (el.getKind() != ElementKind.CLASS)
if (el.getKind() != ElementKind.CLASS) {
return null;
}
TypeElement typeElement = (TypeElement)el;
if (!isEnable(cc.getFileObject(), typeElement)) {
return null;
Expand Down Expand Up @@ -119,7 +122,9 @@ private static boolean isEnable(FileObject fileObject, TypeElement typeElement)
if (project == null) {
return false;
}
if(ElementKind.INTERFACE == typeElement.getKind())return false;
if(ElementKind.INTERFACE == typeElement.getKind()) {
return false;
}
DataObject dObj = null;
try {
dObj = DataObject.find(fileObject);
Expand All @@ -144,12 +149,7 @@ private static boolean isEnable(FileObject fileObject, TypeElement typeElement)
}

FileObject entityMgrRes = cp.findResource("javax/persistence/EntityManager.class"); // NOI18N

if (entityMgrRes != null) {
return true;
} else {
return false;
}
return entityMgrRes != null;
}

public static TreePath getPathElementOfKind(Tree.Kind kind, TreePath path) {
Expand All @@ -158,8 +158,9 @@ public static TreePath getPathElementOfKind(Tree.Kind kind, TreePath path) {

public static TreePath getPathElementOfKind(Set<Tree.Kind> kinds, TreePath path) {
while (path != null) {
if (kinds.contains(path.getLeaf().getKind()))
if (kinds.contains(path.getLeaf().getKind())) {
return path;
}
path = path.getParentPath();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,7 @@ public Set<FileObject> generate(ProgressContributor progressContributor) throws
helper.buildBeans();

} catch (DBException ex){
IOException wrapper = new IOException(ex.getMessage());
wrapper.initCause(ex);
throw wrapper;
throw new IOException(ex);
}


Expand All @@ -231,7 +229,7 @@ public Set<FileObject> generate(ProgressContributor progressContributor) throws
private TableClosure getTableClosure() throws SQLException, DBException{
TableProvider tableProvider = new DBSchemaTableProvider(getSchemaElement(), generator, project);

Set<Table> selectedTables = new HashSet<Table>();
Set<Table> selectedTables = new HashSet<>();
for (Table each : tableProvider.getTables()){
if (tableNames.contains(each.getName())){
selectedTables.add(each);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class JavaPersistenceQLKeywords {
private JavaPersistenceQLKeywords() {}

static{
String rawKeywords = NbBundle.getBundle(JavaPersistenceQLKeywords.class).getString("JavaPersistenceQLKeywords");
String rawKeywords = NbBundle.getMessage(JavaPersistenceQLKeywords.class, "JavaPersistenceQLKeywords");
keywords = new TreeSet<String>(Arrays.asList(rawKeywords.split(","))); //NOI18N
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public boolean isScanInProgress() {
public void waitScanFinished() {
// TODO: RETOUCHE
//NNMDRListener.getDefault().waitScanFinished();
return;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,21 @@ public Persistence getRoot(FileObject fo) throws java.io.IOException {
synchronized (ddMap) {
persistence = ddMap.get(fo);
if (persistence == null) {
InputStream is=fo.getInputStream();
String version=Persistence.VERSION_1_0;
try {
try (InputStream is=fo.getInputStream()) {
version=JPAParseUtils.getVersion(is);
} catch (SAXException ex) {
Exceptions.printStackTrace(ex);
}
finally
{
if(is!=null)is.close();
}
is=fo.getInputStream();
try{

try (InputStream is=fo.getInputStream()) {
if(Persistence.VERSION_2_1.equals(version)) {
persistence = org.netbeans.modules.j2ee.persistence.dd.persistence.model_2_1.Persistence.createGraph(is);
} else if(Persistence.VERSION_2_0.equals(version)) {
persistence = org.netbeans.modules.j2ee.persistence.dd.persistence.model_2_0.Persistence.createGraph(is);
} else {//1.0 - default
persistence = org.netbeans.modules.j2ee.persistence.dd.persistence.model_1_0.Persistence.createGraph(is);
}
} finally {
if(is!=null) {
is.close();
}
}
ddMap.put(fo, persistence);
}
Expand All @@ -108,7 +99,9 @@ public Persistence getRoot(FileObject fo) throws java.io.IOException {
*/
public void refresh(FileObject fo){
synchronized (ddMap) {
if( fo!=null )ddMap.remove(fo);
if( fo!=null ) {
ddMap.remove(fo);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.netbeans.api.project.Sources;
import org.netbeans.api.project.libraries.Library;
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModel;
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModelAction;
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModelException;
import org.netbeans.modules.j2ee.persistence.api.EntityClassScope;
import org.netbeans.modules.j2ee.persistence.api.PersistenceScope;
Expand Down Expand Up @@ -73,16 +72,7 @@ public static EntityMappings getEntityMappings(FileObject documentFO) {
MetadataModel<EntityMappingsMetadata> model = entityClassScope.getEntityMappingsModel(true);
EntityMappings mappings = null;
try {
mappings = model.runReadAction(
new MetadataModelAction<EntityMappingsMetadata, EntityMappings>(){

@Override
public EntityMappings run(EntityMappingsMetadata metadata) throws Exception {
return metadata.getRoot();
}

}
);
mappings = model.runReadAction( (EntityMappingsMetadata metadata) -> metadata.getRoot() );
} catch (MetadataModelException ex) {
Exceptions.printStackTrace(ex);
} catch (IOException ex) {
Expand Down Expand Up @@ -115,7 +105,7 @@ public static PersistenceUnit[] getPersistenceUnits(FileObject sourceFile) throw
return new PersistenceUnit[0];
}

List<PersistenceUnit> result = new ArrayList<PersistenceUnit>();
List<PersistenceUnit> result = new ArrayList<>();
ClassPath cp = ClassPath.getClassPath(sourceFile, ClassPath.SOURCE);
for (PersistenceScope persistenceScope : getPersistenceScopes(project, cp != null ? cp.findOwnerRoot(sourceFile) : null)) {
Persistence persistence = null;
Expand All @@ -129,7 +119,7 @@ public static PersistenceUnit[] getPersistenceUnits(FileObject sourceFile) throw
}
}

return result.toArray(new PersistenceUnit[result.size()]);
return result.toArray(new PersistenceUnit[0]);
}

/**
Expand Down Expand Up @@ -235,7 +225,7 @@ public static String getJPAVersion(Project target)

public static String getJPAVersion(Library lib) {
List<URL> roots=lib.getContent("classpath");
ClassPath cp = ClassPathSupport.createClassPath(roots.toArray(new URL[roots.size()]));
ClassPath cp = ClassPathSupport.createClassPath(roots.toArray(new URL[0]));
String version=null;
if(cp.findResource("javax/persistence/criteria/CriteriaUpdate.class")!=null) {
version=Persistence.VERSION_2_1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.netbeans.modules.j2ee.persistence.dd.common;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.jar.Attributes;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -40,7 +42,7 @@ public class JPAParseUtils {

/** Parsing just for detecting the version SAX parser used
*/
public static String getVersion(java.io.InputStream is) throws java.io.IOException, SAXException {
public static String getVersion(InputStream is) throws IOException, SAXException {
return ParseUtils.getVersion(is, new VersionHandler(), DDResolver.getInstance());
}

Expand All @@ -59,7 +61,6 @@ public void startElement(String uri, String localName, String rawName, Attribute
}
}


@Override
public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException {
return super.resolveEntity(publicId, systemId);
Expand All @@ -86,6 +87,7 @@ static synchronized DDResolver getInstance() {
return resolver;
}

@Override
public InputSource resolveEntity(String publicId, String systemId) {
// additional logging for #127276
if (LOGGER.isLoggable(Level.FINE)) {
Expand All @@ -107,19 +109,19 @@ public InputSource resolveEntity(String publicId, String systemId) {
if (resource==null) {
return null;
}
java.net.URL url = this.getClass().getResource(resource);
URL url = this.getClass().getResource(resource);
return new InputSource(url.toString());
}
}

public static SAXParseException parse(FileObject fo)
throws org.xml.sax.SAXException, java.io.IOException {
throws SAXException, IOException {
// no need to close the stream, will be closed by the parser, see @org.xml.sax.InputSource
return parse(new InputSource(fo.getInputStream()));
}

public static SAXParseException parse (InputSource is)
throws org.xml.sax.SAXException, java.io.IOException {
throws SAXException, IOException {
return ParseUtils.parseDD(is, DDResolver.getInstance());
}

Expand All @@ -131,7 +133,7 @@ public static SAXParseException parse (InputSource is)
* no exception.
*/
public static SAXParseException parse (InputSource is, EntityResolver resolver)
throws org.xml.sax.SAXException, java.io.IOException {
throws SAXException, IOException {
return ParseUtils.parseDD(is, resolver);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,23 @@ private static class ErrorHandler implements org.xml.sax.ErrorHandler {
private int errorType=-1;
SAXParseException error;

@Override
public void warning(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException {
if (errorType<0) {
errorType=0;
error=sAXParseException;
}
//throw sAXParseException;
}
@Override
public void error(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException {
if (errorType<1) {
errorType=1;
error=sAXParseException;
}
//throw sAXParseException;
}
@Override
public void fatalError(org.xml.sax.SAXParseException sAXParseException) throws org.xml.sax.SAXException {
errorType=2;
throw sAXParseException;
Expand Down Expand Up @@ -127,7 +130,9 @@ public static SAXParseException parseDD(InputSource is, EntityResolver ddResolve
reader.setFeature("http://xml.org/sax/features/namespaces", true); // NOI18N
reader.parse(is);
SAXParseException error = errorHandler.getError();
if (error!=null) return error;
if (error!=null) {
return error;
}
} catch (IllegalArgumentException ex) {
// yes, this may happen, see issue #71738
SAXException sax = new SAXException(ex.getMessage(), ex);
Expand Down
Loading