Skip to content

[Bug]: Missing visible annotations of fields #1168

@rnoennig

Description

@rnoennig

What happened?

In sootup 1.3.0 I am not seeing my custom annotations on fields.

This is my sootup code:

List<AnalysisInputLocation> inputLocations = new ArrayList();

inputLocations.add(new JavaClassPathAnalysisInputLocation("/Users/rnoennig/Dev/MyProject/classes"));
inputLocations.add(new DefaultRTJarAnalysisInputLocation());

JavaView view = new JavaView(inputLocations);

JavaClassType targetServiceClassType = view.getIdentifierFactory().getClassType("service.MyServiceImpl");

JavaSootClass targetServiceClass = view.getClass(targetServiceClassType).get();
Set<JavaSootField> allFields = targetServiceClass.getFields();
JavaSootField firstField = allFields.stream().findFirst().get();
System.out.println(firstField.getAnnotations(Optional.of(view)));

This is the output:

[]

This would be the expected output:

[@service.Inject]

The project I am analysing looks like this:
The class:

package service;

@Service
public class MyServiceImpl {
    @Inject
    private UserIdService userIdService;
}

The annotation:

package service;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Marker interface for injectable instances
 */
@Target({ElementType.METHOD,ElementType.CONSTRUCTOR,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {

}

When I make the following changes to AsmClassSource the above code works as expected:

diff --git a/./sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmClassSource.java b/Users/rnoennig/eclipse-workspace-4/SecCommerceAnalysis/src/main/java/sootup/java/bytecode/frontend/AsmClassSource.java
index 38fdedbdd7..5c7202d867 100644
--- a/./sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmClassSource.java
+++ b/Users/rnoennig/eclipse-workspace-4/SecCommerceAnalysis/src/main/java/sootup/java/bytecode/frontend/AsmClassSource.java
@@ -23,21 +23,36 @@ package sootup.java.bytecode.frontend;
  */
 
 import java.nio.file.Path;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.EnumSet;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
 import java.util.stream.Collectors;
 import java.util.stream.StreamSupport;
 import javax.annotation.Nonnull;
-import org.objectweb.asm.tree.*;
+import org.objectweb.asm.tree.AnnotationNode;
+import org.objectweb.asm.tree.ClassNode;
+import org.objectweb.asm.tree.FieldNode;
 import sootup.core.IdentifierFactory;
 import sootup.core.frontend.ResolveException;
 import sootup.core.inputlocation.AnalysisInputLocation;
 import sootup.core.jimple.basic.NoPositionInformation;
-import sootup.core.model.*;
+import sootup.core.model.ClassModifier;
+import sootup.core.model.FieldModifier;
+import sootup.core.model.MethodModifier;
 import sootup.core.signatures.FieldSignature;
 import sootup.core.signatures.MethodSignature;
 import sootup.core.types.ClassType;
 import sootup.core.types.Type;
-import sootup.java.core.*;
+import sootup.java.core.AnnotationUsage;
+import sootup.java.core.JavaIdentifierFactory;
+import sootup.java.core.JavaSootClassSource;
+import sootup.java.core.JavaSootField;
+import sootup.java.core.JavaSootMethod;
 import sootup.java.core.types.JavaClassType;
 
 /** A ClassSource that reads from Java bytecode */
@@ -65,11 +80,19 @@ class AsmClassSource extends JavaSootClassSource {
                   signatureFactory.getFieldSignature(fieldName, classSignature, fieldType);
               EnumSet<FieldModifier> modifiers = AsmUtil.getFieldModifiers(fieldNode.access);
 
+              List<AnnotationUsage> annotations = new ArrayList<>();
+              if (fieldNode.invisibleAnnotations != null) {
+                  annotations.addAll(convertAnnotation(fieldNode.invisibleAnnotations));
+              }
+              if (fieldNode.visibleAnnotations != null) {
+                  annotations.addAll(convertAnnotation(fieldNode.visibleAnnotations));
+              }
+
               // TODO: add Position info
               return new JavaSootField(
                   fieldSignature,
                   modifiers,
-                  convertAnnotation(fieldNode.invisibleAnnotations),
+                  annotations,
                   NoPositionInformation.getInstance());
             })
         .collect(Collectors.toSet());
@@ -182,7 +205,7 @@ class AsmClassSource extends JavaSootClassSource {
   }
 
   @Nonnull
-  public Position resolvePosition() {
+  public sootup.core.model.Position resolvePosition() {
     // TODO [ms]: implement line numbers for bytecode
     return NoPositionInformation.getInstance();
   }

I suspect there is a relation to #611

Version

Latest release (e.g. via Maven)

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions