This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 256032
Collapse All | Expand All

(-)j2ee.persistence/src/org/netbeans/modules/j2ee/persistence/jpqleditor/ui/JPQLEditorTopComponent.java (-114 / +10 lines)
Lines 53-72 Link Here
53
import java.awt.event.InputEvent;
53
import java.awt.event.InputEvent;
54
import java.awt.event.KeyEvent;
54
import java.awt.event.KeyEvent;
55
import java.awt.event.MouseEvent;
55
import java.awt.event.MouseEvent;
56
import java.beans.IntrospectionException;
56
import java.beans.PropertyChangeEvent;
57
import java.beans.PropertyChangeEvent;
57
import java.beans.PropertyChangeListener;
58
import java.beans.PropertyChangeListener;
58
import java.io.IOException;
59
import java.io.IOException;
59
import java.io.PrintWriter;
60
import java.io.PrintWriter;
60
import java.io.StringWriter;
61
import java.io.StringWriter;
61
import java.lang.reflect.Field;
62
import java.lang.reflect.InvocationTargetException;
63
import java.net.URL;
62
import java.net.URL;
64
import java.util.ArrayList;
63
import java.util.ArrayList;
65
import java.util.Arrays;
66
import java.util.HashMap;
64
import java.util.HashMap;
67
import java.util.List;
65
import java.util.List;
68
import java.util.StringTokenizer;
66
import java.util.StringTokenizer;
69
import java.util.Vector;
70
import java.util.logging.Level;
67
import java.util.logging.Level;
71
import java.util.logging.Logger;
68
import java.util.logging.Logger;
72
import javax.swing.DefaultComboBoxModel;
69
import javax.swing.DefaultComboBoxModel;
Lines 77-92 Link Here
77
import javax.swing.event.DocumentEvent;
74
import javax.swing.event.DocumentEvent;
78
import javax.swing.event.DocumentListener;
75
import javax.swing.event.DocumentListener;
79
import javax.swing.table.DefaultTableModel;
76
import javax.swing.table.DefaultTableModel;
77
import javax.swing.table.TableModel;
80
import org.netbeans.api.db.explorer.ConnectionManager;
78
import org.netbeans.api.db.explorer.ConnectionManager;
81
import org.netbeans.api.db.explorer.DatabaseConnection;
79
import org.netbeans.api.db.explorer.DatabaseConnection;
82
import org.netbeans.api.db.explorer.JDBCDriver;
83
import org.netbeans.api.db.explorer.JDBCDriverManager;
84
import org.netbeans.api.java.classpath.ClassPath;
85
import org.netbeans.api.progress.ProgressHandle;
80
import org.netbeans.api.progress.ProgressHandle;
86
import org.netbeans.api.progress.ProgressHandleFactory;
81
import org.netbeans.api.progress.ProgressHandleFactory;
87
import org.netbeans.api.project.FileOwnerQuery;
82
import org.netbeans.api.project.FileOwnerQuery;
88
import org.netbeans.api.project.Project;
83
import org.netbeans.api.project.Project;
89
import org.netbeans.api.project.libraries.Library;
90
import org.netbeans.modules.editor.NbEditorDocument;
84
import org.netbeans.modules.editor.NbEditorDocument;
91
import org.netbeans.modules.j2ee.persistence.api.PersistenceEnvironment;
85
import org.netbeans.modules.j2ee.persistence.api.PersistenceEnvironment;
92
import org.netbeans.modules.j2ee.persistence.dd.common.Persistence;
86
import org.netbeans.modules.j2ee.persistence.dd.common.Persistence;
Lines 100-107 Link Here
100
import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil;
94
import org.netbeans.modules.j2ee.persistence.provider.ProviderUtil;
101
import org.netbeans.modules.j2ee.persistence.unit.PUDataObject;
95
import org.netbeans.modules.j2ee.persistence.unit.PUDataObject;
102
import org.netbeans.modules.j2ee.persistence.wizard.Util;
96
import org.netbeans.modules.j2ee.persistence.wizard.Util;
103
import org.netbeans.modules.j2ee.persistence.wizard.jpacontroller.JpaControllerUtil;
104
import org.netbeans.modules.j2ee.persistence.wizard.library.PersistenceLibrarySupport;
105
import org.openide.awt.MouseUtils.PopupMouseAdapter;
97
import org.openide.awt.MouseUtils.PopupMouseAdapter;
106
import org.openide.filesystems.FileObject;
98
import org.openide.filesystems.FileObject;
107
import org.openide.filesystems.MIMEResolver;
99
import org.openide.filesystems.MIMEResolver;
Lines 182-187 Link Here
182
        });
