/
ClassDefAdapter.java
169 lines (144 loc) · 5.57 KB
/
ClassDefAdapter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* Copyright (C) 2006, 2007 Dennis Hunziker, Ueli Kistler
* Copyright (C) 2007 Reto Schuettel, Robin Stocker
*
* IFS Institute for Software, HSR Rapperswil, Switzerland
*
*/
package org.python.pydev.refactoring.ast.adapters;
import java.util.ArrayList;
import java.util.List;
import org.python.pydev.core.MisconfigurationException;
import org.python.pydev.parser.jython.ast.ClassDef;
import org.python.pydev.refactoring.ast.visitors.VisitorFactory;
import org.python.pydev.refactoring.ast.visitors.context.LocalAttributeVisitor;
import org.python.pydev.refactoring.ast.visitors.context.PropertyVisitor;
import org.python.pydev.refactoring.ast.visitors.context.ScopeAssignedVisitor;
import org.python.pydev.refactoring.ast.visitors.position.IndentVisitor;
public class ClassDefAdapter extends AbstractScopeNode<ClassDef> implements IClassDefAdapter {
private static final String OBJECT = "object";
private List<SimpleAdapter> attributes;
private List<PropertyAdapter> properties;
public ClassDefAdapter(ModuleAdapter module, AbstractScopeNode<?> parent, ClassDef node, AdapterPrefs adapterPrefs) {
super(module, parent, node, adapterPrefs);
this.attributes = null;
this.properties = null;
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#getBaseClassNames()
*/
public List<String> getBaseClassNames() {
return nodeHelper.getBaseClassName(getASTNode());
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#getBaseClasses()
*/
public List<IClassDefAdapter> getBaseClasses() throws MisconfigurationException {
return getModule().getBaseClasses(this);
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#hasBaseClass()
*/
public boolean hasBaseClass() {
return getBaseClassNames().size() > 0;
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#getAttributes()
*/
public List<SimpleAdapter> getAttributes() {
if(attributes == null){
LocalAttributeVisitor visitor = VisitorFactory.createContextVisitor(LocalAttributeVisitor.class, getASTNode(), getModule(), this);
attributes = visitor.getAll();
}
return attributes;
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#getProperties()
*/
public List<PropertyAdapter> getProperties() {
if(properties == null){
PropertyVisitor visitor = VisitorFactory.createContextVisitor(PropertyVisitor.class, getASTNode(), getModule(), this);
properties = visitor.getAll();
}
return properties;
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#getFunctionsInitFiltered()
*/
public List<FunctionDefAdapter> getFunctionsInitFiltered() {
List<FunctionDefAdapter> functionsFiltered = new ArrayList<FunctionDefAdapter>();
for(FunctionDefAdapter adapter:getFunctions()){
if(!(adapter.isInit())){
functionsFiltered.add(adapter);
}
}
return functionsFiltered;
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#hasFunctions()
*/
public boolean hasFunctions() {
return getFunctions().size() > 0;
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#hasFunctionsInitFiltered()
*/
public boolean hasFunctionsInitFiltered() {
return getFunctionsInitFiltered().size() > 0;
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#isNested()
*/
public boolean isNested() {
return nodeHelper.isFunctionOrClassDef(getParent().getASTNode());
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#hasAttributes()
*/
public boolean hasAttributes() {
return getAttributes().size() > 0;
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#getNodeBodyIndent()
*/
public int getNodeBodyIndent() {
ClassDef classNode = getASTNode();
IndentVisitor visitor = VisitorFactory.createVisitor(IndentVisitor.class, classNode.body[0]);
return visitor.getIndent();
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#hasInit()
*/
public boolean hasInit() {
return(getFirstInit() != null);
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#getFirstInit()
*/
public FunctionDefAdapter getFirstInit() {
for(FunctionDefAdapter func:getFunctions()){
if(func.isInit()){
return func;
}
}
return null;
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#getAssignedVariables()
*/
public List<SimpleAdapter> getAssignedVariables() {
ScopeAssignedVisitor visitor = VisitorFactory.createContextVisitor(ScopeAssignedVisitor.class, getASTNode(), this.getModule(), this);
return visitor.getAll();
}
/* (non-Javadoc)
* @see org.python.pydev.refactoring.ast.adapters.IClassDefAdapter#isNewStyleClass()
*/
public boolean isNewStyleClass() {
for(String base:getBaseClassNames()){
if(base.compareTo(OBJECT) == 0){
return true;
}
}
return false;
}
}