Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <jni.h>
     18 #include <vector>
     19 
     20 #include "art_field-inl.h"
     21 #include "base/enums.h"
     22 #include "common_compiler_test.h"
     23 #include "mirror/field-inl.h"
     24 #include "proxy_test.h"
     25 #include "scoped_thread_state_change-inl.h"
     26 
     27 namespace art {
     28 namespace proxy_test {
     29 
     30 class ProxyTest : public CommonRuntimeTest {};
     31 
     32 // Creates a proxy class and check ClassHelper works correctly.
     33 TEST_F(ProxyTest, ProxyClassHelper) {
     34   ScopedObjectAccess soa(Thread::Current());
     35   jobject jclass_loader = LoadDex("Interfaces");
     36   StackHandleScope<4> hs(soa.Self());
     37   Handle<mirror::ClassLoader> class_loader(
     38       hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
     39 
     40   Handle<mirror::Class> I(hs.NewHandle(
     41       class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
     42   Handle<mirror::Class> J(hs.NewHandle(
     43       class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
     44   ASSERT_TRUE(I != nullptr);
     45   ASSERT_TRUE(J != nullptr);
     46 
     47   std::vector<mirror::Class*> interfaces;
     48   interfaces.push_back(I.Get());
     49   interfaces.push_back(J.Get());
     50   Handle<mirror::Class> proxy_class(hs.NewHandle(
     51       GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1234", interfaces)));
     52   interfaces.clear();  // Don't least possibly stale objects in the array as good practice.
     53   ASSERT_TRUE(proxy_class != nullptr);
     54   ASSERT_TRUE(proxy_class->IsProxyClass());
     55   ASSERT_TRUE(proxy_class->IsInitialized());
     56 
     57   EXPECT_EQ(2U, proxy_class->NumDirectInterfaces());  // Interfaces$I and Interfaces$J.
     58   EXPECT_OBJ_PTR_EQ(I.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class.Get(), 0));
     59   EXPECT_OBJ_PTR_EQ(J.Get(), mirror::Class::GetDirectInterface(soa.Self(), proxy_class.Get(), 1));
     60   std::string temp;
     61   const char* proxy_class_descriptor = proxy_class->GetDescriptor(&temp);
     62   EXPECT_STREQ("L$Proxy1234;", proxy_class_descriptor);
     63   EXPECT_EQ(nullptr, proxy_class->GetSourceFile());
     64 }
     65 
     66 // Creates a proxy class and check FieldHelper works correctly.
     67 TEST_F(ProxyTest, ProxyFieldHelper) {
     68   ScopedObjectAccess soa(Thread::Current());
     69   jobject jclass_loader = LoadDex("Interfaces");
     70   StackHandleScope<9> hs(soa.Self());
     71   Handle<mirror::ClassLoader> class_loader(
     72       hs.NewHandle(soa.Decode<mirror::ClassLoader>(jclass_loader)));
     73 
     74   Handle<mirror::Class> I(hs.NewHandle(
     75       class_linker_->FindClass(soa.Self(), "LInterfaces$I;", class_loader)));
     76   Handle<mirror::Class> J(hs.NewHandle(
     77       class_linker_->FindClass(soa.Self(), "LInterfaces$J;", class_loader)));
     78   ASSERT_TRUE(I != nullptr);
     79   ASSERT_TRUE(J != nullptr);
     80 
     81   Handle<mirror::Class> proxyClass;
     82   {
     83     std::vector<mirror::Class*> interfaces;
     84     interfaces.push_back(I.Get());
     85     interfaces.push_back(J.Get());
     86     proxyClass = hs.NewHandle(
     87         GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1234", interfaces));
     88   }
     89 
     90   ASSERT_TRUE(proxyClass != nullptr);
     91   ASSERT_TRUE(proxyClass->IsProxyClass());
     92   ASSERT_TRUE(proxyClass->IsInitialized());
     93 
     94   EXPECT_TRUE(proxyClass->GetIFieldsPtr() == nullptr);
     95 
     96   LengthPrefixedArray<ArtField>* static_fields = proxyClass->GetSFieldsPtr();
     97   ASSERT_TRUE(static_fields != nullptr);
     98   ASSERT_EQ(2u, proxyClass->NumStaticFields());
     99 
    100   Handle<mirror::Class> interfacesFieldClass(
    101       hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Class;")));
    102   ASSERT_TRUE(interfacesFieldClass != nullptr);
    103   Handle<mirror::Class> throwsFieldClass(
    104       hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[[Ljava/lang/Class;")));
    105   ASSERT_TRUE(throwsFieldClass != nullptr);
    106 
    107   // Test "Class[] interfaces" field.
    108   ArtField* field = &static_fields->At(0);
    109   EXPECT_STREQ("interfaces", field->GetName());
    110   EXPECT_STREQ("[Ljava/lang/Class;", field->GetTypeDescriptor());
    111   EXPECT_OBJ_PTR_EQ(interfacesFieldClass.Get(), field->ResolveType());
    112   std::string temp;
    113   EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
    114   EXPECT_FALSE(field->IsPrimitiveType());
    115 
    116   // Test "Class[][] throws" field.
    117   field = &static_fields->At(1);
    118   EXPECT_STREQ("throws", field->GetName());
    119   EXPECT_STREQ("[[Ljava/lang/Class;", field->GetTypeDescriptor());
    120   EXPECT_OBJ_PTR_EQ(throwsFieldClass.Get(), field->ResolveType());
    121   EXPECT_STREQ("L$Proxy1234;", field->GetDeclaringClass()->GetDescriptor(&temp));
    122   EXPECT_FALSE(field->IsPrimitiveType());
    123 }
    124 
    125 // Creates two proxy classes and check the art/mirror fields of their static fields.
    126 TEST_F(ProxyTest, CheckArtMirrorFieldsOfProxyStaticFields) {
    127   ScopedObjectAccess soa(Thread::Current());
    128   jobject jclass_loader = LoadDex("Interfaces");
    129   StackHandleScope<7> hs(soa.Self());
    130 
    131   Handle<mirror::Class> proxyClass0;
    132   Handle<mirror::Class> proxyClass1;
    133   {
    134     std::vector<mirror::Class*> interfaces;
    135     proxyClass0 = hs.NewHandle(
    136         GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy0", interfaces));
    137     proxyClass1 = hs.NewHandle(
    138         GenerateProxyClass(soa, jclass_loader, class_linker_, "$Proxy1", interfaces));
    139   }
    140 
    141   ASSERT_TRUE(proxyClass0 != nullptr);
    142   ASSERT_TRUE(proxyClass0->IsProxyClass());
    143   ASSERT_TRUE(proxyClass0->IsInitialized());
    144   ASSERT_TRUE(proxyClass1 != nullptr);
    145   ASSERT_TRUE(proxyClass1->IsProxyClass());
    146   ASSERT_TRUE(proxyClass1->IsInitialized());
    147 
    148   LengthPrefixedArray<ArtField>* static_fields0 = proxyClass0->GetSFieldsPtr();
    149   ASSERT_TRUE(static_fields0 != nullptr);
    150   ASSERT_EQ(2u, static_fields0->size());
    151   LengthPrefixedArray<ArtField>* static_fields1 = proxyClass1->GetSFieldsPtr();
    152   ASSERT_TRUE(static_fields1 != nullptr);
    153   ASSERT_EQ(2u, static_fields1->size());
    154 
    155   EXPECT_OBJ_PTR_EQ(static_fields0->At(0).GetDeclaringClass(), proxyClass0.Get());
    156   EXPECT_OBJ_PTR_EQ(static_fields0->At(1).GetDeclaringClass(), proxyClass0.Get());
    157   EXPECT_OBJ_PTR_EQ(static_fields1->At(0).GetDeclaringClass(), proxyClass1.Get());
    158   EXPECT_OBJ_PTR_EQ(static_fields1->At(1).GetDeclaringClass(), proxyClass1.Get());
    159 
    160   ASSERT_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
    161   ASSERT_FALSE(Runtime::Current()->IsActiveTransaction());
    162   Handle<mirror::Field> field00 =
    163       hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
    164           soa.Self(), &static_fields0->At(0), true));
    165   Handle<mirror::Field> field01 =
    166       hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
    167           soa.Self(), &static_fields0->At(1), true));
    168   Handle<mirror::Field> field10 =
    169       hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
    170           soa.Self(), &static_fields1->At(0), true));
    171   Handle<mirror::Field> field11 =
    172       hs.NewHandle(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
    173           soa.Self(), &static_fields1->At(1), true));
    174   EXPECT_EQ(field00->GetArtField(), &static_fields0->At(0));
    175   EXPECT_EQ(field01->GetArtField(), &static_fields0->At(1));
    176   EXPECT_EQ(field10->GetArtField(), &static_fields1->At(0));
    177   EXPECT_EQ(field11->GetArtField(), &static_fields1->At(1));
    178 }
    179 
    180 }  // namespace proxy_test
    181 }  // namespace art
    182