174
        });
183
        jpqlEditor.addMouseListener(new JPQLEditorPopupMouseAdapter());
175
        jpqlEditor.addMouseListener(new JPQLEditorPopupMouseAdapter());
184
        showSQL(NbBundle.getMessage(JPQLEditorTopComponent.class, "BuildHint"));
176
        showSQL(NbBundle.getMessage(JPQLEditorTopComponent.class, "BuildHint"));
177
        resultsTable.setDefaultRenderer(Object.class, new NullTableCellRenderer());
185
    }
178
    }
186
179
187
    private class JPQLEditorPopupMouseAdapter extends PopupMouseAdapter {
180
    private class JPQLEditorPopupMouseAdapter extends PopupMouseAdapter {
Lines 538-572 Link Here
538
531
539
            setStatus(strBuffer.toString());
532
            setStatus(strBuffer.toString());
540
533
541
            Vector<String> tableHeaders = new Vector<String>();
534
            TableModel tm = new DefaultTableModel();
542
            Vector<Vector> tableData = new Vector<Vector>();
543
535
544
            if (!result.getQueryResults().isEmpty()) {
536
            if (!result.getQueryResults().isEmpty()) {
545
537
                try {
546
                Object firstObject = result.getQueryResults().get(0);
538
                    List<ReflectionInfo> info = ReflectionInfo.prepare(result.getQueryResults());
547
                if (firstObject instanceof Object[]) {
539
                    tm = new ReflectiveTableModel(info, result.getQueryResults());
548
                    // Join query result.
540
                } catch (IntrospectionException ex) {
549
                    for (Object oneObject : (Object[]) firstObject) {
541
                    logger.log(Level.WARNING, "Failed to reflect while building table model for JPA display", ex);
550
                        createTableHeaders(tableHeaders, oneObject);
551
                    }
542
                    }
552
553
                    for (Object row : result.getQueryResults()) {
554
                        createTableData(tableData, (Object[]) row);
555
                    }
543
                    }
556
557
                } else {
558
                    // Construct the table headers
559
                    createTableHeaders(tableHeaders, firstObject);
560
                    for (Object oneObject : result.getQueryResults()) {
561
                        createTableData(tableData, oneObject);
562
                    }
563
                }
564
565
            } else {
566
                
567
            }
568
            resultsTable.clearSelection();
544
            resultsTable.clearSelection();
569
            resultsTable.setModel(new JPQLEditorResultTableModel(tableData, tableHeaders)); //new DefaultTableModel(tableData, tableHeaders));
545
            resultsTable.setModel(tm);
570
546
571
547
572
        } else {
548
        } else {
Lines 596-685 Link Here
596
        Thread.currentThread().setContextClassLoader(oldClassLoader);
572
        Thread.currentThread().setContextClassLoader(oldClassLoader);
597
    }
573
    }
598
574
599
    private void createTableHeaders(Vector<String> tableHeaders, Object oneObject) {
600
        if (oneObject == null || oneObject.getClass().getName().startsWith("java.lang") || oneObject.getClass().getName().startsWith("java.math")) {//NOI18N
601
            //case for Long, String etc
602
            tableHeaders.add(org.openide.util.NbBundle.getMessage(JPQLEditorTopComponent.class, "queryResultDefaultColumnName") + " " + (tableHeaders.size() + 1));//NOI18N
603
        } else {
604
            for (java.lang.reflect.Method m : oneObject.getClass().getDeclaredMethods()) {
605
                String methodName = m.getName();
606
                if (methodName.startsWith("get")) { //NOI18N
607
                    String head = JpaControllerUtil.getPropNameFromMethod(methodName);
608
                    try {
609
                        oneObject.getClass().getDeclaredField(head);
610
                        tableHeaders.add(head);
611
                    } catch (Exception ex) {
612
                        String head2 = null;
613
                        for(Field f:oneObject.getClass().getDeclaredFields()){
614
                            if(head.equalsIgnoreCase(f.getName())){
615
                                head2 = head;
616
                            }
617
                        }
618
                        head2 = head2 == null ? methodName.substring(3) : head2;
619
                        tableHeaders.add(head2);
620
                    }
621
                    
622
                }
623
            }
624
        }
625
    }
