下面列出了org.eclipse.jdt.core.dom.TypeDeclaration#getFields ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Searching changed default values
* @param version1
* @param version2
*/
private void findDefaultValueFields(APIVersion version1, APIVersion version2){
for (TypeDeclaration type : version1.getApiAcessibleTypes()) {
if(version2.containsAccessibleType(type)){
for (FieldDeclaration fieldInVersion1 : type.getFields()) {
if(this.isFieldAccessible(fieldInVersion1)){
FieldDeclaration fieldInVersion2 = version2.getVersionField(fieldInVersion1, type);
if(this.isFieldAccessible(fieldInVersion2) && this.thereAreDifferentDefaultValueField(fieldInVersion1, fieldInVersion2)){
String description = this.description.changeDefaultValue(UtilTools.getFieldName(fieldInVersion2), UtilTools.getPath(type));
this.addChange(type, fieldInVersion2, Category.FIELD_CHANGE_DEFAULT_VALUE, true, description);
}
}
}
}
}
}
/**
* Searching changed fields type
* @param version1
* @param version2
*/
private void findChangedTypeFields(APIVersion version1, APIVersion version2) {
for (TypeDeclaration type : version1.getApiAcessibleTypes()) {
if(version2.containsAccessibleType(type)){
for (FieldDeclaration fieldInVersion1 : type.getFields()) {
if(!UtilTools.isVisibilityPrivate(fieldInVersion1) && !UtilTools.isVisibilityDefault(fieldInVersion1)){
FieldDeclaration fieldInVersion2 = version2.getVersionField(fieldInVersion1, type);
if(fieldInVersion2 != null && !UtilTools.isVisibilityPrivate(fieldInVersion2)){
if(!fieldInVersion1.getType().toString().equals(fieldInVersion2.getType().toString())){
String description = this.description.returnType(UtilTools.getFieldName(fieldInVersion2), UtilTools.getPath(type));
this.addChange(type, fieldInVersion2, Category.FIELD_CHANGE_TYPE, true, description);
}
}
}
}
}
}
}
/**
* Finding added fields
* @param version1
* @param version2
*/
private void findAddedFields(APIVersion version1, APIVersion version2) {
for (TypeDeclaration typeVersion2 : version2.getApiAcessibleTypes()) {
if(version1.containsAccessibleType(typeVersion2)){
for (FieldDeclaration fieldInVersion2 : typeVersion2.getFields()) {
String fullNameAndPath = this.getNameAndPath(fieldInVersion2, typeVersion2);
if(!UtilTools.isVisibilityPrivate(fieldInVersion2) && !UtilTools.isVisibilityDefault(fieldInVersion2) && !this.fieldWithPathChanged.contains(fullNameAndPath)){
FieldDeclaration fieldInVersion1;
fieldInVersion1 = version1.getVersionField(fieldInVersion2, typeVersion2);
if(fieldInVersion1 == null){
String description = this.description.addition(UtilTools.getFieldName(fieldInVersion2), UtilTools.getPath(typeVersion2));
this.addChange(typeVersion2, fieldInVersion2, Category.FIELD_ADD, false, description);
}
}
}
}
}
}
/**
* Finding removed fields. If class was removed, class removal is a breaking change.
* @param version1
* @param version2
*/
private void findRemoveAndRefactoringFields(APIVersion version1, APIVersion version2) {
for (TypeDeclaration type : version1.getApiAcessibleTypes()) {
if(version2.containsAccessibleType(type)){
for (FieldDeclaration fieldInVersion1 : type.getFields()) {
if(!UtilTools.isVisibilityPrivate(fieldInVersion1)){
FieldDeclaration fieldInVersion2 = version2.getVersionField(fieldInVersion1, type);
if(fieldInVersion2 == null){
Boolean refactoring = this.checkAndProcessRefactoring(fieldInVersion1, type);
if(!refactoring){
this.processRemoveField(fieldInVersion1, type);
}
}
}
}
}
}
}
/**
* Returns the <CODE>FieldDeclaration</CODE> for the specified field name.
* The field has to be declared in the specified
* <CODE>TypeDeclaration</CODE>.
*
* @param type
* The <CODE>TypeDeclaration</CODE>, where the
* <CODE>FieldDeclaration</CODE> is declared in.
* @param fieldName
* The simple field name to search for.
* @return the <CODE>FieldDeclaration</CODE> found in the specified
* <CODE>TypeDeclaration</CODE>.
* @throws FieldDeclarationNotFoundException
* if no matching <CODE>FieldDeclaration</CODE> was found.
*/
public static FieldDeclaration getFieldDeclaration(TypeDeclaration type, String fieldName)
throws FieldDeclarationNotFoundException {
requireNonNull(type, "type declaration");
requireNonNull(fieldName, "field name");
for (FieldDeclaration field : type.getFields()) {
for (Object fragObj : field.fragments()) {
VariableDeclarationFragment fragment = (VariableDeclarationFragment) fragObj;
if (fieldName.equals(fragment.getName().getIdentifier())) {
return field;
}
}
}
throw new FieldDeclarationNotFoundException(type, fieldName);
}
private void addOwnerProposals(List<ICompletionProposal> proposals,
TypeDeclaration ownerDecl) {
// Generate proposals for "ui:field" annotated field declarations
for (FieldDeclaration fieldDecl : ownerDecl.getFields()) {
if (UiBinderUtilities.isUiField(fieldDecl)) {
// If the widget qualified type name is known and the binding can be
// resolved, validate them.
ITypeBinding binding = fieldDecl.getType().resolveBinding();
if (widgetTypeName != null && binding != null
&& !widgetTypeName.equals(binding.getQualifiedName())) {
continue;
}
addFieldProposal(proposals, fieldDecl);
}
}
}
@Override
public boolean visit(TypeDeclaration typeDecl) {
if (!shouldValidateType(typeDecl)) {
return true;
}
for (IType uiBinderType : uiBinderToOwner.getUiBinderTypes(typeDecl.resolveBinding().getQualifiedName())) {
result.addTypeDependency(uiBinderType.getFullyQualifiedName());
}
for (FieldDeclaration field : typeDecl.getFields()) {
if (UiBinderUtilities.isUiField(field)) {
validateUiField(field);
}
}
for (MethodDeclaration method : typeDecl.getMethods()) {
if (UiBinderUtilities.isUiHandler(method)) {
validateUiHandler(method);
}
}
return true;
}
private void initializeDeclaration(TypeDeclaration node) {
FieldDeclaration[] fields= node.getFields();
for (int i= 0; i < fields.length; i++) {
FieldDeclaration fieldDeclaration= fields[i];
List<VariableDeclarationFragment> fragments= fieldDeclaration.fragments();
for (Iterator<VariableDeclarationFragment> iterator= fragments.iterator(); iterator.hasNext();) {
VariableDeclarationFragment vdf= iterator.next();
FieldInfo fieldInfo= getFieldInfo(vdf.getName().getIdentifier());
if (fieldInfo != null) {
Assert.isNotNull(vdf);
fieldInfo.declaration= vdf;
fieldInfo.pi.setOldBinding(vdf.resolveBinding());
}
}
}
}
public static VariableDeclaration findFieldDeclaration(AbstractVariable variable, TypeDeclaration typeDeclaration) {
for(FieldDeclaration fieldDeclaration : typeDeclaration.getFields()) {
List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
for(VariableDeclarationFragment fragment : fragments) {
if(variable.getVariableBindingKey().equals(fragment.resolveBinding().getKey())) {
return fragment;
}
}
}
//fragment was not found in typeDeclaration
Type superclassType = typeDeclaration.getSuperclassType();
if(superclassType != null) {
String superclassQualifiedName = superclassType.resolveBinding().getQualifiedName();
SystemObject system = ASTReader.getSystemObject();
ClassObject superclassObject = system.getClassObject(superclassQualifiedName);
if(superclassObject != null) {
AbstractTypeDeclaration superclassTypeDeclaration = superclassObject.getAbstractTypeDeclaration();
if(superclassTypeDeclaration instanceof TypeDeclaration) {
return findFieldDeclaration(variable, (TypeDeclaration)superclassTypeDeclaration);
}
}
}
return null;
}
/**
* Finding fields with changed visibility
* @param version1
* @param version2
*/
private void findChangedVisibilityFields(APIVersion version1, APIVersion version2) {
for(TypeDeclaration typeVersion1 : version1.getApiAcessibleTypes()){
if(version2.containsAccessibleType(typeVersion1)){
for (FieldDeclaration fieldVersion1 : typeVersion1.getFields()){
FieldDeclaration fieldVersion2 = version2.getVersionField(fieldVersion1, typeVersion1);
this.checkGainOrLostVisibility(typeVersion1, fieldVersion1, fieldVersion2);
}
}
}
}
/**
* Finding deprecated fields
* @param version1
* @param version2
*/
private void findAddedDeprecatedFields(APIVersion version1, APIVersion version2) {
for(TypeDeclaration typeVersion2 : version2.getApiAcessibleTypes()){
for(FieldDeclaration fieldVersion2 : typeVersion2.getFields()){
if(this.isFieldAccessible(fieldVersion2) && this.isDeprecated(fieldVersion2, typeVersion2)){
FieldDeclaration fieldInVersion1 = version1.getVersionField(fieldVersion2, typeVersion2);
if(fieldInVersion1 == null || !this.isDeprecated(fieldInVersion1, version1.getVersionAccessibleType(typeVersion2))){
String description = this.description.deprecate(UtilTools.getFieldName(fieldVersion2), UtilTools.getPath(typeVersion2));
this.addChange(typeVersion2, fieldVersion2, Category.FIELD_DEPRECATED, false, description);
}
}
}
}
}
/**
* Finding change in final modifier
*
* @param version1
* @param version2
*/
private void findChangedFinal(APIVersion version1, APIVersion version2) {
for (TypeDeclaration typeInVersion1 : version1.getApiAcessibleTypes()) {
if(version2.containsType(typeInVersion1)){//Se type ainda existe.
for(FieldDeclaration fieldVersion1: typeInVersion1.getFields()){
FieldDeclaration fieldVersion2 = version2.getVersionField(fieldVersion1, typeInVersion1);
if(this.isFieldAccessible(fieldVersion1) && (fieldVersion2 != null)){
this.diffModifierFinal(typeInVersion1, fieldVersion1, fieldVersion2);
}
}
}
}
}
public FieldDeclaration getVersionField(FieldDeclaration field, TypeDeclaration type){
for (TypeDeclaration versionType : this.apiAccessibleTypes) {
if(versionType.getName().toString().equals(type.getName().toString())){
for (FieldDeclaration versionField : versionType.getFields()) {
String name1 = UtilTools.getFieldName(versionField);
String name2 = UtilTools.getFieldName(field);
if(name1 != null && name2 != null && name1.equals(name2)){
return versionField;
}
}
}
}
return null;
}
public boolean visit(TypeDeclaration node) {
Pair<String, String> clazzAndMethodName = NodeUtils.getTypeDecAndMethodDec(node.getName());
String clazz = clazzAndMethodName.getFirst();
AST ast = AST.newAST(AST.JLS8);
Type type = ast.newSimpleType(ast.newSimpleName(clazz));
ProjectInfo.addFieldType(clazz, "THIS", type);
Type suType = node.getSuperclassType();
if(suType != null){
ProjectInfo.addFieldType(clazz, "SUPER", suType);
ProjectInfo.addSuperClass(clazz, suType.toString());
}
List<Object> sInterfaces = node.superInterfaceTypes();
if(sInterfaces != null){
for(Object object : sInterfaces){
if(object instanceof Type){
Type interfaceType = (Type) object;
ProjectInfo.addSuperInterface(clazz, interfaceType.toString());
}
}
}
FieldDeclaration fields[] = node.getFields();
for (FieldDeclaration f : fields) {
for (Object o : f.fragments()) {
VariableDeclarationFragment vdf = (VariableDeclarationFragment) o;
Type tmpType = f.getType();
if(vdf.getExtraDimensions() > 0){
tmpType = ast.newArrayType((Type) ASTNode.copySubtree(ast, tmpType), vdf.getExtraDimensions());
}
ProjectInfo.addFieldType(clazz, vdf.getName().toString(), tmpType);
}
}
return true;
}
private boolean visitInterface(TypeDeclaration node) {
InterfaceInfo interfaceInfo = new InterfaceInfo();
interfaceInfo.name = node.getName().getFullyQualifiedName();
interfaceInfo.fullName = NameResolver.getFullName(node);
interfaceInfo.visibility = getVisibility(node);
List<Type> superInterfaceList = node.superInterfaceTypes();
for (Type superInterface : superInterfaceList)
interfaceInfo.superInterfaceTypeList.add(NameResolver.getFullName(superInterface));
if (node.getJavadoc() != null)
interfaceInfo.comment = sourceContent.substring(node.getJavadoc().getStartPosition(), node.getJavadoc().getStartPosition() + node.getJavadoc().getLength());
interfaceInfo.content = sourceContent.substring(node.getStartPosition(), node.getStartPosition() + node.getLength());
elementInfoPool.interfaceInfoMap.put(interfaceInfo.fullName, interfaceInfo);
MethodDeclaration[] methodDeclarations = node.getMethods();
for (MethodDeclaration methodDeclaration : methodDeclarations) {
MethodInfo methodInfo = createMethodInfo(methodDeclaration, interfaceInfo.fullName);
elementInfoPool.methodInfoMap.put(methodInfo.hashName(), methodInfo);
}
FieldDeclaration[] fieldDeclarations = node.getFields();
for (FieldDeclaration fieldDeclaration : fieldDeclarations) {
List<FieldInfo> fieldInfos = createFieldInfos(fieldDeclaration, interfaceInfo.fullName);
for (FieldInfo fieldInfo : fieldInfos)
elementInfoPool.fieldInfoMap.put(fieldInfo.hashName(), fieldInfo);
}
return true;
}
private boolean visitClass(TypeDeclaration node) {
ClassInfo classInfo = new ClassInfo();
classInfo.name = node.getName().getFullyQualifiedName();
classInfo.fullName = NameResolver.getFullName(node);
classInfo.visibility = getVisibility(node);
classInfo.isAbstract = isAbstract(node);
classInfo.isFinal = isFinal(node);
classInfo.superClassType = node.getSuperclassType() == null ? "java.lang.Object" : NameResolver.getFullName(node.getSuperclassType());
List<Type> superInterfaceList = node.superInterfaceTypes();
for (Type superInterface : superInterfaceList)
classInfo.superInterfaceTypeList.add(NameResolver.getFullName(superInterface));
if (node.getJavadoc() != null)
classInfo.comment = sourceContent.substring(node.getJavadoc().getStartPosition(), node.getJavadoc().getStartPosition() + node.getJavadoc().getLength());
classInfo.content = sourceContent.substring(node.getStartPosition(), node.getStartPosition() + node.getLength());
elementInfoPool.classInfoMap.put(classInfo.fullName, classInfo);
MethodDeclaration[] methodDeclarations = node.getMethods();
for (MethodDeclaration methodDeclaration : methodDeclarations) {
MethodInfo methodInfo = createMethodInfo(methodDeclaration, classInfo.fullName);
elementInfoPool.methodInfoMap.put(methodInfo.hashName(), methodInfo);
}
FieldDeclaration[] fieldDeclarations = node.getFields();
for (FieldDeclaration fieldDeclaration : fieldDeclarations) {
List<FieldInfo> fieldInfos = createFieldInfos(fieldDeclaration, classInfo.fullName);
for (FieldInfo fieldInfo : fieldInfos)
elementInfoPool.fieldInfoMap.put(fieldInfo.hashName(), fieldInfo);
}
return true;
}
private List<? extends BuilderField> findBuilderFieldsRecursively(TypeDeclaration currentOwnerClass) {
List<BuilderField> builderFields = new ArrayList<>();
if (preferencesManager.getPreferenceValue(INCLUDE_VISIBLE_FIELDS_FROM_SUPERCLASS)) {
builderFields.addAll(getFieldsFromSuperclass(currentOwnerClass));
}
FieldDeclaration[] fields = currentOwnerClass.getFields();
for (FieldDeclaration field : fields) {
List<VariableDeclarationFragment> fragments = field.fragments();
builderFields.addAll(getFilteredDeclarations(field, fragments));
}
return builderFields;
}
public void insertMethodToFirstPlace(TypeDeclaration originalType, ListRewrite listRewrite, MethodDeclaration constructor) {
FieldDeclaration[] fields = originalType.getFields();
if (fields == null || fields.length == 0) {
listRewrite.insertFirst(constructor, null);
} else {
listRewrite.insertAfter(constructor, fields[fields.length - 1], null);
}
}
public TypeCheckCodeFragmentAnalyzer(TypeCheckElimination typeCheckElimination,
TypeDeclaration typeDeclaration, MethodDeclaration typeCheckMethod, IFile iFile) {
this.typeCheckElimination = typeCheckElimination;
this.typeDeclaration = typeDeclaration;
this.typeCheckMethod = typeCheckMethod;
this.fields = typeDeclaration.getFields();
this.methods = typeDeclaration.getMethods();
this.typeVariableCounterMap = new LinkedHashMap<SimpleName, Integer>();
this.typeMethodInvocationCounterMap = new LinkedHashMap<MethodInvocation, Integer>();
this.complexExpressionMap = new LinkedHashMap<Expression, IfStatementExpressionAnalyzer>();
typeCheckElimination.setTypeCheckClass(typeDeclaration);
typeCheckElimination.setTypeCheckMethod(typeCheckMethod);
typeCheckElimination.setTypeCheckIFile(iFile);
processTypeCheckCodeFragment();
}