626
627
    private void createTableData(Vector<Vector> tableData, Object... rowObject) {
628
        Vector<Object> oneRow = new Vector<Object>();
629
        for (Object oneObject : rowObject) {
630
            if (oneObject == null) {
631
                oneRow.add("NULL");//NOI18N
632
            } else if (oneObject.getClass().getName().startsWith("java.lang") || oneObject.getClass().getName().startsWith("java.math")) {
633
                //case for Long, String etc
634
                oneRow.add(oneObject.toString());
635
            } else {
636
                for (java.lang.reflect.Method m : oneObject.getClass().getDeclaredMethods()) {
637
                    String methodName = m.getName();
638
                    if (methodName.startsWith("get")) { //NOI18N
639
                        try {
640
                            Object methodReturnValue = m.invoke(oneObject, new Object[]{});
641
                            if (methodReturnValue == null) {
642
                                oneRow.add("NULL"); //NOI18N
643
                                continue;
644
                            }
645
                            if (methodReturnValue instanceof java.util.Collection) {
646
                                oneRow.add(methodReturnValue.toString());
647
                                continue;
648
                            }
649
                            oneRow.add(methodReturnValue.toString());
650
                        } catch (IllegalAccessException ex) {
651
                            //Exceptions.printStackTrace(ex);
652
                        } catch (IllegalArgumentException ex) {
653
                            //Exceptions.printStackTrace(ex);
654
                        } catch (InvocationTargetException ex) {
655
                            Exceptions.printStackTrace(ex);
656
                        }
657
                    }
658
                }
659
            }
660
        }
661
        tableData.add(oneRow);
662
    }
663
664
    private void setStatus(String message) {
575
    private void setStatus(String message) {
665
        statusLabel.setText(message);
576
        statusLabel.setText(message);
666
    }
577
    }
667
578
668
    /*
669
     * Creates custom table model with read only cell customization.
670
     */
671
    private class JPQLEditorResultTableModel extends DefaultTableModel {
672
673
        public JPQLEditorResultTableModel(Vector<Vector> tableData, Vector<String> tableHeaders) {
674
            super(tableData, tableHeaders);
675
        }
676
677
        @Override
678
        public boolean isCellEditable(int row, int column) {
679
            return false;
680
        }
681
    }
682
683
    private String removePersistenceModuleCodelines(String exceptionTrace) {
579
    private String removePersistenceModuleCodelines(String exceptionTrace) {
684
        StringTokenizer tokenizer = new StringTokenizer(exceptionTrace, "\n");
580
        StringTokenizer tokenizer = new StringTokenizer(exceptionTrace, "\n");
685
        StringBuilder filteredExceptionTrace = new StringBuilder();
581
        StringBuilder filteredExceptionTrace = new StringBuilder();
(-)j2ee.persistence/src/org/netbeans/modules/j2ee/persistence/jpqleditor/ui/NullTableCellRenderer.java (+58 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2015 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2015 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.j2ee.persistence.jpqleditor.ui;
43
44
import java.awt.Component;
45
import javax.swing.JTable;
46
import javax.swing.table.DefaultTableCellRenderer;
47
48
public class NullTableCellRenderer extends DefaultTableCellRenderer {
49
50
    @Override
51
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
52
        if(value == null) {
53
            value = "<NULL>";
54
        }
55
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
56
    }
57
    
58
}
(-)j2ee.persistence/src/org/netbeans/modules/j2ee/persistence/jpqleditor/ui/ReflectionInfo.java (+144 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2015 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2015 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.j2ee.persistence.jpqleditor.ui;
43
44
import java.beans.BeanInfo;
45
import java.beans.IntrospectionException;
46
import java.beans.Introspector;
47
import java.beans.PropertyDescriptor;
48
import java.util.ArrayList;
49
import java.util.Collections;
50
import java.util.List;
51
import java.util.TreeSet;
52
53
public class ReflectionInfo implements Comparable<ReflectionInfo> {
54
    private final Integer index;
55
    private final String propertyName;
56
57
    private ReflectionInfo(Integer index, String propertyName) {
58
        this.index = index;
59
        this.propertyName = propertyName;
60
    }
61
62
    public Integer getIndex() {
63
        return index;
64
    }
65
66
    public String getPropertyName() {
67
        return propertyName;
68
    }
69
70
    @Override
71
    public String toString() {
72
        return "{" + index + ", " + propertyName + '}';
73
    }
74
75
    
76
    @Override
77
    public int compareTo(ReflectionInfo o) {
78
        int val1 = (index == null ? 0 : index);
79
        int val2 = (o.index == null ? 0 : o.index);
80
        int result = val1 - val2;
81
        if(result != 0) {
82
            return result;
83
        }
84
        String string1 = (propertyName == null ? "" : propertyName);
85
        String string2 = (o.propertyName == null ? "" : o.propertyName);
86
        return string1.compareToIgnoreCase(string2);
87
    }
88
    
89
    public static List<ReflectionInfo> prepare (List<? extends Object> data) throws IntrospectionException {
90
        // Array mode is followed, if each data item is an array and has same dimension
91
        Integer rowLength = null;
92
        for(Object row: data) {
93
            if(row instanceof Object[]) {
94
                if(rowLength != null) {
95
                    // Arraylength differs
96
                    if(rowLength != ((Object[]) row).length) {
97
                        rowLength = null;
98
                        break;
99
                    }
100
                } else {
101
                    // Initial row length
102
                    rowLength = ((Object[]) row).length;
103
                }
104
            } else {
105
                // Non array
106
                rowLength = null;
107
                break;
108
            }
109
        }
110
        
111
        TreeSet<ReflectionInfo> resultPrecursor = new TreeSet<>();
112
        for (Object row : data) {
113
            if (rowLength != null) {
114
                Object[] rowArray = (Object[]) row;
115
                for (int i = 0; i < rowLength; i++) {
116
                    resultPrecursor.addAll(fromObject(i, rowArray[i]));
117
                }
118
            } else {
119
                resultPrecursor.addAll(fromObject(null, row));
120
            }
121
122
        }
123
124
        return new ArrayList<>(resultPrecursor);
125
    }
126
    
127
    private static List<ReflectionInfo> fromObject(Integer index, Object obj) throws IntrospectionException {
128
        if(obj == null || obj.getClass().getName().startsWith("java.")) {
129
            // Let the default handle this
130
            return Collections.singletonList(new ReflectionInfo(index, null));
131
        } else {
132
            BeanInfo bi = Introspector.getBeanInfo(obj.getClass(), Object.class);
133
            List<ReflectionInfo> result = new ArrayList<>();
134
            for(PropertyDescriptor pd: bi.getPropertyDescriptors()) {
135
                result.add(new ReflectionInfo(index, pd.getName()));
136
            }
137
            if(result.isEmpty()) {
138
                return Collections.singletonList(new ReflectionInfo(index, null));
139
            } else {
140
                return result;
141
            }
142
        }
143
    }
144
}
(-)j2ee.persistence/src/org/netbeans/modules/j2ee/persistence/jpqleditor/ui/ReflectiveTableModel.java (+126 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2015 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2015 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.j2ee.persistence.jpqleditor.ui;
43
44
import java.lang.reflect.InvocationTargetException;
45
import java.lang.reflect.Method;
46
import java.util.List;
47
import java.util.logging.Level;
48
import java.util.logging.Logger;
49
import javax.swing.table.AbstractTableModel;
50
51
public class ReflectiveTableModel extends AbstractTableModel {
52
53
    private static final Logger LOG = Logger.getLogger(ReflectiveTableModel.class.getName());
54
    
55
    private List<ReflectionInfo> reflectionInfo;
56
    private List<Object> data;
57
    
58
    public ReflectiveTableModel(List<ReflectionInfo> reflectionInfo, List<Object> data) {
59
        if(data == null) {
60
            throw new IllegalArgumentException("Data must not be null");  //NOI18N
61
        }
62
        this.reflectionInfo = reflectionInfo;
63
        this.data = data;
64
    }
65
66
    @Override
67
    public String getColumnName(int columnIndex) {
68
        ReflectionInfo ri = reflectionInfo.get(columnIndex);
69
        if(ri.getIndex() == null && ri.getPropertyName() == null) {
70
            return "Result";
71
        } else if (ri.getIndex() != null && ri.getPropertyName() == null) {
72
            return "[" + ri.getIndex() + "]";
73
        }  else if (ri.getIndex() == null && ri.getPropertyName() != null) {
74
            return ri.getPropertyName();
75
        } else {
76
            return "[" + ri.getIndex() + "]." + ri.getPropertyName();
77
        }
78
    }
79
    
80
    @Override
81
    public int getRowCount() {
82
        return data.size();
83
    }
84
85
    @Override
86
    public int getColumnCount() {
87
        return reflectionInfo.size();
88
    }
89
90
    @Override
91
    public Object getValueAt(int rowIndex, int columnIndex) {
92
        Object dataItem = data.get(rowIndex);
93
        if(dataItem == null) {
94
            return null;
95
        }
96
        ReflectionInfo ri = reflectionInfo.get(columnIndex);
97
        if(ri.getIndex() != null) {
98
            dataItem = ((Object[]) dataItem)[ri.getIndex()];
99
        }
100
        if(dataItem == null || ri.getPropertyName() == null) {
101
            return dataItem;
102
        }
103
        Class klass = dataItem.getClass();
104
        String property = ri.getPropertyName();
105
        Method accessor = null;
106
        String getterString = "get" + property.substring(0, 1).toUpperCase() + property.substring(1);
107
        String isString = "is" + property.substring(0, 1).toUpperCase() + property.substring(1);
108
        try {
109
            Method getter = klass.getMethod(getterString);
110
            return getter.invoke(dataItem);
111
        } catch (NoSuchMethodException ex) {
112
        } catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
113
            LOG.log(Level.WARNING, "Failed to reflect", ex); //NOI18N
114
            return null;
115
        }
116
        try {
117
            Method getter = klass.getMethod(isString);
118
            return getter.invoke(dataItem);
119
        } catch (NoSuchMethodException ex) {
120
        } catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
121
            LOG.log(Level.WARNING, "Failed to reflect", ex);  //NOI18N
122
            return null;
123
        }
124
        return null;
125
    }
126
}
(-)j2ee.persistence/test/unit/src/org/netbeans/modules/j2ee/persistence/jpqleditor/ui/ReflectionInfoTest.java (+215 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2015 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2015 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.j2ee.persistence.jpqleditor.ui;
43
44
import java.beans.IntrospectionException;
45
import java.util.Arrays;
46
import java.util.Collections;
47
import java.util.List;
48
import org.junit.Test;
49
import static org.junit.Assert.*;
50
import static org.hamcrest.CoreMatchers.*;
51
52
public class ReflectionInfoTest {
53
54
    public ReflectionInfoTest() {
55
    }
56
57
    @Test
58
    public void testSingleResult() throws IntrospectionException {
59
        List<ReflectionInfo> ri = ReflectionInfo.prepare(Collections.singletonList(1));
60
        assertThat(ri.size(), is(1));
61
        assertNull(ri.get(0).getIndex());
62
        assertNull(ri.get(0).getPropertyName());
63
    }
64
65
    @Test
66
    public void testSimpleObject() throws IntrospectionException {
67
        List<ReflectionInfo> ri = ReflectionInfo.prepare(Collections.singletonList(new BaseObject()));
68
        assertThat(ri.size(), is(1));
69
        assertNull(ri.get(0).getIndex());
70
        assertThat(ri.get(0).getPropertyName(), is("id"));
71
    }
72
73
    @Test
74
    public void testInheritetObject() throws IntrospectionException {
75
        List<ReflectionInfo> ri = ReflectionInfo.prepare(Collections.singletonList(new DemoObject()));
76
        assertThat(ri.size(), is(3));
77
        assertNull(ri.get(0).getIndex());
78
        assertThat(ri.get(0).getPropertyName(), is("demoBool"));
79
        assertNull(ri.get(1).getIndex());
80
        assertThat(ri.get(1).getPropertyName(), is("id"));
81
        assertNull(ri.get(2).getIndex());
82
        assertThat(ri.get(2).getPropertyName(), is("title"));
83
    }
84
85
    @Test
86
    public void testMixedList() throws IntrospectionException {
87
        List<ReflectionInfo> ri = ReflectionInfo.prepare(Arrays.asList(new BaseObject[]{new DemoObject2(), new DemoObject()}));
88
        assertThat(ri.size(), is(4));
89
        assertNull(ri.get(0).getIndex());
90
        assertThat(ri.get(0).getPropertyName(), is("demoBool"));
91
        assertNull(ri.get(1).getIndex());
92
        assertThat(ri.get(1).getPropertyName(), is("id"));
93
        assertNull(ri.get(2).getIndex());
94
        assertThat(ri.get(2).getPropertyName(), is("title"));
95
        assertNull(ri.get(3).getIndex());
96
        assertThat(ri.get(3).getPropertyName(), is("titleId"));
97
    }
98
99
    @Test
100
    public void testMixedRowArray() throws IntrospectionException {
101
        List<ReflectionInfo> ri = ReflectionInfo.prepare(Collections.singletonList(new Object[]{1, new DemoObject()}));
102
        assertThat(ri.size(), is(4));
103
        assertThat(ri.get(0).getIndex(), is(0));
104
        assertNull(ri.get(0).getPropertyName());
105
        assertThat(ri.get(1).getIndex(), is(1));
106
        assertThat(ri.get(1).getPropertyName(), is("demoBool"));
107
        assertThat(ri.get(2).getIndex(), is(1));
108
        assertThat(ri.get(2).getPropertyName(), is("id"));
109
        assertThat(ri.get(3).getIndex(), is(1));
110
        assertThat(ri.get(3).getPropertyName(), is("title"));
111
    }
112
113
    @Test
114
    public void testNullRowLeadsToNonArray() throws IntrospectionException {
115
        List<ReflectionInfo> ri = ReflectionInfo.prepare(Arrays.asList(
116
                new Object[]{1, new DemoObject()},
117
                null,
118
                new Object[]{2, new DemoObject2()}
119
        ));
120
        assertThat(ri.size(), is(1));
121
        assertNull(ri.get(0).getPropertyName());
122
        assertNull(ri.get(0).getIndex());
123
    }
124
    
125
    @Test
126
    public void testNullValues() throws IntrospectionException {
127
        List<ReflectionInfo> ri = ReflectionInfo.prepare(Arrays.asList(
128
                null,
129
                null,
130
                null
131
        ));
132
        assertThat(ri.size(), is(1));
133
        assertNull(ri.get(0).getPropertyName());
134
        assertNull(ri.get(0).getIndex());
135
    }
136
    
137
    @Test
138
    public void testNullValueArrays() throws IntrospectionException {
139
        List<ReflectionInfo> ri = ReflectionInfo.prepare(Arrays.asList(
140
                new Object[] {null, null},
141
                new Object[] {null, null},
142
                new Object[] {null, null}
143
        ));
144
        assertThat(ri.size(), is(2));
145
        assertNull(ri.get(0).getPropertyName());
146
        assertThat(ri.get(0).getIndex(), is(0));
147
        assertNull(ri.get(1).getPropertyName());
148
        assertThat(ri.get(1).getIndex(), is(1));
149
    }
150
    
151
    @Test
152
    public void testPartitialNullValueArrays() throws IntrospectionException {
153
        List<ReflectionInfo> ri = ReflectionInfo.prepare(Arrays.asList(
154
                new Object[] {null, null},
155
                new Object[] {1, new BaseObject()},
156
                new Object[] {null, null}
157
        ));
158
        assertThat(ri.size(), is(3));
159
        assertNull(ri.get(0).getPropertyName());
160
        assertThat(ri.get(0).getIndex(), is(0));
161
        assertNull(ri.get(1).getPropertyName());
162
        assertThat(ri.get(1).getIndex(), is(1));
163
        assertThat(ri.get(2).getPropertyName(), is("id"));
164
        assertThat(ri.get(2).getIndex(), is(1));
165
    }
166
    
167
    public static class BaseObject {
168
169
        private int id;
170
171
        public int getId() {
172
            return id;
173
        }
174
175
        public void setId(int id) {
176
            this.id = id;
177
        }
178
    }
179
180
    public static class DemoObject2 extends BaseObject {
181
182
        private int titleId;
183
184
        public int getTitleId() {
185
            return titleId;
186
        }
187
188
        public void setTitleId(int titleId) {
189
            this.titleId = titleId;
190
        }
191
192
    }
193
194
    public static class DemoObject extends BaseObject {
195
196
        private String title;
197
        private boolean demoBool;
198
199
        public String getTitle() {
200
            return title;
201
        }
202
203
        public void setTitle(String title) {
204
            this.title = title;
205
        }
206
207
        public boolean isDemoBool() {
208
            return demoBool;
209
        }
210
211
        public void setDemoBool(boolean demoBool) {
212
            this.demoBool = demoBool;
213
        }
214
    }
215
}

Return to bug 256032